到STM32上测试- static stRingBuff g_stRingBuffer = {0,0,0};
- static u8 g_recvFinshFlag = 0;
- stRingBuff *GetRingBufferStruct(void)
- {
- return &g_stRingBuffer;
- }
- u8 *IsUsart1RecvFinsh(void)
- {
- return &g_recvFinshFlag;
- }
- void USART1_IRQHandler(void) //串口1中断服务程序
- {
- u8 res;
- if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
- {
- res = USART_ReceiveData(USART1); //读取接收到的数据
- WriteOneByteToRingBuffer(GetRingBufferStruct(),res);
- }
- if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) //空闲中断
- {
- USART_ReceiveData(USART1); //清除空闲中断
- g_recvFinshFlag = 1; //接收完成
- }
- }
主函数:
- int main(void)
- {
- char readBuffer[100];
- u16 t;
- u16 len;
- u16 times = 0;
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- uart_init(115200); //串口初始化为115200
- LED_Init(); //LED端口初始化
- KEY_Init(); //初始化与按键连接的硬件接口
-
- while(1)
- {
- times++;
- if(*IsUsart1RecvFinsh())
- {
- ReadRingBuffer(GetRingBufferStruct(),readBuffer,GetRingBufferLength(GetRingBufferStruct()));
- printf("%s",readBuffer);
- memset(readBuffer,0,100);
- *IsUsart1RecvFinsh() = 0;
- }
- if(times%500==0)
- LED0=!LED0;
- delay_ms(1);
- }
- }
|