⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新建 文本文档 (9).txt

📁 8051系列单片机可以使用的简易串口打印机驱动
💻 TXT
字号:
 用C编写51单片机的串口打印程序
悬赏分:50 - 解决时间:2008-2-27 22:54
哪位大虾给个示例程序供小弟参考参考!
提问者: 匿名
最佳答案
#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();
}
放在主函数体里就可以

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -