00001 /************************************************** 00002 * Uart driver 00003 * Function: Driver for 8051 uart interface 00004 * 00005 * File: uart.c 00006 * Author: Book Chen 00007 * 2008.08.08 00008 **************************************************** 00009 */ 00010 #include "..\inc\includes.h" 00011 00012 struct UART_CONTROL UartCtrl; 00013 bit bUartTransfer; 00014 00015 void F_UartPowerOnInit(void) LargeReentrant; 00016 void F_UartSvc(void) LargeReentrant; 00017 void F_UartKeyInPut(INT8U KeyCode) LargeReentrant; 00018 INT8U F_UartKeyInGet() LargeReentrant; 00019 INT8U F_UartCheckKeyInBuffer() LargeReentrant; 00020 void F_UartKeyOutPut(INT8U) LargeReentrant; 00021 INT8U F_UartKeyOutGet() LargeReentrant; 00022 INT8U F_UartKeyOutGetIsr() LargeReentrant; 00023 INT8U F_UartCheckKeyOutBuffer() LargeReentrant; 00024 INT8U F_UartCheckKeyOutBufferIsr() LargeReentrant; 00025 char putchar(char) LargeReentrant; 00026 char getchar(void) LargeReentrant; 00027 00028 void F_UartPowerOnInit(void) LargeReentrant{ 00029 // Initial UART H/W 00030 SCON|=0x50; // serial configuration register... 00031 // bit76=01 mode 1 8 bit uart,bit4=1 receive enable. 00032 ES=1; // Enable serial interrupt...interrupt 4 00033 // variable initial 00034 bUartTransfer=0; 00035 UartCtrl.KeyInGet=0; 00036 UartCtrl.KeyInPut=0; 00037 } 00038 void F_UartKeyInPut(INT8U KeyCode) LargeReentrant{ 00039 if(UartCtrl.KeyInPut<(KEY_IN_BUFFER_SIZE-1)){ 00040 if((UartCtrl.KeyInPut+1)!=UartCtrl.KeyInGet){ 00041 UartCtrl.KeyInPut++; 00042 UartCtrl.KeyInBuffer[UartCtrl.KeyInPut]=KeyCode; 00043 } 00044 } 00045 else{ 00046 if(UartCtrl.KeyInGet==0); 00047 else{ 00048 UartCtrl.KeyInPut=0; 00049 UartCtrl.KeyInBuffer[UartCtrl.KeyInPut]=KeyCode; 00050 } 00051 } 00052 } 00053 INT8U F_UartKeyInGet(void) LargeReentrant{ 00054 if(UartCtrl.KeyInGet!=UartCtrl.KeyInPut){ 00055 if(UartCtrl.KeyInGet<(KEY_IN_BUFFER_SIZE-1)){ 00056 UartCtrl.KeyInGet++; 00057 return UartCtrl.KeyInBuffer[UartCtrl.KeyInGet]; 00058 } 00059 else{ 00060 UartCtrl.KeyInGet=0; 00061 return UartCtrl.KeyInBuffer[UartCtrl.KeyInGet]; 00062 } 00063 } 00064 return 0xff; 00065 } 00066 INT8U F_UartCheckKeyInBuffer(void) LargeReentrant{ // check key in buffer status 00067 if(UartCtrl.KeyInPut<(KEY_IN_BUFFER_SIZE-1)){ 00068 if((UartCtrl.KeyInPut+1)==UartCtrl.KeyInGet) return UART_KEY_BUFFER_FULL; 00069 else if(UartCtrl.KeyInGet!=UartCtrl.KeyInPut) return UART_KEY_BUFFER_NOT_FULL; 00070 else return UART_KEY_BUFFER_EMPTY; 00071 } 00072 else{ 00073 if(UartCtrl.KeyInGet==0) return UART_KEY_BUFFER_FULL; 00074 else if(UartCtrl.KeyInGet!=UartCtrl.KeyInPut) return UART_KEY_BUFFER_NOT_FULL; 00075 else if(UartCtrl.KeyInGet==(KEY_IN_BUFFER_SIZE-1)) return UART_KEY_BUFFER_EMPTY; 00076 } 00077 } 00078 char putchar(char c) LargeReentrant{ 00079 while(bUartTransfer==1); 00080 if(c==0x0a){ 00081 SBUF=0x0d; 00082 bUartTransfer=1; 00083 while(bUartTransfer==1); 00084 } 00085 SBUF=c; 00086 bUartTransfer=1; 00087 while(bUartTransfer==1); 00088 return c; 00089 } 00090 char getchar(void) LargeReentrant{ 00091 while(F_UartCheckKeyInBuffer()==UART_KEY_BUFFER_EMPTY); 00092 return (F_UartKeyInGet()); 00093 } 00094 00095 00096 00097