Chinaunix首页 | 论坛 | 博客
  • 博客访问: 805398
  • 博文数量: 281
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2770
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-02 19:45
个人简介

邮箱:zhuimengcanyang@163.com 痴爱嵌入式技术的蜗牛

文章分类
文章存档

2020年(1)

2018年(1)

2017年(56)

2016年(72)

2015年(151)

分类: 嵌入式

2015-12-11 15:16:32

利用FIFO存储串口接收到的字符信息。

FIFO如何构造?
1. 构造一个FIFO结构体: static str_rbuf rec_buf;
2. 构造一个真正的FIFO缓冲区:static uint8_t receive_buffer[100];
3. 初始化,关联FIFO结构体和缓冲区
rbuf_init(&rec_buf, receive_buffer, 1, 100);
4. 在串口接收中断函数里,将接收到的数据压入到FIFO缓冲区中,并产生串口接收到数据的事件。
    在主程序中不断查询事件的发生。如果串口接收事件发生,则执行该事件。

具体代码:

点击(此处)折叠或打开

  1. uint8_t receivedChar;
  2. static void OnCharRecEvent(void* data);
  3. static event_t charRecEvent = EVENT_INIT(OnCharRecEvent);

  4. // ---------------------------------------------------------------------
  5. static str_rbuf rec_buf;
  6. static uint8_t receive_buffer[100];

  7. // 初始化FIFO结构体和缓冲区
  8. void uart_ringBuffer_Init(void)
  9. {
  10.     rbuf_init(&rec_buf, receive_buffer, 1, 100);
  11. }



  12. //------------------------------------------------------------------------
  13. void USART1_IRQHandler(void)
  14. {
  15.     uint8_t tmp_remove = 0;
  16.     
  17.     if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  18.     {
  19.         receivedChar = USART_ReceiveData(USART1);

  20.         if ( rbuf_nr_of_items ( &rec_buf ) == rec_buf.max_entries)
  21.         {
  22.             /*如果buffer已满,并且此时受到数据,则从当前读指针读出一个数据,抛弃掉。*/
  23.             rbuf_pull ( &rec_buf, &tmp_remove );
  24.         }

  25.         /* 将新的数据加入到buffer中,注意:这里当FIFO为满的时候,新数据还是可以写进去,只是把以前老的数据给覆盖了。 */
  26.         rbuf_push ( &rec_buf, (uint8_t *)&receivedChar );
  27.         
  28.         
  29.         // 添加事件,通知主程序,发生中断接收事件。
  30.         // 进行异步处理。
  31.         EventQueue_Enqueue_ISR(&charRecEvent); /* 中断事件发生 */
  32.     }
  33. }


  34. /* 接收中断事件处理函数 */
  35. static void OnCharRecEvent(void* data)
  36. {
  37.     static uint8_t a_tmp = 0;
  38.    
  39.     //将FIFO缓冲区中的数据读完为止
  40.     while (rbuf_nr_of_items ( &rec_buf ) != 0)
  41.     {
  42.         rbuf_pull(&rec_buf, &a_tmp);
  43.         uart_sendChar(a_tmp);
  44.         
  45.         DI_CallbackISR_ByteReceived(a_tmp);
  46.     }
  47. }


  48. void DI_CallbackISR_ByteReceived(uint8_t rxByte)
  49. {
  50.     // state machine here.
  51. }


主函数只需添加初始化FIFO的函数,就OK了。

点击(此处)折叠或打开

  1. int main(void)
  2. {    
  3.     volatile static event_t* event;
  4.     board_init();
  5.     keyScanTask_init();
  6.     uart_ringBuffer_Init();
  7.     
  8.     while(1)
  9.     {
  10.         event = EventQueue_GetPendingEvent();
  11.         if (event)
  12.         {
  13.             event->execute(event->data);
  14.         }
  15.     }
  16.  }



阅读(1668) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~