51单⽚机使⽤串⼝printf
#define BIT(x) ((unsigned int)((unsigned int)1<<x))
串⼝初始化
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
void Uart0_Init(unsigned int baud)
{
unsigned long tmp;
#if 0
P2CR &= ~BIT(0); //Input Mode
P2PCR |= BIT(0); //Enable pull-up resistor
P2 |= BIT(0);
P2CR |= BIT(1); //Output Mode
P2PCR &= ~(BIT(1)); //Disable pull-up resistor
P2 |= BIT(1);
#endif
/* Config Uart0:Mode 1, 8-bits asynchronous. */
SCON = 0x50;
PCON |=0x80; //Double baud rate
tmp = 16000000/16/baud; //16000000UL*2/2/16/baud;
tmp = 65536 - tmp;
RCLK = 0; //T4 create rxd baud rate
TCLK = 0; //T4 create txd baud rate
_push_(INSCON);
Select_Bank1();
T4CLKS = 0; //T4 Clock select = Sysclk = 16MHz;
T4PS1 = 0; //T4 Prescaler = 1/1;
T4PS0 = 0;
T4M1 = 0; //T4 mode select bits = 01, T4 as Baud rate generator.
T4M0 = 1;
TL4 = (unsigned char)tmp;
TH4 = (unsigned char)(tmp>>8);模拟串口使用printf函数
TR4 = 1;
Select_Bank0();
_pop_(INSCON);
REN = 1; //Enable Receive
//ES0 = 1; //Enable Uart0 Interrupt
//TI = 1; //printf need
}
printf重映射
/*----------------------------------------------------------------------------*/
//printf call
/*----------------------------------------------------------------------------*/
char putchar(char ch)
{
//ES=0; //Disable Uart0 Interrupt
TI = 0;
SBUF = (unsigned char)ch;
while (!TI);
TI = 0;
//ES=1; //Enable Uart0 Interrupt
return (ch);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论