Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
111 | mikeljo | 1 | |
2 | #include <avr/io.h> |
||
3 | #include <inttypes.h> |
||
4 | #include <stdlib.h> |
||
5 | #include <avr/interrupt.h> |
||
6 | #include "main.h" |
||
7 | #include "rs232.h" |
||
8 | |||
9 | volatile uint8_t read_index, write_index; |
||
10 | volatile uint8_t rs232_buffer[16]; |
||
11 | |||
12 | ISR(USART_RXC_vect) |
||
13 | { |
||
14 | rs232_buffer[write_index++] = UDR; |
||
15 | write_index &= 15; |
||
16 | } |
||
17 | |||
18 | void RS232_init (void) |
||
19 | { |
||
20 | UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE); |
||
21 | // UBRRL = 7; // 57600 Baud @ 7,372800 MHz |
||
22 | UBRR = 7; // 57600 Baud @ 7,372800 MHz |
||
23 | } |
||
24 | |||
25 | uint8_t RS232_get (void) |
||
26 | { |
||
27 | uint8_t c; |
||
28 | |||
29 | rs232_timer = 10; |
||
30 | |||
31 | while ((read_index == write_index) && (rs232_timer > 0)); |
||
32 | if (rs232_timer != 0) |
||
33 | { |
||
34 | c = rs232_buffer[read_index++]; |
||
35 | read_index &= 15; |
||
36 | } |
||
37 | else |
||
38 | { |
||
39 | c=0; |
||
40 | } |
||
41 | return c; |
||
42 | } |
||
43 | void RS232_send (uint8_t data) |
||
44 | { |
||
45 | // while ((UCSRA & (1<<UDRE)) == 0); |
||
46 | while ((USR & (1<<UDRE)) == 0); |
||
47 | UDR = data; |
||
48 | } |
||
49 | void RS232_text (uint8_t *text) |
||
50 | { |
||
51 | while (*text) |
||
52 | { |
||
53 | RS232_send(*text); |
||
54 | text++; |
||
55 | } |
||
56 | } |
||
57 | |||
58 |