1 | #define F_CPU 16000000
|
2 |
|
3 | #include <avr/io.h>
|
4 | #include <avr/interrupt.h>
|
5 |
|
6 | #define BAUD 9600
|
7 | #define MYUBRR F_CPU/16/BAUD-1
|
8 |
|
9 | unsigned char UART_receive( void );
|
10 | void UART_send( char data);
|
11 |
|
12 | void AVR_init(void) {
|
13 |
|
14 | /* Clock init done by FUSES, CLK = 16 MHz ext. Oscillator */
|
15 |
|
16 | /*LED PIN*/
|
17 | /*---------------------------------------------------------*/
|
18 | DDRB = (1 << PINB5);
|
19 |
|
20 | /* init UART asynchronous 9600 8Bits 1Stop */
|
21 | /*---------------------------------------------------------*/
|
22 | /*Set baud rate registers according to MYUBRR definition*/
|
23 | UBRR0H = (unsigned char) (MYUBRR >> 8);
|
24 | UBRR0L = MYUBRR;
|
25 |
|
26 | /* Enable receiver and transmitter */
|
27 | UCSR0B = (1 << RXEN0) | (1 << TXEN0);
|
28 |
|
29 | /* Set frame format: 8data, 1stop bit */
|
30 | UCSR0C = (0 << UMSEL00);
|
31 | UCSR0C |= (0 << UCSZ00) | (1 << UCSZ01) | (1 << UCSZ00);
|
32 |
|
33 | /*init PWM, Timer*/
|
34 | /*---------------------------------------------------------*/
|
35 | /*Timer/Counte0 as PWM Waveform Generator on PD6/PD5 OC0A/OC0B (Arduino Pin 6, 5) (FAST PWM MODE 3)*/
|
36 | DDRD = (1 << PIND6);
|
37 | DDRD = (1 << PIND5);
|
38 |
|
39 | TCCR0A = (1 << COM0A1) | (0 << COM0A0);
|
40 | TCCR0A |= (1 << COM0B1) | (0 << COM0B0);
|
41 |
|
42 | /*MODE 3: Fast PWM TOPP = 0xFF*/
|
43 | TCCR0A |= (1 << WGM01) | (1 << WGM00);
|
44 |
|
45 | /* MODE 7 */
|
46 | TCCR0B |= (0 << WGM02);
|
47 |
|
48 | /*Select CLK Source and Prescaler*/
|
49 | /*Prescaler 1, 8, 64, 256, 1024*/
|
50 | TCCR0B |= (0 << CS02) | (1 << CS01) | (0 << CS00);
|
51 | // HIER GEHT WAS SCHIEF sobald ein CS0x bit gesetzt wird
|
52 |
|
53 | /*Interrupt enable*/
|
54 | TIMSK0 = (1 << OCIE0A) | (1 << OCIE0B);
|
55 |
|
56 | /*This Register defines compare Match => Duty Cycle PWM Channel A in MODE 3 activated*/
|
57 | /*This Register defines compare Match => Periode PWM Channel A in MODE 7 activated*/
|
58 | OCR0A = 128;
|
59 |
|
60 | /*This Register defines compare Match => Duty Cycle PWM Channel B in MODE 3 activated*/
|
61 | /*This Register defines compare Match => Duty Cycle PWM Channel A in MODE 7 activated*/
|
62 | OCR0B = 64;
|
63 |
|
64 | /* MODE3: Output Waveform Frequency = F_CPU/Prescaler/256 */
|
65 | /* MODE7: Output Waveform Frequency = F_CPU/2/Prescaler/(OCR0A+1) */
|
66 |
|
67 | sei();
|
68 | };
|
69 |
|
70 | int main(void){
|
71 |
|
72 | AVR_init();
|
73 |
|
74 | while( 1 ) {
|
75 | UART_send( UART_receive() );
|
76 | }
|
77 | }
|
78 |
|
79 | unsigned char UART_receive(void){
|
80 | /* Wait for data to be received */
|
81 | while (!(UCSR0A & (1<<RXC0)));
|
82 | /* Get and return received data from buffer */
|
83 | return UDR0;
|
84 | };
|
85 |
|
86 | void UART_send( char data){
|
87 | /* Wait for empty transmit buffer */
|
88 | while (!(UCSR0A & (1<<UDRE0)));
|
89 | /* Put data into buffer, sends the data */
|
90 | UDR0 = data;
|
91 | };
|