#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); 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); }