[demo程序] NV32F100 UART接收中断例程

[复制链接]
97|5
 楼主 | 2017-12-26 16:51 | 显示全部楼层 |阅读模式

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册 手机登录

x
 楼主 | 2017-12-26 16:58 | 显示全部楼层
  1. /**********NV32F100x--串口接收中断例程**********/

  2. #include "common.h"
  3. #include "ics.h"
  4. #include "etm.h"
  5. #include "uart.h"
  6. #include "sysinit.h"

  7. void UART_Rec(void);


  8. int main (void)
  9. {     
  10.     UART_ConfigType sConfig;
  11.     sysinit();//系统初始化

  12.           sConfig.u32SysClkHz = BUS_CLK_HZ;//时钟配置
  13.     sConfig.u32Baudrate = UART_PRINT_BITRATE;//波特率配置
  14.    
  15.           UART_SetCallback(UART_Rec);//设置串口中断回调入口
  16.     UART_Init(UART1,&sConfig);//初始化串口1
  17.        
  18.                 NVIC_EnableIRQ(UART1_IRQn);
  19.           printf("\nRunning the UART_Recint_demo project.\n");
  20.     UART_EnableRxBuffFullInt(UART1);//开启UART1接收中断

  21.         while(1)
  22.         {   
  23.         }
  24. }
  25. /********ISR*********/
  26. void UART_Rec(void)
  27. {  uint8_t fip;
  28.          if(UART1->S1 & UART_S1_RDRF_MASK)//判别接收是否已满
  29.          {
  30.                 fip = UART1->D;
  31.                 UART_PutChar(UART1,fip+1);//打印输入的字符+1后到UART1
  32.          }
  33. }



复制代码
 楼主 | 2017-12-26 16:59 | 显示全部楼层
  1. /******************************************************************************
  2. *
  3. * [url=home.php?mod=space&uid=247401]@brief[/url] providing common UART API.
  4. *
  5. ******************************************************************************/
  6. #include "UART_app.h"

  7. static uint8_t *pUART_TxBuff[MAX_UART_NO] = {NULL};           /* pointer to RxBuf */
  8. static uint8_t *pUART_RxBuff[MAX_UART_NO] = {NULL};           /* pointer to TxBuf */
  9. static uint16_t gu16UART_TxBuffPos[MAX_UART_NO] = {0};        /* write position to RxBuf */
  10. static uint16_t gu16UART_RxBuffPos[MAX_UART_NO] = {0};        /* read position to TxBuf */
  11. static uint32_t gu32UART_BuffSize[MAX_UART_NO] = {0};         /* buffer size*/

  12. UART_TxDoneCallbackType UART_TxDoneCallback[MAX_UART_NO] = {NULL};
  13. UART_RxDoneCallbackType UART_RxDoneCallback[MAX_UART_NO] = {NULL};
  14. /*****************************************************************************//*!
  15. *
  16. * @brief send a series of charecters using interrupt mode.
  17. *        
  18. * @param[in] pUART      base of UART port
  19. * @param[in] pSendBuff  pointer of charecters to send
  20. * @param[in] u32Length  number of charecters
  21. *
  22. * [url=home.php?mod=space&uid=266161]@return[/url]       none
  23. *
  24. * [url=home.php?mod=space&uid=72445]@[/url] Pass/ Fail criteria:
  25. *****************************************************************************/
  26. void UART_SendInt(UART_Type *pUART, uint8_t *pSendBuff, uint32_t u32Length)
  27. {
  28.     uint8_t u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
  29.    
  30.     /* save user defined send buffer pointers and size */
  31.     pUART_TxBuff[u8Port]        = pSendBuff;
  32.     gu32UART_BuffSize[u8Port]   = u32Length;
  33.     gu16UART_TxBuffPos[u8Port]  = 0;

  34.     UART_EnableTxBuffEmptyInt(pUART);   /* enable tx interrupt */
  35. }
  36. /*****************************************************************************//*!
  37. *
  38. * @brief receive a series of charecters using interrupt mode.
  39. *        
  40. * @param[in] pUART      base of UART port
  41. * @param[in] pSendBuff  pointer of charecters to send
  42. * @param[in] u32Length  number of charecters
  43. *
  44. * @return       none
  45. *
  46. * @ Pass/ Fail criteria:
  47. *****************************************************************************/
  48. void UART_ReceiveInt(UART_Type *pUART, uint8_t *pReceiveBuff, uint32_t u32Length)
  49. {
  50.     uint8_t u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
  51.    
  52.     /* save user defined read buffer pointers and size */
  53.     pUART_RxBuff[u8Port]        = pReceiveBuff;
  54.     gu32UART_BuffSize[u8Port]   = u32Length;
  55.     gu16UART_RxBuffPos[u8Port]  = 0;

  56.     UART_EnableRxBuffFullInt(pUART);   /* enable rx interrupt */
  57. }
  58. void UART_HandleInt(UART_Type *pUART)
  59. {
  60.     uint8_t   u8Port;
  61.     uint8_t   *pRdBuff;
  62.     uint8_t   *pWrBuff;  
  63.     volatile uint8_t read_temp = 0;
  64.    
  65.     u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
  66.    
  67.     /* check overrun flag */
  68.     if(UART_CheckFlag(pUART,UART_FlagOR))
  69.     {
  70.         read_temp = UART_ReadDataReg(pUART);     
  71.     }
  72.    
  73.     /* check receiver */
  74.     else if(UART_IsRxBuffFull(pUART))      
  75.     {
  76.         /* there's data received in rx buffer */
  77.         pRdBuff = pUART_RxBuff[u8Port];                     /* get user read buffer */
  78.         read_temp = UART_ReadDataReg(pUART);
  79.         pRdBuff[gu16UART_RxBuffPos[u8Port]++]= read_temp;   /* save received data */
  80.         //pRdBuff[gu16UART_RxBuffPos[u8Port]++] = UART_ReadDataReg(pUART);
  81.         if(gu16UART_RxBuffPos[u8Port] == gu32UART_BuffSize[u8Port])
  82.         {   
  83.             /* User read buffer is full, end receive */
  84.             UART_DisableRxBuffFullInt(pUART);
  85.             if (UART_RxDoneCallback[u8Port])
  86.                         {
  87.                             /* call back function to tell user that rx is done */
  88.                             UART_RxDoneCallback[u8Port]();
  89.                         }
  90.         }   
  91.     }
  92.     /* check transmitter */
  93.                         else if(UART_IsTxBuffEmpty(pUART))
  94.     {
  95.         if(gu16UART_TxBuffPos[u8Port] != gu32UART_BuffSize[u8Port])
  96.         {
  97.             /* user tx buffer not empty */
  98.             pWrBuff = pUART_TxBuff[u8Port];
  99.             UART_WriteDataReg(pUART, pWrBuff[gu16UART_TxBuffPos[u8Port]++]);     
  100.         }  
  101.         else
  102.         {
  103.                         UART_DisableTxBuffEmptyInt(pUART);
  104.                         if (UART_TxDoneCallback[u8Port])
  105.                         {
  106.                             /* call back function to tell user that tx is done */
  107.                             UART_TxDoneCallback[u8Port]();
  108.                         }
  109.         }
  110.     }
  111.     else
  112.     {
  113.         /* default interrupt */
  114.     }
  115. }

  116. void UART_SetTxDoneCallback(UART_Type *pUART, UART_TxDoneCallbackType pfnCallback)
  117. {
  118.     uint8_t u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
  119.     UART_TxDoneCallback[u8Port] = pfnCallback;
  120. }

  121. void UART_SetRxDoneCallback(UART_Type *pUART, UART_RxDoneCallbackType pfnCallback)
  122. {
  123.     uint8_t u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
  124.     UART_RxDoneCallback[u8Port] = pfnCallback;
  125. }
