1 | #include <SI_EFM8BB1_Register_Enums.h>
|
2 |
|
3 | #define SBUF SBUF0
|
4 | #define SCON SCON0
|
5 | #define TI SCON0_TI
|
6 | #define TR1 TCON_TR1
|
7 |
|
8 | // long delay
|
9 | void delay (unsigned char val)
|
10 | {
|
11 | volatile short idx, cnt;
|
12 |
|
13 | for (idx=0; idx<=val; idx++)
|
14 | for (cnt=0; cnt<=0x3FFF; cnt++);
|
15 | }
|
16 |
|
17 | // segment dot
|
18 | sbit S7 = P1^3;
|
19 |
|
20 | // serial send
|
21 | void send(unsigned char ch)
|
22 | {
|
23 | // add parity to char
|
24 | // unsigned char par = ((unsigned char) P) << 7;
|
25 | // SBUF = ch | par;
|
26 |
|
27 | // no partity
|
28 | SBUF = ch;
|
29 |
|
30 | while (!TI); // wait until transmitted
|
31 | TI=0;
|
32 | }
|
33 |
|
34 | void init(void)
|
35 | {
|
36 | // EFM8 specific serial configuration
|
37 | // P0.4 - UART0 TX - PUSH_PULL
|
38 | // P0.5 - UART0 RX - OPEN_DRAIN
|
39 | P0MDOUT |= (1 << (4));
|
40 |
|
41 | // EFM8 specific ADC configuration
|
42 | // P0.7 - ADC0
|
43 | P0MDIN &= ~(1 << (7)); // set P0.7 to analog input
|
44 | P0SKIP |= (1 << (7)); // disconnect P0.7 from digital
|
45 | ADC0MX = 7; // connect P0.7 to ADC0 via MUX
|
46 | // ADC0 configuration - %00001|1|0|1 - SYSCLK/(1+1)|8-bit mode|no delay|gain=1
|
47 | ADC0CF = 0x0D;
|
48 | REF0CN = 0x08; // voltage reference is VDD=3.3V
|
49 | ADC0CN0 |= (1 << (7)); // enable ADC0
|
50 |
|
51 | // SYSCLK = HFOSC0 = 24.5 MHz
|
52 | // SYSCLK = SYSCLK/1
|
53 | CLKSEL = 0;
|
54 | // timer 1 uses SYSCLK
|
55 | CKCON0 |= (1 << (3));
|
56 |
|
57 | XBR0 |= (1 << (0)); // cross-bar enable UART0 pins
|
58 | XBR2 |= (1 << (6)); // cross-bar enable all pins
|
59 |
|
60 | // 8051 serial configuration
|
61 | // UART mode %01 = 8-bit data
|
62 | SCON = 0x40;
|
63 | // timer 1 mode 2
|
64 | TMOD = 0x20;
|
65 | // Baud rate
|
66 | TH1 = 0x96; // 256-150=106 - 24.5 MHz / 106 = 231132 / 2 = 115566 (115200 Baud 0.3% error)
|
67 | TL1 = 0x96;
|
68 | // timer 1 start
|
69 | TR1 = 1;
|
70 | }
|
71 |
|
72 | void main(void)
|
73 | {
|
74 | unsigned short counter = 0;
|
75 | char ch, buffer[] = "-----"; // 4 decimal digits + CR terminal new line
|
76 | buffer[4] = 0x0D; // CR terminal new line
|
77 |
|
78 | init();
|
79 |
|
80 | while (1)
|
81 | {
|
82 | unsigned char idx = 0;
|
83 |
|
84 | // ADC0 conversion
|
85 | ADC0CN0 |= (1 << (4)); // start conversion
|
86 | while ((ADC0CN0 & 0x10) == 0); // wait for done
|
87 | ADC0CN0 &= ~(1 << (5)); // reset
|
88 |
|
89 | // CAUTION: 8-bit result is bits 9:2
|
90 | counter = ((ADC0H << 8) | ADC0L);
|
91 | counter >>= 2; // 8-bit result
|
92 |
|
93 | // convert to ASCII
|
94 | buffer[0] = 0x30 + counter/1000;
|
95 | buffer[1] = 0x30 + (counter%1000)/100;
|
96 | buffer[2] = 0x30 + (counter%100)/10;
|
97 | buffer[3] = 0x30 + counter%10;
|
98 |
|
99 | while ((ch = buffer[idx++]) != 0)
|
100 | send(ch);
|
101 |
|
102 | // blink dot
|
103 | S7 ^= 1;
|
104 |
|
105 | delay(25);
|
106 | }
|
107 | }
|