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