a2b0785ace59229731063366585e53ff3a2b5628
[fw/sdcc] / device / examples / ds390 / clock390 / clock390.c
1 /* This example compiles right out of the box for TINI after you did a 
2    make install of sdcc (latest revision :).
3    
4    Just do a "make" which compiles and compresses it a little.
5    Than use JavaKit to load clock390.hex (it goes into bank 1, see Makefile)
6    Type 'E' in JavaKit and enjoy :) */
7
8 /* if you are using the (default) interrupt scheme for serial i/o,
9    you need to include this in the main program, so the vector will
10    be initialized */
11 #include <serial390.h>
12
13 #include <ds80c390.h>
14 #include <stdio.h>
15 #include "lcd.h"
16
17 volatile unsigned long milliSeconds=0;
18
19 #define RELOAD_VALUE 18432000/2/1000 // appr. 1ms
20
21 void Timer2Handler (void) interrupt 5 using 1 {
22   TF2=0; // reset overflow flag
23   milliSeconds++;
24   // that's all for now :)
25 }
26
27 // we can't just use milliSeconds
28 unsigned long GetMilliSeconds(void) {
29   unsigned long ms;
30   ET2=0;
31   ms=milliSeconds;
32   ET2=1;
33   return ms;
34 }
35
36 void main (void) {
37   unsigned long ms, seconds, oldSeconds=-1;
38   
39   printf ("\n\rStarting timer 2 test.\n\r");
40
41   // initialise timer
42   ET2=0; // disable timer interrupts initially
43   T2CON=0; // stop, timer mode, autoreload
44   T2MOD&=0xf4;
45   
46   TL2=RTL2=(-RELOAD_VALUE)&0xff;
47   TH2=RTH2=(-RELOAD_VALUE)>>8;
48   TR2=1; // run
49
50   ET2=1; // enable timer interrupts
51
52   // now do something
53   LcdInit();
54   LcdLPutString(0, "Testing timer2");
55   LcdLPutString(2, "ms: ");
56   while (1) {
57     ms=GetMilliSeconds();
58     seconds=ms/1000;
59     LcdLPrintf (2 + (4<<8), "%ld", ms);
60     if (seconds!=oldSeconds) {
61       printf ("%02d:%02d.%02d\n\r", (int)seconds/3600, 
62               (int)(seconds/60)%60, 
63               (int)seconds%60);
64       oldSeconds=seconds;
65       _asm
66         cpl P3.5 ; toggle led
67       _endasm;
68     }
69   }
70 }
71
72