//------------------------------------------------------------------------------------ // serial.c //----------------------------------------------------------------------------------- //------------------------------------------------------------------------------------ // Includes //------------------------------------------------------------------------------------ #include // SFR declarations #include // for printf and _getkey functions //------------------------------------------------------------------------------------ // Global CONSTANTS //------------------------------------------------------------------------------------ #define SYSCLK 22118400 #define BAUDRATE 9600 #define bool bit #define true 1 #define false 0 #define null 0 //------------------------------------------------------------------------------------ // Function PROTOTYPES //------------------------------------------------------------------------------------ void SYSCLK_init(void); void cross_bar_init(void); void UART0_init(void); //------------------------------------------------------------------------------------ // MAIN Routine //------------------------------------------------------------------------------------ void main (void) { unsigned char ch; WDTCN = 0xDE; // disable watchdog timer WDTCN = 0xAD; // initialize the system clock SYSCLK_init(); // initialize the 8051 crossbar cross_bar_init(); // initialize UART 0 for 9600 baud UART0_init(); // send a message from UART0 while (1) { printf("\n Type a key: "); ch = _getkey(); printf("\n You typed: %c", ch); } } //------------------------------------------------------------------------------------ // SYSCLK_init // //------------------------------------------------------------------------------------ void SYSCLK_init(void) { int i; OSCXCN = 0x67; // start external oscillastor with 22.1184 MHz crystal for (i = 0; i < 256; i++) ; // wait for oscillator to start while (! (OSCXCN & 0x80)) ; // wait for crystal osc. to settle OSCICN = 0x88; // select external oscillator as SYSCLK source // and enable missing clock detector } //------------------------------------------------------------------------------------ // cross_bar_init // //------------------------------------------------------------------------------------ void cross_bar_init(void) { XBR0 = 0x04; XBR2 = 0x40; P0MDOUT = 0x01; // output configuration for port P0 } //------------------------------------------------------------------------------------ // UART0_init // //------------------------------------------------------------------------------------ void UART0_init(void) { SCON0 = 0x50; // SCON0: mode 1 8-bit UART, enable RX TMOD = 0x20; // TMOD: TH1 = -(SYSCLK/BAUDRATE/16); // set Timer 1 reload value for correct baud rate TR1 = 1; // start timer 1 CKCON |= 0x10; // timer 1 uses SYSCLK as time base PCON |= 0x80; // SMOD0 = 1 disable baud rate divide by 2 TI0 = 1; // set transmit TX0 to ready }