打印
[技术问答]

M051串口0和串口1求解

[复制链接]
1095|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
yexinyuan1212|  楼主 | 2017-12-22 21:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "UART.h"
/****************************************
*函数名称:UartInit
*输    入:unFosc   晶振频率
          unBaud  波特率
*输    出:无
*功    能:串口初始化
******************************************/
VOID UartInit(UINT32 unFosc,UINT32 unBaud)
{
    P3_MFP &= ~(P31_TXD0 | P30_RXD0);  
    P3_MFP |= (TXD0 | RXD0);      //P3.0 使能为串口0接收
          //P3.1 使能为串口0发送
    UART0_Clock_EN;         //串口0时钟使能
    UARTClkSource_ex12MHZ;  //串口时钟选择为外部晶振
    CLKDIV &= ~(15<<8);  //串口时钟分频为0
    IPRSTC2 |= UART0_RST;   //复位串口0
    IPRSTC2 &= ~UART0_RST;  //复位结束
    UA0_FCR |= TX_RST;      //发送FIFO复位
    UA0_FCR |= RX_RST;      //接收FIFO复位
    UA0_LCR &= ~PBE;      //校验位功能取消
UA0_LCR &= ~WLS;
    UA0_LCR |= WL_8BIT;     //8位数据位
    UA0_LCR &= NSB_ONE;     //1位停止位
    UA0_BAUD |= DIV_X_EN|DIV_X_ONE;   //设置波特率分频   
    UA0_BAUD |= ((unFosc / unBaud) -2); //波特率设置  UART_CLK/(A+2) = 115200, UART_CLK=12MHz
UA0_IER |= RDA_IEN;     //接收数据中断使能
NVIC_ISER |= UART0_INT;     //使能串口0中断
}
/****************************************
*函数名称:UartSend
*输    入:pBuf     发送数据缓冲区
          unNumOfBytes  发送字节总数
*输    出:无
*功    能:串口发送数据
******************************************/
VOID UartSend(UINT8 *pBuf,UINT32 unNumOfBytes)
{
  UINT32 i;
  for(i=0; i<unNumOfBytes; i++)
  {
         UA0_THR = *(pBuf+i);
         while ((UA0_FSR&TX_EMPTY) != 0x00); //检查发送FIFO是否为空
  }
}
/****************************************
*函数名称:UART0_IRQHandler
*输    入:无
*输    出:无
*功    能:串口0中断服务函数
******************************************/
VOID UART0_IRQHandler(VOID)
{
    UINT8 ucData;
if(UA0_ISR & RDA_INT)     //检查是否接收数据中断
{  
  while(UA0_ISR & RDA_IF)    //获取所有接收到的数据
  {
   while (UA0_FSR & RX_EMPTY);  //检查接收FIFO是否为空   
   ucData = UA0_RBR;    //读取数据
   UartSend(&ucData,1);   //发送数据
   //以下测试接收数据用
    if(ucData == 0x11)  P2_DOUT &= ~0xff;
    if(ucData == 0xff)  P2_DOUT |= 0xff;
  }
}
}
/****************************************
*函数名称:main
*输    入:无
*输    出:无
*功    能:函数主体
******************************************/
INT32 main(VOID)
{
// unsigned char i,j;
     PROTECT_REG
  (            //ISP下载时保护FLASH存储器
   PWRCON |= XTL12M_EN;      //默认时钟源为外部晶振
   while((CLKSTATUS & XTL12M_STB) == 0);   //等待12MHz时钟稳定
   CLKSEL0 = (CLKSEL0 & (~HCLK)) | HCLK_12M; //设置外部晶振为系统时钟
   //以下测试接收数据
   //P2_PMD=0x5555;          //GPIO设置为输出模式  
  )
  UartInit(12000000,9600);      //波特率设置为9600bps
  while(1)
  {
/*   j=0;
                //发送数据0~255
    for(i=0; i<256; i++)
    {
       UartSend(&j,1);
     j++;
     Delayms(50);   
    } */
  }
}

用以上程序能进串口0,已下是改成串口1的配置为什么进不了串口2??请各位大哥看看??

