Stm32RS232串口通信——中断接收发送数据

main.c配置:

[cpp]  view plain copy print ?
  1. /* 
  2. *说明: 
  3. *PA0:KEY1;PA1:KEY2; 
  4. *PA2:LED1;PA3:LED2; 
  5. *PA9:USART1_TX;PA10:USART1_RX 
  6. */  
  7. #include "stm32f10x.h"  
  8. #include "stm32f10x_rcc.h"  
  9. #include "stm32f10x_gpio.h"  
  10. #include "stm32f10x_usart.h"  
  11. #include "stm32f10x_exti.h"  
  12. #include "system_stm32f10x.h"  
  13. #include "misc.h"  
  14.   
  15. void RCC_Configuration(void);  
  16. void GPIO_Configuration(void);   
  17. void USART_Configuration(void);  
  18. void NVIC_Configuration(void);  
  19.   
  20. int main()  
  21. {  
  22.     SystemInit();//72m  
  23.       
  24.     RCC_Configuration();  
  25.     GPIO_Configuration();   
  26.     USART_Configuration();  
  27.     NVIC_Configuration();  
  28.                                                               
  29.     while(1);  
  30. }  
  31.   
  32. void RCC_Configuration(void)    
  33. {     
  34.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);     
  35. }   
  36.   
  37. void GPIO_Configuration(void)    
  38. {    
  39.   GPIO_InitTypeDef GPIO_InitStructure;    
  40.     
  41.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  42.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;             //管脚9  
  43.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;       //复用推挽输出  
  44.   GPIO_Init(GPIOA, &GPIO_InitStructure);                //TX初始化  
  45.     
  46.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;            //管脚10  
  47.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入          
  48.   GPIO_Init(GPIOA, &GPIO_InitStructure);                //RX初始化  
  49.      
  50.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;    
  51.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  
  52.   GPIO_Init(GPIOA, &GPIO_InitStructure);   
  53. }   
  54.   
  55. void USART_Configuration(void)//串口初始化函数  
  56. {  
  57.     //串口参数初始化    
  58.     USART_InitTypeDef USART_InitStructure;               //串口设置恢复默认参数  
  59.     //初始化参数设置  
  60.     USART_InitStructure.USART_BaudRate = 9600;                  //波特率9600  
  61.     USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位  
  62.     USART_InitStructure.USART_StopBits = USART_StopBits_1;      //1位停止字节  
  63.     USART_InitStructure.USART_Parity = USART_Parity_No;         //无奇偶校验  
  64.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无流控制  
  65.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//打开Rx接收和Tx发送功能  
  66.     USART_Init(USART1,&USART_InitStructure);                   //初始化  
  67.       
  68.     USART_ITConfig(USART1,USART_IT_TXE,ENABLE);                //允许串口1发送中断。  
  69.     USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);               //允许串口1接收中断。  
  70.     USART_Cmd(USART1,ENABLE);                                  //启动串口  
  71.     USART_ClearFlag(USART1,USART_FLAG_TC);                     //发送完成标志位  
  72. }   
  73. void NVIC_Configuration(void)//配置中断优先级    
  74. {    
  75.   NVIC_InitTypeDef NVIC_InitStructure;    
  76.       
  77.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);    
  78.       
  79.   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;    
  80.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;    
  81.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;    
  82.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;    
  83.   NVIC_Init(&NVIC_InitStructure);    
  84. }    

stm32f10x_it.c的中断函数接收发送数据(接收和发送的时候改变LED状态):

[cpp]  view plain copy print ?
  1. #include "..\include\stm32f10x.h"   
  2. #include "..\include\stm32f10x_it.h"  
  3. #include "..\include\stm32f10x_gpio.h"  
  4. #include "..\include\stm32f10x_usart.h"  
  5.   
  6. void USART1_IRQHandler(void)  
  7. {  
  8.   u8 RX_dat;  
  9.     
  10.   if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)//USART_IT_RXNE:接收中断  
  11.   {   
  12.     USART_ClearITPendingBit(USART1,USART_IT_RXNE);  
  13.     RX_dat=USART_ReceiveData(USART1);      
  14.     while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==RESET){}  //接收数据寄存器非空标志位  
  15.     GPIO_WriteBit(GPIOA,GPIO_Pin_2,(BitAction)(Bit_SET-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_2)));  
  16.      
  17.     USART_SendData(USART1,RX_dat);     
  18.     while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET){} //发送数据寄存器空标志位  
  19.     GPIO_WriteBit(GPIOA,GPIO_Pin_3,(BitAction)(Bit_SET-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_3)));  
  20.   }     
  21. }  

你可能感兴趣的:(stm32)