2个回答
展开全部
#include "Include/c8051f120.h" // SFR declarations
#include "Include/system.h" // SFR declarations
void UART0_Init (void)
{
unsigned char T1_PRESCALE;
unsigned char T1_RELOAD;
unsigned long BAUD;
BAUD = 38400;
if ((SYSCLK/BAUD/32/1)<256)
{
T1_PRESCALE = 0x10;
T1_RELOAD = -((SYSCLK/BAUD/16/1+1)/2);
}
else if ((SYSCLK/BAUD/32/4)<256)
{
T1_PRESCALE = 0x01;
T1_RELOAD = -((SYSCLK/BAUD/16/4+1)/2);
}
else if ((SYSCLK/BAUD/32/12)<256)
{
T1_PRESCALE = 0x00;
T1_RELOAD = -((SYSCLK/BAUD/16/12+1)/2);
}
else if ((SYSCLK/BAUD/32/48)<256)
{
T1_PRESCALE = 0x02;
T1_RELOAD = -((SYSCLK/BAUD/16/48+1)/2);
}
SFRPAGE = UART0_PAGE;
SCON0 = 0x50; // SCON0: 8-bit variable bit rate
// RX enabled
// SCON0 |= 0xd0; // SCON0: 8-bit variable bit rate
SFRPAGE = TIMER01_PAGE;
CKCON &= ~0x1B; // clear T1M, SCA1, SCA0
CKCON |= T1_PRESCALE; // set T1M, SCA1, SCA0 according to BAUD
TH1 = T1_RELOAD; // reload value calculated from BAUD
TL1 = T1_RELOAD; // also load into TL1
TMOD &= ~0xf0; // clear T1 bits in TMOD
TMOD |= 0x20; // set TMOD for 8 bit reload
TR1 = 1; // START Timer1
//added by edward to test the priority settings of UART0. If this line is here, means good.
IP |= 0x10;
SFRPAGE = UART0_PAGE;
TI0 = 1; // Indicate TX0 ready
ES0 = 1;
}
void UartRun(void)
{
unsigned char Receive_userData;
if (RI0)
{
Receive_userData = SBUF0;
UART0_SEND(Receive_userData);
RI0 = 0;
}
if (TI0)
{
TI0 = 0;
}
}
void UART0_ISR (void) interrupt 4
{
_push_(SFRPAGE);
SFRPAGE = UART0_PAGE;
UartRun();
_pop_(SFRPAGE);
}
这个是串口输入什么就显示什么的例子,如果需要打印的话你可以加入打印的函数print();
{UartRun();
}
放在主函数体里就可以
#include "Include/system.h" // SFR declarations
void UART0_Init (void)
{
unsigned char T1_PRESCALE;
unsigned char T1_RELOAD;
unsigned long BAUD;
BAUD = 38400;
if ((SYSCLK/BAUD/32/1)<256)
{
T1_PRESCALE = 0x10;
T1_RELOAD = -((SYSCLK/BAUD/16/1+1)/2);
}
else if ((SYSCLK/BAUD/32/4)<256)
{
T1_PRESCALE = 0x01;
T1_RELOAD = -((SYSCLK/BAUD/16/4+1)/2);
}
else if ((SYSCLK/BAUD/32/12)<256)
{
T1_PRESCALE = 0x00;
T1_RELOAD = -((SYSCLK/BAUD/16/12+1)/2);
}
else if ((SYSCLK/BAUD/32/48)<256)
{
T1_PRESCALE = 0x02;
T1_RELOAD = -((SYSCLK/BAUD/16/48+1)/2);
}
SFRPAGE = UART0_PAGE;
SCON0 = 0x50; // SCON0: 8-bit variable bit rate
// RX enabled
// SCON0 |= 0xd0; // SCON0: 8-bit variable bit rate
SFRPAGE = TIMER01_PAGE;
CKCON &= ~0x1B; // clear T1M, SCA1, SCA0
CKCON |= T1_PRESCALE; // set T1M, SCA1, SCA0 according to BAUD
TH1 = T1_RELOAD; // reload value calculated from BAUD
TL1 = T1_RELOAD; // also load into TL1
TMOD &= ~0xf0; // clear T1 bits in TMOD
TMOD |= 0x20; // set TMOD for 8 bit reload
TR1 = 1; // START Timer1
//added by edward to test the priority settings of UART0. If this line is here, means good.
IP |= 0x10;
SFRPAGE = UART0_PAGE;
TI0 = 1; // Indicate TX0 ready
ES0 = 1;
}
void UartRun(void)
{
unsigned char Receive_userData;
if (RI0)
{
Receive_userData = SBUF0;
UART0_SEND(Receive_userData);
RI0 = 0;
}
if (TI0)
{
TI0 = 0;
}
}
void UART0_ISR (void) interrupt 4
{
_push_(SFRPAGE);
SFRPAGE = UART0_PAGE;
UartRun();
_pop_(SFRPAGE);
}
这个是串口输入什么就显示什么的例子,如果需要打印的话你可以加入打印的函数print();
{UartRun();
}
放在主函数体里就可以
北京合创芯彩科技
2024-08-22 广告
2024-08-22 广告
在线打印公司有很多,其中推荐如下:* 易绘创校园打印站。该小程序提供自助操作,用户可以自己上传文件,系统会自动计算价格,下单后即可开始打印。其打印价格便宜,黑白A4纸仅需5分钱单面,7分钱/张。此外,该小程序还提供当日发货服务,售后态度也很...
点击进入详情页
本回答由北京合创芯彩科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询