[demo程序] NV32F101FL64E之串口中断收发数据代码

[复制链接]
 楼主| 詹求实 发表于 2020-6-27 10:47 | 显示全部楼层 |阅读模式
  1. #include "common.h"
  2. #include "rtc.h"
  3. #include "uart.h"
  4. #include "UART_app.h"
  5. #include "sysinit.h"


  6. #define SEND_BUF_LEN    50

  7. uint8_t send_buf[SEND_BUF_LEN]; //发送缓冲区
  8. volatile uint8_t u8IsSendDone; //定义发送完成标志位

  9. /******************************************************************************
  10. ******************************************************************************/

  11. void printf_int(int8* str)
  12. {
  13.         uint32 len = 0;
  14.    
  15.         u8IsSendDone = 0;                  
  16.    
  17.         while(*str)
  18.         {
  19.                 send_buf[len++] = *str;
  20.                 str++;
  21.                 if (len >= (SEND_BUF_LEN-1))
  22.                 {
  23.                         send_buf[SEND_BUF_LEN-1] = 0;
  24.                         break;
  25.                 }
  26.         }


  27.     UART_SendInt(UART1, send_buf, len);//UART1口串口发送初始化

  28. }
  29. /*
  30. 串口数据发送完成函数*/
  31. void UART_SendDone(void)//发送完成时,将标志位置位
  32. {
  33.     u8IsSendDone = 1;
  34. }

  35. int main (void)
  36. {
  37.     UART_ConfigType sConfig;
  38.    
  39.     /*执行系统初始化*/
  40.     sysinit();
  41.    
  42.     u8IsSendDone = 1;

  43.     sConfig.u32SysClkHz = BUS_CLK_HZ;//配置系统时钟和波特率
  44.     sConfig.u32Baudrate = UART_PRINT_BITRATE;
  45.    
  46.     UART_Init(UART1,&sConfig);  //初始化串口1
  47.     UART_SetTxDoneCallback(UART1, UART_SendDone);//串口中断回调函数
  48.     UART_SetCallback(UART_HandleInt);

  49.     LED0_Init();//初始化LED灯
  50.     printf("\nRunning the UART_Interrupt_demo project.\r\n");
  51.          
  52.           /* 打开串口1中断 */      
  53.     NVIC_EnableIRQ(UART1_IRQn);

  54.     printf_int("\nPrint characters using interrupt mode.\r\n");
  55.     while (!u8IsSendDone);         /* 等待发送完成 */
  56.    
  57.     printf_int("\nrepeat Print characters using interrupt mode.\r\n");
  58.     while (!u8IsSendDone);         /* 等待发送完成 */
  59.    
  60.     while (1);
  61. }



coshi 发表于 2020-7-6 17:26 | 显示全部楼层
非常感谢楼主分享
zljiu 发表于 2020-7-6 17:26 | 显示全部楼层
中断函数很简洁
wiba 发表于 2020-7-6 17:27 | 显示全部楼层
用的是等待?
tfqi 发表于 2020-7-6 17:27 | 显示全部楼层
楼主辛苦了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

13

主题

135

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部

13

主题

135

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部