N76E003 双串口 uart0收发正常,uart1 发送异常 接收正常
附源代码 根据BSP 包例程改写,测试结果uart0 收发逻辑正常,uart1 接收正常,发送不正常没反应/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
//***********************************************************************************************************
//Nuvoton Technoledge Corp.
//Website: http://www.nuvoton.com
//E-Mail : MicroC-8bit@nuvoton.com
//Date : Apr/21/2017
//***********************************************************************************************************
//***********************************************************************************************************
//File Function: N76E003 UART-0 Mode1 demo code
//***********************************************************************************************************
#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#define BUFFER_SIZE16
UINT8UART_BUFFER,temp;
UINT16 u16CNT=0,u16CNT1=0;
bit riflag;
/******************************************************************************
* FUNCTION_PURPOSE: Serial interrupt, echo received data.
* FUNCTION_INPUTS : P0.7(RXD) serial input
* FUNCTION_OUTPUTS: P0.6(TXD) serial output
* Following setting in Common.c
******************************************************************************/
#if 0
//void InitialUART0_Timer1(UINT32 u32Baudrate) //T1M = 1, SMOD = 1
//{
//P06_Quasi_Mode;
//P07_Quasi_Mode;
//
// SCON = 0x52; //UART0 Mode1,REN=1,TI=1
// TMOD |= 0x20; //Timer1 Mode1
//
// set_SMOD; //UART0 Double Rate Enable
// set_T1M;
// clr_BRCK; //Serial port 0 baud rate clock source = Timer1
//
//#ifdef FOSC_160000
// TH1 = 256 - (1000000/u32Baudrate+1); /*16 MHz */
//#endif
// set_TR1;
//}
////---------------------------------------------------------------
//void InitialUART0_Timer3(UINT32 u32Baudrate) //use timer3 as Baudrate generator
//{
//P06_Quasi_Mode;
//P07_Quasi_Mode;
//
// SCON = 0x52; //UART0 Mode1,REN=1,TI=1
// set_SMOD; //UART0 Double Rate Enable
// T3CON &= 0xF8; //T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1)
// set_BRCK; //UART0 baud rate clock source = Timer3
//#ifdef FOSC_160000
// RH3 = HIBYTE(65536 - (1000000/u32Baudrate)-1); /*16 MHz */
//RL3 = LOBYTE(65536 - (1000000/u32Baudrate)-1); /*16 MHz */
//#endif
// set_TR3; //Trigger Timer3
//}
#endif
/*******************************************************************************
* FUNCTION_PURPOSE: Main function
******************************************************************************/
void main (void)
{
InitialUART1_Timer3(9600); //UART0 Baudrate initial,T1M=0,SMOD=0
InitialUART0_Timer1(9600); //UART0 Baudrate initial,T1M=0,SMOD=0
//IE = 0x90;
set_ES_1; //For interrupt enable
set_ES;
set_EA;
while(1)
{
if(UART_BUFFER == 0x56)
{
UART_BUFFER = 0;
Send_Data_To_UART1(0x56);
Send_Data_To_UART0(0x56);
}
if(UART_BUFFER == 0x57)
{
UART_BUFFER = 0;
Send_Data_To_UART1(0x57);
Send_Data_To_UART0(0x57);
}
if(UART_BUFFER == 0x58)
{
UART_BUFFER = 0;
Send_Data_To_UART1(0x58);
Send_Data_To_UART0(0x58);
}
if(UART_BUFFER == 0x59)
{
UART_BUFFER = 0;
Send_Data_To_UART1(0x59);
Send_Data_To_UART0(0x59);
}
}
}
void SerialPort0_ISR(void) interrupt 4
{
if (RI==1)
{ /* if reception occur */
clr_RI;
clr_TI;
/* clear reception flag for next reception */
UART_BUFFER = SBUF;
u16CNT ++;
riflag =1;
}
if(TI==1)
{
clr_TI; /* if emission occur */
}
}
void SerialPort1_ISR(void) interrupt 15
{
if (RI_1==1)
{ /* if reception occur */
clr_RI_1;
clr_TI_1;
/* clear reception flag for next reception */
UART_BUFFER = SBUF_1;
u16CNT ++;
riflag =1;
}
if(TI_1==1)
{
clr_TI_1; /* if emission occur */
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2016 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
//***********************************************************************************************************
//Nuvoton Technoledge Corp.
//Website: http://www.nuvoton.com
//E-Mail : MicroC-8bit@nuvoton.com
//Date : Apr/21/2016
//***********************************************************************************************************
#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"
//----------------------------------------------------------------------------------
// UART0 baud rate initial setting
//----------------------------------------------------------------------------------
void InitialUART0_Timer1(UINT32 u32Baudrate) //T1M = 1, SMOD = 1
{
P06_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
P07_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
SCON = 0x50; //UART0 Mode1,REN=1,TI=1
TMOD |= 0x20; //Timer1 Mode1
set_SMOD; //UART0 Double Rate Enable
set_T1M;
clr_BRCK; //Serial port 0 baud rate clock source = Timer1
#ifdef FOSC_160000
TH1 = 256 - (1000000/u32Baudrate+1); /*16 MHz */
#endif
#ifdef FOSC_166000
TH1 = 256 - (1037500/u32Baudrate); /*16.6 MHz */
#endif
set_TR1;
set_TI; //For printf function must setting TI = 1
}
//---------------------------------------------------------------
void InitialUART0_Timer3(UINT32 u32Baudrate) //use timer3 as Baudrate generator
{
P06_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
P07_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
SCON = 0x50; //UART0 Mode1,REN=1,TI=1
set_SMOD; //UART0 Double Rate Enable
T3CON &= 0xF8; //T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1)
set_BRCK; //UART0 baud rate clock source = Timer3
#ifdef FOSC_160000
RH3 = HIBYTE(65536 - (1000000/u32Baudrate)-1); /*16 MHz */
RL3 = LOBYTE(65536 - (1000000/u32Baudrate)-1); /*16 MHz */
#endif
#ifdef FOSC_166000
RH3 = HIBYTE(65536 - (1037500/u32Baudrate)); /*16.6 MHz */
RL3 = LOBYTE(65536 - (1037500/u32Baudrate)); /*16.6 MHz */
#endif
set_TR3; //Trigger Timer3
set_TI; //For printf function must setting TI = 1
}
UINT8 Receive_Data_From_UART0(void)
{
UINT8 c;
while (!RI);
c = SBUF;
RI = 0;
return (c);
}
void Send_Data_To_UART0 (UINT8 c)
{
TI = 0;
SBUF = c;
while(TI==0);
}
//----------------------------------------------------------------------------------
// UART1 baud rate initial setting
//----------------------------------------------------------------------------------
void InitialUART1_Timer3(UINT32 u32Baudrate) //use timer3 as Baudrate generator
{
P02_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
P16_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
SCON_1 = 0x50; //UART1 Mode1,REN_1=1,TI_1=1
T3CON = 0x08; //T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1), UART1 in MODE 1
clr_BRCK;
#ifdef FOSC_160000
RH3 = HIBYTE(65536 - (1000000/u32Baudrate)-1); /*16 MHz */
RL3 = LOBYTE(65536 - (1000000/u32Baudrate)-1); /*16 MHz */
#endif
#ifdef FOSC_166000
RH3 = HIBYTE(65536 - (1037500/u32Baudrate)); /*16.6 MHz */
RL3 = LOBYTE(65536 - (1037500/u32Baudrate)); /*16.6 MHz */
#endif
set_TR3; //Trigger Timer3
}
UINT8 Receive_Data_From_UART1(void)
{
UINT8 c;
while (!RI_1);
c = SBUF_1;
RI_1 = 0;
return (c);
}
void Send_Data_To_UART1 (UINT8 c)
{
TI_1 = 0;
SBUF_1 = c;
while(TI_1==0);
}
/*==========================================================================*/
#ifdef SW_Reset
void SW_Reset(void)
{
TA = 0xAA;
TA = 0x55;
set_SWRST;
}
#endif
/*==========================================================================*/
N76E003 因为UART1 是搭在ICE pin上,所以要断开ICE jasontu 发表于 2022-11-15 12:04
N76E003 因为UART1 是搭在ICE pin上,所以要断开ICE
是拔掉nulink测试的,应该不是 这个问题 init UART0再init UART1,T3CON会被修改到
timer 3在uart 1被修改到, 要用 or 才是
T3CON |= 0x08; //T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1), UART1 in MODE 1
clr_BRCK; jasontu 发表于 2022-11-17 08:14
init UART0再init UART1,T3CON会被修改到
timer 3在uart 1被修改到, 要用 or 才是
T3CON |= 0x08; ...
谢谢 晚上回家试试非常感谢 发送异常,是不是波特率的问题呢?
可以用 隔离器 干扰把波特率调低 通讯老是不稳定 是干扰问题吗? 串口之间存在干扰了吧。 之前遇到过,可能串口的频率太高了。
页:
[1]