void UART1_Init(void)
{

        CLK->APBCLK   |=  CLK_UART1_EN ;           
        CLK->CLKSEL1   =  CLKS1_UART_EXT_0 ;
        CLK->CLKDIV   &= ~(15<<8) ;                
        GCR->IPRSTC2  |=  RST_UART1 ;        
        GCR->IPRSTC2  &=  ~ RST_UART1 ;               
        UART1->FCR |= TX_RESET ;                   
        UART1->FCR |= RX_RESET ;            
        UART1->LCR    = WORD_LEN_8BIT ;      
       UART1->BAUD   |= DIV_X_EN | DIV_X_ONE ;        
       UART1->BAUD   |= ((unFosc1 / Baud1) -2);   
       UART1->IER    = 1 ;                          
       NVIC_EnableIRQ(UART1_IRQn) ;
       
}

void UART1_IRQHandler(void)                              
{
        uint8_t ucData_1 ;   
        //P46 = 1;       
        if(UART1->ISR & RDA_INT)             
        {
         while(UART1->ISR & RX_DATA_IF)      
         {
         while (UART1->FSR & RX_FIFO_EMPTY);  
        ucData_1 = UART1->RBR;              
        UART1->THR = ucData_1;
         UartSend1(&ucData_1,1);            
         if(ucData_1 == 0x11)  P46 = 1;                 
         }

        }               
}       

为什么这个程序进不了串口1的中断函数。请指教。

沙发
天灵灵地灵灵| | 2017-12-22 22:24 | 只看该作者
可以查看相关头文件,看看关于串口1的是怎么样的。

使用特权

评论回复
板凳
xinxianshi| | 2017-12-26 19:36 | 只看该作者
是不是一直没收到消息。

使用特权

评论回复
地板
734774645| | 2017-12-27 10:45 | 只看该作者
/****************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url]     main.c
* [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
* $Revision: 7 $
* $Date: 15/05/22 3:53p $
* [url=home.php?mod=space&uid=247401]@brief[/url]    Transmit and receive data from PC terminal through RS232 interface.
*
* @note
* Copyright (C) 2011 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include <stdio.h>
#include "M051Series.h"


#define PLL_CLOCK           50000000

#define RXBUFSIZE 1024

/*---------------------------------------------------------------------------------------------------------*/
/* Global variables                                                                                        */
/*---------------------------------------------------------------------------------------------------------*/
uint8_t g_u8RecData[RXBUFSIZE]  = {0};

volatile uint32_t g_u32comRbytes = 0;
volatile uint32_t g_u32comRhead  = 0;
volatile uint32_t g_u32comRtail  = 0;
volatile int32_t g_bWait         = TRUE;

/*---------------------------------------------------------------------------------------------------------*/
/* Define functions prototype                                                                              */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void);
void UART_TEST_HANDLE(void);
void UART_FunctionTest(void);


void SYS_Init(void)
{
    /*---------------------------------------------------------------------------------------------------------*/
    /* Init System Clock                                                                                       */
    /*---------------------------------------------------------------------------------------------------------*/

    /* Enable Internal RC 22.1184MHz clock */
    CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

    /* Waiting for Internal RC clock ready */
    CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

    /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
    CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

    /* Enable external XTAL 12MHz clock */
    CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

    /* Waiting for external XTAL clock ready */
    CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

    /* Set core clock as PLL_CLOCK from PLL */
    CLK_SetCoreClock(PLL_CLOCK);

    /* Enable UART module clock */
    CLK_EnableModuleClock(UART0_MODULE);

    /* Select UART module clock source */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

    /*---------------------------------------------------------------------------------------------------------*/
    /* Init I/O Multi-function                                                                                 */
    /*---------------------------------------------------------------------------------------------------------*/

    /* Set P3 multi-function pins for UART0 RXD and TXD */
    SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
    SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0);

}

void UART0_Init()
{
    /*---------------------------------------------------------------------------------------------------------*/
    /* Init UART                                                                                               */
    /*---------------------------------------------------------------------------------------------------------*/
    /* Reset UART0 */
    SYS_ResetModule(UART0_RST);

    /* Configure UART0 and set UART0 Baudrate */
    UART_Open(UART0, 115200);
}

/*---------------------------------------------------------------------------------------------------------*/
/* UART Test Sample                                                                                        */
/* Test Item                                                                                               */
/* It sends the received data to HyperTerminal.                                                            */
/*---------------------------------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function                                                                                           */
/*---------------------------------------------------------------------------------------------------------*/

