Subversion Repositories group.electronics

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
122 pfowler 1
#include <avr/io.h>
2
#include <avr/wdt.h>
3
#include <avr/interrupt.h>
4
#include <avr/pgmspace.h>
5
#include <util/delay.h>
6
#include <stdlib.h>
7
 
8
#include "config.h"
9
#include "uart.h"
10
 
11
#ifndef NULL
12
#define NULL    ((void *)0)
13
#endif
14
 
15
 
16
/* ------------------------------------------------------------------------- */
17
 
18
#define UART_BAUD_RATE	9600
19
 
20
volatile uint8_t tmr0_ovf = 0;
21
volatile uint32_t systime = 0;
22
 
23
volatile uint16_t sendtime = 1000;
24
 
25
 
26
int main(void) {
27
 
28
 
29
  /*
30
  DDR : 1 = Output, 0 = Input
31
  PORT: 1 = Pullup for Input, otherwise set output
32
  PIN : Read input pin
33
  */
34
 
35
  /*
36
        PB0     - 
37
        PB1     - 
38
        PB2     - 
39
        PB3     - 
40
        PB4     - 
41
        PB5     - 
42
        PB6     - 
43
        PB7     - 
44
  */
45
  DDRB          = 0B11111111;
46
  PORTB         = 0B00000000;
47
 
48
  /*
49
        PC0     - 
50
        PC1     - 
51
        PC2     - 
52
        PC3     - 
53
        PC4     - 
54
        PC5     - 
55
  */
56
  DDRC          = 0B11111111;
57
  PORTC         = 0B00000000;
58
 
59
  /*
60
        PD0     - 
61
        PD1     - 
62
        PD2     - 
63
        PD3     - 
64
        PD4     - 
65
        PD5     - Input PcInt		- * I/O Interrupt PCINT21
66
        PD6     - Output		- * Status LED
67
        PD7     - Input, Pullup		- * Button
68
  */
69
  DDRD          = 0B01011111;
70
  PORTD         = 0B10000000;
71
 
72
    TIMSK0 = (1<<TOIE0);                    // Enable timer overflow
73
    TCNT0 = 0x00;                           // Set Timer0 initial value to 0
74
    TCCR0B = (1<< CS00) ;                   // /1 prescaler
75
 
76
 
77
 
78
 
79
  uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
80
 
81
 
82
    wdt_enable(WDTO_1S);		// Watchdog for 1 sec
83
    sei();				// Enable interrupts
84
 
85
    sbi(PIND, PD6);
86
    _delay_ms(500);
87
    cbi(PIND, PD6);
88
 
89
  char buffer[7];
90
 
91
    for(;;){
92
        wdt_reset();
93
 
94
	if (rbi(PIND, PD7))
95
	   cbi(PORTD, PD6);
96
	else
97
	   sbi(PORTD, PD6);
98
 
99
 
100
 
101
	if (!sendtime) {
102
	  ltoa(systime, buffer, 10);
103
	  uart_puts(buffer);
104
	  uart_puts("\r\n");
105
	  sendtime=1000;
106
	  xbi(PORTD, PD6);
107
	}
108
	_delay_ms(1);
109
 
110
    }
111
 
112
    return 0;
113
}
114
 
115
 
116
ISR(TIMER0_OVF_vect) {
117
        tmr0_ovf++;
118
 
119
	// Clk/1 TCCR0B = (1<< CS00);
120
	//20.0Mhz, 1ms = 78ovf
121
	//16.5Mhz, 1ms = 64ovf
122
	//16.0Mhz, 1ms = 62ovf
123
	//12.0Mhz, 1ms = 46ovf
124
 
125
        if (tmr0_ovf>=64) {
126
                systime++;
127
                tmr0_ovf = 0;
128
		if (sendtime)
129
		  sendtime--;
130
        }
131
}