main.c


1
/*
2
 * main.c
3
 *
4
 *  Created on: 5.07.2012
5
 *  Author: christian
6
 */
7
8
#include "main.h"
9
#include <avr/io.h>
10
#include <eigene/uart.h>
11
#include <inttypes.h>
12
#include <avr/interrupt.h>
13
#include <avr/pgmspace.h>
14
#include <util/delay.h>
15
#include <avr/wdt.h>
16
17
#define UART_BAUD_RATE 2400
18
19
20
21
22
#define DCC_IN    PD3
23
#define DCC_PORT  PORTD
24
#define DCC_DDRX DDRD
25
#define DCC_PIN PIND
26
27
#define TIMER_ON TCCR1B |= (1 << CS10);  //Timer anschalten
28
#define TIMER_OFF TCCR1B &= ~(1 << CS10); TCNT1 =0;  //Timer ausschalten
29
30
#define INT_ON   GICR = (1 << INT1); //Interrupt angeschaltet
31
#define INT_OFF GICR &=~(1 << INT1);
32
33
const uint16_t Timer = F_CPU / 1000 / 1000 * 80;
34
volatile int Preambel_anz[20];
35
volatile uint64_t dcc = 0;
36
volatile int16_t Preambel_zahler=0;
37
38
ISR(INT1_vect)
39
{
40
  TIMER_ON; //startet counter
41
}
42
43
ISR(TIMER1_COMPA_vect)
44
{
45
  TIMER_OFF; //beendet_counter
46
  if ((DCC_PIN & (1 << DCC_IN))) //Wenn 0
47
  {
48
    Preambel_anz[Preambel_zahler]++;
49
    Preambel_zahler=0;
50
51
  }
52
53
  else
54
  {
55
    Preambel_zahler++;
56
  }
57
}
58
59
int main()
60
{
61
  DCC_DDRX = (0 << DCC_IN); //Eingang
62
  DCC_PORT = (1 << DCC_IN); //Pullup
63
64
  uart_init(UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU)); //standard in jedem UART-Programm
65
  interrupt_init();
66
  sei();
67
68
  _delay_ms(500);
69
  uart_puts_P("Hallo!");
70
  _delay_ms(500);
71
72
  uart_puts("weichendekoder");
73
74
   _delay_ms(3000);
75
   for(int i=0;i<20;i++)
76
   {
77
     uart_puts_P("\n"); 
78
     uart_puti(i);
79
     uart_puts_P(": "); 
80
     uart_puti(Preambel_anz[i]);
81
   }
82
   wdt_enable(WDTO_2S);//restart µC
83
  while (1);
84
85
  
86
  return 0;
87
}
88
89
void interrupt_init()
90
{
91
  //Interruptpin
92
  MCUCR |= (1 << ISC11) | (1 << ISC10);
93
  INT_ON;
94
95
  //timer interrupt
96
  TIMSK = (1 << OCIE1A); //Compare Match
97
  OCR1A = Timer; //auf 70µs eingestellt
98
  TCCR1B |= (1 << WGM12);
99
}