复制代码
 楼主 | 2017-12-26 16:59 | 显示全部楼层
  1. /******************************************************************************
  2. * File:    isr.h
  3. * Purpose: Define interrupt service routines referenced by the vector table.
  4. * Note: Only "vectors.c" should include this header file.
  5. ******************************************************************************/

  6. #ifndef __ISR_H
  7. #define __ISR_H


  8. /* Example */
  9. /*
  10. #undef  VECTOR_036
  11. #define VECTOR_036 RTC_Isr

  12. // ISR(s) are defined in your project directory.
  13. extern void RTC_Isr(void);
  14. */

  15. #undef  VECTOR_036
  16. #define VECTOR_036 RTC_Isr


  17. #undef  VECTOR_028
  18. #undef  VECTOR_029
  19. #undef  VECTOR_030
  20. #define VECTOR_028  UART0_Isr
  21. #define VECTOR_029  UART1_Isr
  22. #define VECTOR_030  UART2_Isr


  23. extern void RTC_Isr(void);
  24. extern void UART0_Isr(void);
  25. extern void UART1_Isr(void);
  26. extern void UART2_Isr(void);

  27. #endif  //__ISR_H

  28. /* End of "isr.h" */
复制代码
| 2017-12-29 16:51 | 显示全部楼层
好多操作寄存器的地方,没法用库函数吗?
| 2018-1-8 08:29 | 显示全部楼层
串口的发送和接受中断是分开的吗?可是在中断接口中只看到了一个中断入口。
扫描二维码,随时随地手机跟帖
您需要登录后才可以回帖 登录 | 注册 手机登录

本版积分规则

快速回复

您需要登录后才可以回帖
登录 | 注册 手机登录
高级模式

论坛热帖

分享 快速回复 返回顶部 返回列表