1 | #include <avr/io.h>
|
2 | #include <avr/sleep.h>
|
3 | #include <avr/interrupt.h>
|
4 | #include <util/delay.h>
|
5 | #include <stdio.h>
|
6 | #include <stdlib.h>
|
7 | #include <stdarg.h>
|
8 | #include <string.h>
|
9 |
|
10 | //Makros==============================
|
11 |
|
12 | #define SET_LED(x) PORTB=PORTB&(~(1<<x))
|
13 | #define CLEAR_LED(x) PORTB=PORTB|(1<<x)
|
14 |
|
15 | #define cb_SIZE 10
|
16 |
|
17 | uint8_t init_uart(uint16_t baud,uint8_t data_bits,uint8_t stop_bits);
|
18 | void rs232_send_char(unsigned char data);
|
19 | unsigned char USART_Receive(void);
|
20 | void USART_Init(unsigned int baud);
|
21 | int init(void);
|
22 |
|
23 | int main(void)
|
24 | {
|
25 | init();
|
26 |
|
27 | _delay_ms(2000);
|
28 | rs232_send_char(0x55);
|
29 | SET_LED(1);
|
30 |
|
31 | rs232_send_char(0x45);//Befehl für Hintergrund löschen
|
32 | SET_LED(2);
|
33 | rs232_send_char(0x42);//Befehl für Hintergrundfarbe ändern
|
34 | //Farbe ROT:
|
35 | rs232_send_char(0xF0);
|
36 | rs232_send_char(0x00);
|
37 | SET_LED(3);
|
38 |
|
39 | while(1)
|
40 | {
|
41 |
|
42 | }
|
43 | return 0;
|
44 | }
|
45 |
|
46 | //Die init Funktion initialsiert LEDs, CPU Tackt und USART=============
|
47 |
|
48 | int init(void)
|
49 | {
|
50 | DDRB=0xFF; /* set the Port PORTB to output */
|
51 | PORTB=0xFF; /* write all 1s to PORTB */
|
52 | DDRC=0x04; /* set the Port Pin PORTC.2 to output */
|
53 | PORTC=0x04; /* set the Port Pin PORTC.2 (LE) */
|
54 |
|
55 | //Clock Konfiguration====================================================
|
56 | CLKPR=0x80;
|
57 | CLKPR=0x00;//16MHz
|
58 | //USART Konfiguration====================================================
|
59 |
|
60 | init_uart(9600,8,1);
|
61 |
|
62 | return 0;
|
63 | }
|
64 |
|
65 | uint8_t init_uart(uint16_t baud,uint8_t data_bits,uint8_t stop_bits)
|
66 | {
|
67 |
|
68 | uint16_t temp; //temporary work var
|
69 | temp = (((F_CPU)/16)/baud)-1; //baud rate calculation. see at90usb162 datasheet page 149
|
70 | temp = (((F_CPU+baud*8)/16)/baud)-1;// better formula...
|
71 |
|
72 | if((data_bits >=5)&&(data_bits <=8))
|
73 | data_bits -=5; // 00 = 5 ; 11 = 8
|
74 | else
|
75 | return 1;
|
76 |
|
77 | if((stop_bits == 1)|(stop_bits == 2))
|
78 | stop_bits -= 1; //0 = 1bit 1 = 2bit
|
79 | else
|
80 | return 1;
|
81 |
|
82 | UCSR1B = (1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1); // Transmit, Receive enable
|
83 | UCSR1C = (stop_bits<<USBS1)|(data_bits<<UCSZ10); // 8Databits 1stopbits
|
84 | UBRR1H = (unsigned char)((temp & 0xFF00)>>8); //mask high byte and write it to UBBR1H
|
85 | UBRR1L = (unsigned char)(temp & 0x00FF); //mask low byte and write it to UBBR1L
|
86 |
|
87 | return 0;
|
88 |
|
89 | }
|
90 |
|
91 | void USART_Init(unsigned int baud)
|
92 | {
|
93 | /* Set baud rate */
|
94 | UBRR1H = (unsigned char)(baud>>8);
|
95 | UBRR1L = (unsigned char)baud;
|
96 | /* Enable receiver and transmitter */
|
97 | UCSR1B = (1<<RXEN1)|(1<<TXEN1);
|
98 | /* Set frame format: 8data, 2stop bit */
|
99 | //UCSR1C = (1<<USBS1)|(3<<UCSZ10);
|
100 | UCSR1C=UCSR1C|6;
|
101 | //UCSR1C = (3<<UCSZ10);
|
102 | }
|
103 |
|
104 | void rs232_send_char(unsigned char data)
|
105 | {
|
106 | /* Wait for empty transmit buffer */
|
107 | while(!(UCSR1A&(1<<UDRE1)))
|
108 | ;
|
109 | /* Put data into buffer, sends the data */
|
110 | UDR1 = data;
|
111 | }
|
112 |
|
113 | unsigned char USART_Receive(void)
|
114 | {
|
115 | /* Wait for data to be received */
|
116 | while(!(UCSR1A&(1<<RXC1)))
|
117 | ;
|
118 | /* Get and return received data from buffer */
|
119 | return UDR1;
|
120 | }
|