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