1 | /*--------------------------------------------------------------------------*
|
2 | * Main program
|
3 | *---------------------------------------------------------------------------*
|
4 | * 14-Apr-2014 ShaneG
|
5 | *
|
6 | * Template program for ATtiny85 C/asm projects.
|
7 | *--------------------------------------------------------------------------*/
|
8 | #include <stdint.h>
|
9 | #include <stdio.h>
|
10 | #include <stdbool.h>
|
11 | #include <avr/io.h>
|
12 | #include <avr/interrupt.h>
|
13 | #include <avr/signal.h>
|
14 | #include "softuart.h"
|
15 | #include "iohelp.h"
|
16 | #include "utility.h"
|
17 | #include <util/delay.h>
|
18 |
|
19 |
|
20 |
|
21 | #ifndef OCR0A
|
22 | #define OCR0A OCR0 // 2313 support
|
23 | #endif
|
24 |
|
25 | #ifndef WGM12
|
26 | #define WGM12 CTC0 // 2313 support
|
27 | #endif
|
28 |
|
29 | #ifndef PINC
|
30 | #define KEY_INPUT PIND // 2313
|
31 | #else
|
32 | #define KEY_INPUT PINC // Mega8
|
33 | #endif
|
34 | #define LED_DIR DDRB
|
35 |
|
36 | //#define XTAL 11059201L // nominal value
|
37 | #define XTAL 32768L // after measuring deviation: 1.5s/d
|
38 |
|
39 | #define DEBOUNCE 256L // debounce clock (256Hz = 4msec)
|
40 |
|
41 | #define uchar unsigned char
|
42 | #define uint unsigned int
|
43 |
|
44 | uchar prescaler;
|
45 | uchar volatile second; // count seconds
|
46 |
|
47 |
|
48 | SIGNAL (SIG_OUTPUT_COMPARE1A)
|
49 | {
|
50 | /************************************************************************/
|
51 | /* Insert Key Debouncing Here */
|
52 | /************************************************************************/
|
53 |
|
54 | #if XTAL % DEBOUNCE // bei rest
|
55 | OCR0A = XTAL / DEBOUNCE - 1; // compare DEBOUNCE - 1 times
|
56 | #endif
|
57 | if( --prescaler == 0 ){
|
58 | prescaler = (uchar)DEBOUNCE;
|
59 | second++; // exact one second over
|
60 | #if XTAL % DEBOUNCE // handle remainder
|
61 | OCR0A = XTAL / DEBOUNCE + XTAL % DEBOUNCE - 1; // compare once per second
|
62 | #endif
|
63 | }
|
64 | }
|
65 |
|
66 | // Forward declaration with 'noreturn' attribute
|
67 | void main() __attribute__ ((noreturn));
|
68 |
|
69 | /** Program entry point
|
70 | */
|
71 | void main() {
|
72 | uartInit();
|
73 | uartPrint("Initalizing...\r\n");
|
74 |
|
75 | TCCR0B = (1<<WGM12) | (1<<CS10); // divide by 1
|
76 | // clear on compare
|
77 | OCR0A = XTAL / DEBOUNCE - 1; // Output Compare Register
|
78 | TCNT0 = 0; // Timmer startet mit 0
|
79 | second = 0;
|
80 | prescaler = (uchar)DEBOUNCE; //software teiler
|
81 |
|
82 | TIMSK = 1<<OCIE1A; // beim Vergleichswertes Compare Match
|
83 | // Interrupt (SIG_OUTPUT_COMPARE1A)
|
84 | sei();
|
85 |
|
86 | uartPrint("Initalized!");
|
87 | while(true) {
|
88 | if (second % 5) {
|
89 | char str[255];
|
90 | sprintf(str, "%d\r\n", second);
|
91 | uartPrint(str);
|
92 | }
|
93 | _delay_ms(20);
|
94 | }
|
95 | }
|