标题: STM32串口实验注意点(基于STM32ZET6) [打印本页]

作者: 大力水手PP    时间: 2018-10-7 11:09
标题: STM32串口实验注意点(基于STM32ZET6)
//usart.c
#include "sys.h"
#include "usart.h"
#include "includes.h"       
void My_USART_Init(void)
{
        GPIO_InitTypeDef GPIO_InitStrue;
        USART_InitTypeDef USART_Initstrue;
        NVIC_InitTypeDef NVIC_Initstrue;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);

        GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;//有时需注意这里的模式
        GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;
        GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
        GPIO_Init(GPIOB,&GPIO_InitStrue);                     //每个引脚配置都需要
       
        GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING
        GPIO_InitStrue.GPIO_Pin=GPIO_Pin_11;
        GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
        GPIO_Init(GPIOB,&GPIO_InitStrue);
       
        USART_Initstrue.USART_BaudRate=115200;
        USART_Initstrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
        USART_Initstrue.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;
        USART_Initstrue.USART_Parity=USART_Parity_No;
        USART_Initstrue.USART_StopBits=USART_StopBits_1;
        USART_Initstrue.USART_WordLength=USART_WordLength_8b;
        USART_Init(USART3,&USART_Initstrue);
        USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
        USART_Cmd(USART3,ENABLE);

        NVIC_Initstrue.NVIC_IRQChannel=USART3_IRQn;
        NVIC_Initstrue.NVIC_IRQChannelCmd=ENABLE;
        NVIC_Initstrue.NVIC_IRQChannelPreemptionPriority=1;
        NVIC_Initstrue.NVIC_IRQChannelSubPriority=1;
       
        NVIC_Init(&NVIC_Initstrue);
}
//中断服务函数:如果是UART,如UART4,则注意是void UART4_IRQHandler(void)
void USART3_IRQHandler(void)
{
u8 res;

if(USART_GetITStatus(USART3,USART_IT_RXNE))
{
res=USART_ReceiveData(USART3);
USART_SendData(USART3,res);       
}
}
//main.c
typedef int u16_t;
#include "stm32f10x.h"
#include "usart.h"
int main(void)
{       
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
         My_USART_Init();
         LED_Init();
         while(1)
         { }         
}







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1