0922f9524e88d1542829c8ff27695d9267688a07
[fw/sdcc] / device / include / mcs51 / serial_IO.h
1 /* Default putchar() and getchar() to the serial port\r
2 \r
3    Written By -  Jesus Calvino-Fraga (October/2006)\r
4 \r
5    This library is free software; you can redistribute it and/or\r
6    modify it under the terms of the GNU Lesser General Public\r
7    License as published by the Free Software Foundation; either\r
8    version 2.1 of the License, or (at your option) any later version.\r
9 \r
10    This library is distributed in the hope that it will be useful,\r
11    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
13    Lesser General Public License for more details.\r
14 \r
15    You should have received a copy of the GNU Lesser General Public\r
16    License along with this library; if not, write to the Free Software\r
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA\r
18 */\r
19 \r
20 #ifndef SERIAL_IO_H\r
21 #define SERIAL_IO_H\r
22 \r
23 __sfr __at (0x87) SIO_PCON;\r
24 __sfr __at (0x89) SIO_TMOD;\r
25 __sfr __at (0x8D) SIO_TH1;\r
26 __sfr __at (0x8B) SIO_TL1;\r
27 __sfr __at (0x98) SIO_SCON;\r
28 __sfr __at (0x99) SIO_SBUF;\r
29 __sbit __at (0x8E) SIO_TR1;\r
30 \r
31 /*SCON bits*/\r
32 __sbit __at (0x98) SIO_RI;\r
33 __sbit __at (0x99) SIO_TI;\r
34 __sbit __at (0x9A) SIO_RB8;\r
35 __sbit __at (0x9B) SIO_TB8;\r
36 __sbit __at (0x9C) SIO_REN;\r
37 __sbit __at (0x9D) SIO_SM2;\r
38 __sbit __at (0x9E) SIO_SM1;\r
39 __sbit __at (0x9F) SIO_SM0;\r
40 \r
41 void inituart (unsigned char t1_reload)\r
42 {\r
43         SIO_TR1=0;\r
44         SIO_TMOD=(SIO_TMOD&0x0f)|0x20;\r
45         SIO_PCON|=0x80;\r
46         SIO_TH1=SIO_TL1=t1_reload;\r
47         SIO_TR1=1;\r
48         SIO_SCON=0x52;\r
49 }\r
50 \r
51 void putchar (char c)\r
52 {\r
53         if((!SIO_SM0)&&(!SIO_SM1)) inituart(0xff);\r
54         if (c=='\n')\r
55         {\r
56                 while (!SIO_TI);\r
57                 SIO_TI=0;\r
58                 SIO_SBUF='\r';\r
59         }\r
60         while (!SIO_TI);\r
61         SIO_TI=0;\r
62         SIO_SBUF=c;\r
63 }\r
64 \r
65 char getchar (void)\r
66 {\r
67         char c;\r
68         \r
69         if((!SIO_SM0)&&(!SIO_SM1)) inituart(0xff);\r
70 \r
71         while (!SIO_RI);\r
72         SIO_RI=0;\r
73         c=SIO_SBUF;\r
74         return c;\r
75 }\r
76 #endif\r