打印
[demo程序]

NV32F100 UART接收中断例程

[复制链接]
1216|5
手机看帖
扫描二维码
随时随地手机跟帖
沙发
Puremr|  楼主 | 2017-12-26 16:58 | 只看该作者
/**********NV32F100x--串口接收中断例程**********/

#include "common.h"
#include "ics.h"
#include "etm.h"
#include "uart.h"
#include "sysinit.h"

void UART_Rec(void);


int main (void)
{     
    UART_ConfigType sConfig;
    sysinit();//系统初始化

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

        while(1)
        {   
        }
}
/********ISR*********/
void UART_Rec(void)
{  uint8_t fip;
         if(UART1->S1 & UART_S1_RDRF_MASK)//判别接收是否已满
         {
                fip = UART1->D;
                UART_PutChar(UART1,fip+1);//打印输入的字符+1后到UART1
         }
}



使用特权

评论回复
板凳
Puremr|  楼主 | 2017-12-26 16:59 | 只看该作者
/******************************************************************************
*
* [url=home.php?mod=space&uid=247401]@brief[/url] providing common UART API.
*
******************************************************************************/
#include "UART_app.h"

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

UART_TxDoneCallbackType UART_TxDoneCallback[MAX_UART_NO] = {NULL};
UART_RxDoneCallbackType UART_RxDoneCallback[MAX_UART_NO] = {NULL};
/*****************************************************************************//*!
*
* @brief send a series of charecters using interrupt mode.
*        
* @param[in] pUART      base of UART port
* @param[in] pSendBuff  pointer of charecters to send
* @param[in] u32Length  number of charecters
*
* [url=home.php?mod=space&uid=266161]@return[/url]       none
*
* [url=home.php?mod=space&uid=72445]@[/url] Pass/ Fail criteria:
*****************************************************************************/
void UART_SendInt(UART_Type *pUART, uint8_t *pSendBuff, uint32_t u32Length)
{
    uint8_t u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
   
    /* save user defined send buffer pointers and size */
    pUART_TxBuff[u8Port]        = pSendBuff;
    gu32UART_BuffSize[u8Port]   = u32Length;
    gu16UART_TxBuffPos[u8Port]  = 0;

    UART_EnableTxBuffEmptyInt(pUART);   /* enable tx interrupt */
}
/*****************************************************************************//*!
*
* @brief receive a series of charecters using interrupt mode.
*        
* @param[in] pUART      base of UART port
* @param[in] pSendBuff  pointer of charecters to send
* @param[in] u32Length  number of charecters
*
* @return       none
*
* @ Pass/ Fail criteria:
*****************************************************************************/
void UART_ReceiveInt(UART_Type *pUART, uint8_t *pReceiveBuff, uint32_t u32Length)
{
    uint8_t u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
   
    /* save user defined read buffer pointers and size */
    pUART_RxBuff[u8Port]        = pReceiveBuff;
    gu32UART_BuffSize[u8Port]   = u32Length;
    gu16UART_RxBuffPos[u8Port]  = 0;

    UART_EnableRxBuffFullInt(pUART);   /* enable rx interrupt */
}
void UART_HandleInt(UART_Type *pUART)
{
    uint8_t   u8Port;
    uint8_t   *pRdBuff;
    uint8_t   *pWrBuff;  
    volatile uint8_t read_temp = 0;
   
    u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
   
    /* check overrun flag */
    if(UART_CheckFlag(pUART,UART_FlagOR))
    {
        read_temp = UART_ReadDataReg(pUART);     
    }
   
    /* check receiver */
    else if(UART_IsRxBuffFull(pUART))      
    {
        /* there's data received in rx buffer */
        pRdBuff = pUART_RxBuff[u8Port];                     /* get user read buffer */
        read_temp = UART_ReadDataReg(pUART);
        pRdBuff[gu16UART_RxBuffPos[u8Port]++]= read_temp;   /* save received data */
        //pRdBuff[gu16UART_RxBuffPos[u8Port]++] = UART_ReadDataReg(pUART);
        if(gu16UART_RxBuffPos[u8Port] == gu32UART_BuffSize[u8Port])
        {   
            /* User read buffer is full, end receive */
            UART_DisableRxBuffFullInt(pUART);
            if (UART_RxDoneCallback[u8Port])
                        {
                            /* call back function to tell user that rx is done */
                            UART_RxDoneCallback[u8Port]();
                        }
        }   
    }
    /* check transmitter */
                        else if(UART_IsTxBuffEmpty(pUART))
    {
        if(gu16UART_TxBuffPos[u8Port] != gu32UART_BuffSize[u8Port])
        {
            /* user tx buffer not empty */
            pWrBuff = pUART_TxBuff[u8Port];
            UART_WriteDataReg(pUART, pWrBuff[gu16UART_TxBuffPos[u8Port]++]);     
        }  
        else
        {
                        UART_DisableTxBuffEmptyInt(pUART);
                        if (UART_TxDoneCallback[u8Port])
                        {
                            /* call back function to tell user that tx is done */
                            UART_TxDoneCallback[u8Port]();
                        }
        }
    }
    else
    {
        /* default interrupt */
    }
}

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

void UART_SetRxDoneCallback(UART_Type *pUART, UART_RxDoneCallbackType pfnCallback)
{
    uint8_t u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
    UART_RxDoneCallback[u8Port] = pfnCallback;
}

使用特权

评论回复
地板
Puremr|  楼主 | 2017-12-26 16:59 | 只看该作者
/******************************************************************************
* File:    isr.h
* Purpose: Define interrupt service routines referenced by the vector table.
* Note: Only "vectors.c" should include this header file.
******************************************************************************/

#ifndef __ISR_H
#define __ISR_H


/* Example */
/*
#undef  VECTOR_036
#define VECTOR_036 RTC_Isr

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

#undef  VECTOR_036
#define VECTOR_036 RTC_Isr


#undef  VECTOR_028
#undef  VECTOR_029
#undef  VECTOR_030
#define VECTOR_028  UART0_Isr
#define VECTOR_029  UART1_Isr
#define VECTOR_030  UART2_Isr


extern void RTC_Isr(void);
extern void UART0_Isr(void);
extern void UART1_Isr(void);
extern void UART2_Isr(void);

#endif  //__ISR_H

/* End of "isr.h" */

使用特权

评论回复
5
jerow| | 2017-12-29 16:51 | 只看该作者
好多操作寄存器的地方,没法用库函数吗?

使用特权

评论回复
6
xujunyi3611| | 2018-1-8 08:29 | 只看该作者
串口的发送和接受中断是分开的吗?可是在中断接口中只看到了一个中断入口。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

24

主题

215

帖子

1

粉丝