1 | #include <c8051F330.h>
|
2 | #include <stdio.h>
|
3 |
|
4 | #define SYSCLK 24500000
|
5 | #define BAUDRATE 115200
|
6 | #define RX_length 25
|
7 |
|
8 | //-----------------------------------------------------------------------------
|
9 | // Global Variables
|
10 | //-----------------------------------------------------------------------------
|
11 |
|
12 | //bit TX_ready; //True = OK zur Übertragung
|
13 | //char *TX_ptr; //pointer auf zu übermittelnden String
|
14 |
|
15 | bit RX_ready; //True = string empfangen
|
16 | char RX_buf[RX_length]; //Array als Eingangsbuffer anlegen
|
17 | unsigned char the_char; //zwischenspeicher
|
18 |
|
19 | //-----------------------------------------------------------------------------
|
20 | // Function Prototypes
|
21 | //-----------------------------------------------------------------------------
|
22 |
|
23 | void OSCILLATOR_Init (void);
|
24 | void UART0_Init (void);
|
25 | void PORT_Init (void);
|
26 | void UART_ISR (void);
|
27 | void Print_String (char pstring[]);
|
28 |
|
29 | //-----------------------------------------------------------------------------
|
30 | // main Routine
|
31 | //-----------------------------------------------------------------------------
|
32 |
|
33 | void main (void)
|
34 | {
|
35 | // Disable Watchdog timer
|
36 | PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
|
37 | // enable)
|
38 | OSCILLATOR_Init (); // Initialize Oscillator
|
39 | PORT_Init(); // Initialize Port I/O
|
40 | UART0_Init (); // Initialize UART0
|
41 |
|
42 | EA = 1; //Interrupts global freigeben
|
43 |
|
44 | while (1)
|
45 | {
|
46 |
|
47 | }
|
48 | }
|
49 |
|
50 |
|
51 | //-----------------------------------------------------------------------------
|
52 | // Initialization Subroutines
|
53 | //-----------------------------------------------------------------------------
|
54 |
|
55 | //-----------------------------------------------------------------------------
|
56 | // OSCILLATOR_Init
|
57 | //-----------------------------------------------------------------------------
|
58 | //
|
59 | // Return Value : None
|
60 | // Parameters : None
|
61 | //
|
62 | // This routine initializes the system clock to use the internal 24.5MHz
|
63 | // oscillator as its clock source. Also enables missing clock detector reset.
|
64 | //
|
65 | //-----------------------------------------------------------------------------
|
66 | void OSCILLATOR_Init (void)
|
67 | {
|
68 | OSCICN |= 0x03; // Configure internal oscillator for
|
69 | // its maximum frequency
|
70 | RSTSRC = 0x04; // Enable missing clock detector
|
71 | }
|
72 |
|
73 | //-----------------------------------------------------------------------------
|
74 | // PORT_Init
|
75 | //-----------------------------------------------------------------------------
|
76 | //
|
77 | // Return Value : None
|
78 | // Parameters : None
|
79 | //
|
80 | // P0.4 digital push-pull UART TX
|
81 | // P0.5 digital open-drain UART RX
|
82 | //
|
83 | //-----------------------------------------------------------------------------
|
84 | void PORT_Init (void)
|
85 | {
|
86 | P0SKIP |= 0x01; // Skip P0.0 for external VREF
|
87 | P1MDIN |= 0xEF; // Configure P1.4 as analog input.
|
88 | P0MDOUT |= 0x10; // enable UTX as push-pull output
|
89 | XBR0 = 0x01; // Enable UART on P0.4(TX) and P0.5(RX)
|
90 | XBR1 = 0x40; // Enable crossbar and weak pull-ups
|
91 | }
|
92 |
|
93 | //-----------------------------------------------------------------------------
|
94 | // UART0_Init
|
95 | //-----------------------------------------------------------------------------
|
96 | //
|
97 | // Return Value : None
|
98 | // Parameters : None
|
99 | //
|
100 | // Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
|
101 | //
|
102 | //-----------------------------------------------------------------------------
|
103 | void UART0_Init (void)
|
104 | {
|
105 | SCON0 = 0x10; // SCON0: 8-bit variable bit rate
|
106 | // level of STOP bit is ignored
|
107 | // RX enabled
|
108 | // ninth bits are zeros
|
109 | // clear RI0 and TI0 bits
|
110 | if (SYSCLK/BAUDRATE/2/256 < 1) {
|
111 | TH1 = -(SYSCLK/BAUDRATE/2);
|
112 | CKCON &= ~0x0B; // T1M = 1; SCA1:0 = xx
|
113 | CKCON |= 0x08;
|
114 | } else if (SYSCLK/BAUDRATE/2/256 < 4) {
|
115 | TH1 = -(SYSCLK/BAUDRATE/2/4);
|
116 | CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
|
117 | CKCON |= 0x09;
|
118 | } else if (SYSCLK/BAUDRATE/2/256 < 12) {
|
119 | TH1 = -(SYSCLK/BAUDRATE/2/12);
|
120 | CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
|
121 | } else {
|
122 | TH1 = -(SYSCLK/BAUDRATE/2/48);
|
123 | CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
|
124 | CKCON |= 0x02;
|
125 | }
|
126 |
|
127 | TL1 = TH1; // Init Timer1
|
128 | TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
|
129 | TMOD |= 0x20;
|
130 | TR1 = 1; // START Timer1
|
131 | TI0 = 1; // Indicate TX0 ready
|
132 | }
|
133 |
|
134 | void UART0_ISR (void) interrupt 4 using 3
|
135 | {
|
136 | static unsigned char RX_index = 0; //receive buffer index
|
137 | unsigned char the_char; //zwischenspeicher
|
138 | Print_String ("In ISR..."); //Debugausgabe auf UART
|
139 |
|
140 | if (RI0 == 1)
|
141 | {
|
142 |
|
143 | the_char = SBUF0;
|
144 | RX_buf[RX_index] = the_char; //Byte in array speichern
|
145 |
|
146 | if(RX_index < (RX_length-2))
|
147 | {
|
148 | RX_index++;
|
149 | }
|
150 | else
|
151 | {
|
152 | RX_index = 0;
|
153 | RX_ready = 1;
|
154 | RX_buf[RX_index-1] = '\0';
|
155 | }
|
156 |
|
157 |
|
158 | }
|
159 | }
|
160 |
|
161 | void Print_String (char pstring[])
|
162 | {
|
163 | unsigned char i = 0;
|
164 | while (pstring[i])
|
165 | {
|
166 | putchar(pstring[i++]);
|
167 | }
|
168 | }
|