Imported Upstream version 2.9.0
[debian/cc1111] / device / examples / mcs51 / clock / hwinit.c
1 #include "8051.h"
2
3 #include "hw.h"
4
5 // timer 0 used for systemclock
6 #define TIMER0_RELOAD_VALUE -OSCILLATOR/12/1000 // 0.999348ms for 11.059Mhz
7
8 // timer 1 used for baudrate generator
9 #define TIMER1_RELOAD_VALUE -(2*OSCILLATOR)/(32*12*BAUD)
10
11 static long data milliSeconds;
12
13 void ClockIrqHandler (void) interrupt 1 using 3 {
14   TL0 = TIMER0_RELOAD_VALUE&0xff;
15   TH0 = TIMER0_RELOAD_VALUE>>8;
16   milliSeconds++;
17 }
18
19 // we can't just use milliSeconds
20 unsigned long ClockTicks(void) {
21   unsigned long ms;
22   ET0=0;
23   ms=milliSeconds;
24   ET0=1;
25   return ms;
26 }
27
28 unsigned char _sdcc_external_startup() {
29   // initialize timer0 for system clock
30   TR0=0; // stop timer 0
31   TMOD =(TMOD&0xf0)|0x01; // T0=16bit timer
32   // timeout is xtal/12
33   TL0 = -TIMER0_RELOAD_VALUE&0xff;
34   TH0 = -TIMER0_RELOAD_VALUE>>8;
35   milliSeconds=0; // reset system time
36   TR0=1; // start timer 0
37   ET0=1; // enable timer 0 interrupt
38   
39   // initialize timer1 for baudrate
40   TR1=0; // stop timer 1
41   TMOD = (TMOD&0x0f)|0x20; // T1=8bit autoreload timer
42   // baud = ((2^SMOD)*xtal)/(32*12*(256-TH1))
43   PCON |= 0x80; // SMOD=1: double baudrate
44   TH1=TL1=TIMER1_RELOAD_VALUE;
45   TR1=1; // start timer 1
46
47   // initialize serial port
48   SCON=0x52; // mode 1, ren, txrdy, rxempty
49
50   EA=1; // enable global interrupt
51
52   return 0;
53 }
54
55 char getchar() {
56   char c;
57   while (!RI)
58     ;
59   RI=0;
60   c=SBUF;
61   return c;
62 }
63
64 void putchar(char c) {
65   while (!TI)
66     ;
67   TI=0;
68   SBUF=c;
69   if (c=='\n') {
70     putchar('\r');
71   }
72 }