STM32的usart2串口调试

 先是参考 http://wenku.baidu.com/view/78f6b1350b4c2e3f572763e9.html调通了 usart1

然后将程序进行修改,对Usart2进行配置,配置完了之后,程序还是没有正确,然后在void GPIO_cfg();函数中添加一句 

GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);

最后程序如下,

#include "stm32f10x_lib.h"

 

FlagStatus RX_status;
FlagStatus Tx_status;

 

void RCC_cfg(void);

void GPIO_cfg(void);

void USART_cfg(void);

void NVIC_cfg(void);

unsigned char Rxbuf[20];

int index_send,index_rev;

u8 flag;

 

 

int main()

{

       int i;



       unsigned char TxBuf1[] = "Welcome to my STM32!I'm idiot!";

	   index_send = 0;
	   index_rev = 0;
	   flag = 0;

       RCC_cfg();

       GPIO_cfg();

       NVIC_cfg();

       USART_cfg();

 

       //清除标志位,否则第1位数据会丢失

       USART_ClearFlag(USART2,USART_FLAG_TC);

 

       //发送数据

       //PB5的作用是显示正在发送数据

       //当有数据在发送的时候,PB5会亮

       for( i=0;TxBuf1[i]!='\0';i++)

       {

              USART_SendData(USART2,TxBuf1[i]);

              GPIO_SetBits(GPIOB,GPIO_Pin_5);

              //等待数据发送完毕

              while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET);

              GPIO_ResetBits(GPIOB,GPIO_Pin_5);

       }

 
       while(1)
	   {
	       
	   }

 

}

 

 

//RCC时钟配置

void RCC_cfg()

{
      

       //将RCC寄存器重新设置为默认值

       RCC_DeInit();

         //打开GPIO时钟,复用功能,串口1的时钟

         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
		 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}

 

//IO口配置

void GPIO_cfg()

{

       GPIO_InitTypeDef GPIO_InitStructure;

 

       //PAD5作为US2的TX端,打开复用,负责发送数据

       GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
	   
	   GPIO_StructInit(&GPIO_InitStructure);

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

       GPIO_Init(GPIOD , &GPIO_InitStructure);

 

       //PD6作为US2的RX端,负责接收数据

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

       GPIO_Init(GPIOD, &GPIO_InitStructure);

 

       //LED显示串口正在发送/接收数据

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

       GPIO_Init(GPIOB, &GPIO_InitStructure);

}

 

//串口初始化

void USART_cfg()

{

       USART_InitTypeDef USART_InitStructure;

       //将结构体设置为缺省状态

       USART_StructInit(&USART_InitStructure);

    //波特率设置为115200

       USART_InitStructure.USART_BaudRate = 115200;

       //一帧数据的宽度设置为8bits

       USART_InitStructure.USART_WordLength = USART_WordLength_8b;

       //在帧结尾传输1个停止位

       USART_InitStructure.USART_StopBits = USART_StopBits_1;

       //奇偶失能模式,无奇偶校验

       USART_InitStructure.USART_Parity = USART_Parity_No;

       //发送/接收使能

       USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

       //硬件流控制失能

       USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

       //设置串口2

       USART_Init(USART2, &USART_InitStructure);

      

       //打开串口2的中断响应函数,接收中断

       USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

       //打开串口2

       USART_Cmd(USART2, ENABLE);

}

 

//配置中断

void NVIC_cfg()

{

        NVIC_InitTypeDef NVIC_InitStructure;

        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);                                               //选择中断分组2

        

        

        NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;                              //选择串口2中断

        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;                                 //抢占式中断优先级设置为0

        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;                                            //响应式中断优先级设置为0

        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                                                 //使能中断

        NVIC_Init(&NVIC_InitStructure);

}

 
void USART2_IRQHandler(void)

{
       GPIO_SetBits(GPIOB, GPIO_Pin_5);
       //确认是否接收到数据

       RX_status = USART_GetFlagStatus(USART2, USART_FLAG_RXNE);
      Tx_status = USART_GetFlagStatus(USART2, USART_FLAG_TC);

       //接收到数据
	  
      if(RX_status == SET)
	  {
              flag = 1;
	   //读出数据
	     Rxbuf[index_rev] = USART_ReceiveData(USART2);
	    index_rev = index_rev + 1;
	    if(index_rev == 20)
	       index_rev = 0;
	  }

}

以上程序,编译好,下载到芯片中,运行起来,在超级终端设置好波特率,位数等,可以看到Welcome to my STM32! I'm idiot!

你可能感兴趣的:(STM32的usart2串口调试)