嵌入式学习小组
直播中

王平

7年用户 1208经验值
私信 关注

ML51如何使用串口中断接收数据,如何在主函数读取串口中断接收的数据?

/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2019 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

//***********************************************************************************************************
//  Website: http://www.nuvoton.com
//  E-Mail : MicroC-8bit@nuvoton.com
//***********************************************************************************************************

//***********************************************************************************************************
//  File Function: ML51 simple GPIO toggle out demo code
//***********************************************************************************************************

#include "ML51.h"
bit uart2rvflag=0;
unsigned char TXtemp=0x30;

void SMC0_ISR(void) interrupt 19          // Vector @  0x9B
{
   SFRS=0;
   if((SC0IS&SET_BIT0)==SET_BIT0)
   {
      TXtemp = SC0DR;
      clr_SC0IS_RDAIF;
       uart2rvflag = 1;
   }
}

/**
* @brief       UART2 TXD output demo
* @param       None
* @return      None
* @Details     conned UART2 and UART0 to loop check.
*/

void main (void)
{
     MFP_P31_UART0_TXD;
    P31_QUASI_MODE;
    UART_Open(24000000,UART0_Timer1,115200);        /* Open UART0 use timer1 as baudrate generate and baud rate = 115200 */
  
     MFP_P55_UART2_RXD;
    P55_INPUT_MODE;
    UART_Open(24000000,UART2,115200);                /* Open UART2 use timer1 as baudrate generate and baud rate = 115200*/
    UART_Interrupt_Enable(UART2,Enable);
    ENABLE_GLOBAL_INTERRUPT;

/* Connect P5.5 and P3.1 to check loop  from UART0 to UART2*/
    UART_Send_Data(UART0,TXtemp);  
    while(1)
    {
      if (uart2rvflag)
      {
          TXtemp++;
          uart2rvflag=0;
          Timer3_Delay(24000000,4,300,1000);
          UART_Send_Data(UART0,TXtemp);  
      }
    }

}









回帖(1)

郭大

2024-1-16 17:46:27
sp;                      *                                                                        *//* Filename: main.c*//* Description: Main program*//* Author: Jason0539*//* Email: jason0539@163.com*//* Blog: http://www.jason0539.com*//* Version: V1.0*//* Copyright (c) 2016 Jason0539. All rights reserved. *---------------------------------------------------------------------------------------------------------*/#include "N76E003.h"#include "SFR_Macro.h"#include "Function_define.h"#include "Common_Include.h"#include "stdio.h"volatile uint8_t g_u8RecData;void UART_Data_ISR(void) interrupt 4{   if(RI){      RL_Buf[0] = SBUF;       clr_SCON_RI;   }   if(TI){      clr_SCON_TI;   }}void main(void){   /*Initial UART0 function*/   P06_PushPull_Mode;   P07_Input_Mode;   InitialUART0_Timer1(SYSTEM_CLK,9600);   /*Initial Interrupt Enable*/   ENABLE_GLOBAL_INTERRUPT;   ENABLE_UART0_INTERRUPT;   while(1){      if(RL_Buf[0]){         g_u8RecData = RL_Buf[0];       }   }}//The calculation of the UART baudrate (Fsys = 16MHz)://If SMOD = 0, Baudrate = (2^16) / [12 * (256-x)]//If SMOD = 1, Baudrate = (2^16) / [32 * (256-x)]//==> x = (256 * Fsys) / (32 * Baudrate), for the value of "x" is 49.9375, the register value is 49, so the baudrate calculates according to SMOD is 9600.
举报

更多回帖

发帖
×
20
完善资料,
赚取积分