int main(void)
{
    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Init System, peripheral clock and multi-function I/O */
    SYS_Init();

    /* Lock protected registers */
    SYS_LockReg();

    /* Init UART0 for printf and testing */
    UART0_Init();

    /*---------------------------------------------------------------------------------------------------------*/
    /* SAMPLE CODE                                                                                             */
    /*---------------------------------------------------------------------------------------------------------*/

    printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);

    printf("\n\nUART Sample Program\n");

    /* UART sample function */
    UART_FunctionTest();
   
    while(1);

}

/*---------------------------------------------------------------------------------------------------------*/
/* ISR to handle UART Channel 0 interrupt event                                                            */
/*---------------------------------------------------------------------------------------------------------*/
void UART0_IRQHandler(void)
{
    UART_TEST_HANDLE();
}

/*---------------------------------------------------------------------------------------------------------*/
/* UART Callback function                                                                                  */
/*---------------------------------------------------------------------------------------------------------*/
void UART_TEST_HANDLE()
{
    uint8_t u8InChar = 0xFF;
    uint32_t u32IntSts = UART0->ISR;

    if(u32IntSts & UART_ISR_RDA_INT_Msk)
    {
        printf("\nInput:");

        /* Get all the input characters */
        while(UART_IS_RX_READY(UART0))
        {
            /* Get the character from UART Buffer */
            u8InChar = UART_READ(UART0);

            printf("%c ", u8InChar);

            if(u8InChar == '0')
            {
                g_bWait = FALSE;
            }

            /* Check if buffer full */
            if(g_u32comRbytes < RXBUFSIZE)
            {
                /* Enqueue the character */
                g_u8RecData[g_u32comRtail] = u8InChar;
                g_u32comRtail = (g_u32comRtail == (RXBUFSIZE - 1)) ? 0 : (g_u32comRtail + 1);
                g_u32comRbytes++;
            }
        }
        printf("\nTransmission Test:");
    }

    if(u32IntSts & UART_ISR_THRE_INT_Msk)
    {
        uint16_t tmp;
        tmp = g_u32comRtail;
        if(g_u32comRhead != tmp)
        {
            u8InChar = g_u8RecData[g_u32comRhead];
            while(UART_IS_TX_FULL(UART0));  /* Wait Tx is not full to transmit data */            
            UART_WRITE(UART0, u8InChar);
            g_u32comRhead = (g_u32comRhead == (RXBUFSIZE - 1)) ? 0 : (g_u32comRhead + 1);
            g_u32comRbytes--;
        }
    }
}


/*---------------------------------------------------------------------------------------------------------*/
/*  UART Function Test                                                                                     */
/*---------------------------------------------------------------------------------------------------------*/
void UART_FunctionTest()
{
    printf("+-----------------------------------------------------------+\n");
    printf("|  UART Function Test                                       |\n");
    printf("+-----------------------------------------------------------+\n");
    printf("|  Description :                                            |\n");
    printf("|    The sample code will print input char on terminal      |\n");
    printf("|    Please enter any to start     (Press '0' to exit)      |\n");
    printf("+-----------------------------------------------------------+\n");

    /*
        Using a RS232 cable to connect UART0 and PC.
        UART0 is set to debug port. UART0 is enable RDA and RLS interrupt.
        When inputing char to terminal screen, RDA interrupt will happen and
        UART0 will print the received char on screen.
    */

    /* Enable Interrupt and install the call back function */
    UART_ENABLE_INT(UART0, (UART_IER_RDA_IEN_Msk | UART_IER_THRE_IEN_Msk | UART_IER_RTO_IEN_Msk));
    NVIC_EnableIRQ(UART0_IRQn);
    while(g_bWait);

    /* Disable Interrupt */
    UART_DISABLE_INT(UART0, (UART_IER_RDA_IEN_Msk | UART_IER_THRE_IEN_Msk | UART_IER_RTO_IEN_Msk));
    NVIC_DisableIRQ(UART0_IRQn);
    g_bWait = TRUE;
    printf("\nUART Sample Demo End.\n");

}

使用特权

评论回复
5
734774645| | 2017-12-27 10:45 | 只看该作者
按照这个,修改一下,就可以用两个接口了。

使用特权

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

本版积分规则

5

主题

8

帖子

0

粉丝