Subversion Repositories Projects

Rev

Rev 112 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
112 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
114 mikeljo 22
//      UBRR = 7;               // 57600 Baud @ 7,372800 MHz
23
 
24
        UBRR=(F_CPU / (BAUD_RATE * 16L) - 1);
112 mikeljo 25
}
26
 
27
uint8_t RS232_get (void)
28
{
29
        uint8_t c;
30
 
31
        rs232_timer = 10;
32
 
33
        while ((read_index == write_index) && (rs232_timer > 0));
34
        if (rs232_timer != 0)
35
        {
36
        c = rs232_buffer[read_index++];
37
        read_index &= 15;
38
        }
39
        else
40
        {
41
                c=0;
42
        }
43
        return c;
44
}
45
void RS232_send (uint8_t data)
46
{
47
//      while ((UCSRA & (1<<UDRE)) == 0);
48
        while ((USR & (1<<UDRE)) == 0);
49
        UDR = data;
50
}
51
void RS232_text (uint8_t *text)
52
{
53
        while (*text)
54
        {
55
                RS232_send(*text);
56
                text++;
57
        }
58
}
59
 
60