Imported Upstream version 2.9.0
[debian/cc1111] / device / examples / mcs51 / simple2 / hi.c
1 /*------------------------------------------------------------------------
2  hello.c - This is a simple program designed to operate on basic MCS51
3    hardware at 11.0592Mhz.  It sets up the baudrate to 9600 and responds
4    to simple ascii commands on the serial port.
5
6    Its intended to be a simple example for SDCC and ucSim.
7    The redirection of the simluated serial port to a TCP port
8    is cool, try this:
9    Try: 
10    1.) type>s51 -k 5678 hi.ihx
11    2.) (Now telnet to 127.0.0.1 Port 5678)
12    3.) At the s51 prompt, type: run
13    4.) At the telnet prompt, type in a few keys followed by CR
14    5.) You should see this program send back what you typed.
15
16  6-28-01  Written by Karl Bongers(karl@turbobit.com)
17 |------------------------------------------------------------------------*/
18 #include <8052.h>
19
20 typedef unsigned char byte;
21 typedef unsigned int word;
22 typedef unsigned long l_word;
23
24 data byte li = 0;  // index into lbuf
25 data byte g_dc;
26 data byte lbuf[12];  // this is our line buffer, chars gather here till CR seen
27
28 /*------------------------------------------------------------------------
29   tx_char - transmit(tx) a char out the serial uart.
30 |------------------------------------------------------------------------*/
31 void tx_char(char c)
32 {
33   SBUF = c;
34   while (!TI)
35     ;
36   TI = 0;
37 }
38
39 /*------------------------------------------------------------------------
40   tx_str - transmit(tx) a string out the serial uart.
41 |------------------------------------------------------------------------*/
42 void tx_str(char *str)
43 {
44   
45   while (*str)
46     tx_char(*str++);
47 }
48
49 /*------------------------------------------------------------------------
50   main - 
51 |------------------------------------------------------------------------*/
52 void main(void)
53 {
54   PCON = 0x80;  /* power control byte, set SMOD bit for serial port */
55   SCON = 0x50;  /* serial control byte, mode 1, RI active */
56   TMOD = 0x21;  /* timer control mode, byte operation */
57   TCON = 0;     /* timer control register, byte operation */
58
59   TH1 = 0xFA;   /* serial reload value, 9,600 baud at 11.0952Mhz */
60   TR1 = 1;      /* start serial timer */
61
62   EA = 1;       /* Enable Interrupts */
63
64   TI = 0;       /* clear this out */
65   SBUF = '.';   /* send an initial '.' out serial port */
66   //ES = 1;                           /* Enable serial interrupts IE.4 */
67
68   tx_str("Hello World\n");
69
70   RI = 0;
71   g_dc = 0;
72   for (;;)
73   {
74     if (RI)  // we have new serial rx data
75     {
76       g_dc = SBUF;  // read the serial char
77       RI = 0;  // reset serial rx flag
78
79       tx_char(g_dc);   // echo back out as serial tx data
80       if ((g_dc == 0x0d) || (g_dc == '.') || (g_dc == 0x0a)) // if CR, then end of line
81       {
82         tx_char(0xd);  // CR
83         tx_char(0xa);  // LF
84         lbuf[li] = 0;
85         li = 0;
86         tx_str("You typed in this[");
87         tx_str(lbuf);
88         tx_str("]\n");
89       }
90       else
91       {
92         lbuf[li] = g_dc;
93         if (li < 11)
94           ++li;
95       }
96     }
97   }
98 }
99