main.c


1
#include <stdlib.h>
2
#include <inttypes.h>
3
#include <avr/io.h>
4
#include <util/delay.h>
5
6
#define BAUD 9600          // Baudrate für setbaud.h
7
#include <util/setbaud.h>
8
9
int uart_putc(unsigned char c) {
10
  while (!(UCSRA & (1 << UDRE))) {
11
  }
12
13
  UDR = c;
14
  return 0;
15
}
16
17
void init_uart(void) {
18
19
  UBRRH = UBRRH_VALUE;
20
  UBRRL = UBRRL_VALUE;
21
22
#if USE_2X
23
  UCSRA |= (1 << U2X);
24
#else
25
  UCSRA &= ~(1 << U2X);
26
#endif
27
28
  UCSRB |= (1 << TXEN); // UART TX einschalten
29
  UCSRB |= (1 << RXEN); // UART RX einschalten
30
  UCSRC |= (1 << URSEL) | (3 << UCSZ0); // Asynchron 8N1
31
32
}
33
34
int main(void) {
35
36
  DDRA = 0xFF;
37
  DDRB = 0xFF;
38
  DDRC = 0xFF;
39
  DDRD = 0xFF;
40
41
  init_uart();
42
43
  uart_putc('a');  //Debug
44
45
  ICR1 = 36864;  //20 msec bei Teiler 8 und 14,7456 MHz
46
  TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11); // lösche OC1A/OC1B bei OCR1A/OCR1B, setze OC1A/OC1B bei ICR1
47
  TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11); // Prescaler 8; Fast PWM; TOP = ICR1,
48
  TCNT1 = 0; // reset Timer
49
50
  uart_putc('b');  //Debug
51
52
  OCR1A = 1000; // set Servo 1 to 1ms-position
53
  OCR1B = 2000; // set Servo 1 to 2ms-position
54
55
  uart_putc('c');  //Debug
56
57
  for (int i = 0; i < 50; i++) {
58
    _delay_ms(10);
59
  }
60
61
  uart_putc('d');  //Debug
62
63
  OCR1A = 2000; // set Servo 1 to 1ms-position
64
  OCR1B = 1000; // set Servo 1 to 2ms-position
65
66
  uart_putc('e');  //Debug
67
68
  while (1) {
69
70
  }
71
}