1 | #include <stdlib.h>
|
2 | #include <avr/io.h>
|
3 | #include <avr/interrupt.h>
|
4 | #include <avr/pgmspace.h>
|
5 | #include <util/crc16.h>
|
6 | #include <avr/eeprom.h>
|
7 | #include "uart.h"
|
8 | #include "lib_crc.h"
|
9 | #include <util/delay.h>
|
10 |
|
11 | /* define CPU frequency in Mhz here if not defined in Makefile */
|
12 | #ifndef F_CPU
|
13 | #define F_CPU 8000000UL
|
14 | #endif
|
15 |
|
16 |
|
17 | /* 9600 baud */
|
18 | #define UART_BAUD_RATE 9600
|
19 |
|
20 | uint8_t count =0;
|
21 | uint8_t timerdelay = 10;
|
22 | uint8_t t_overflow = 0;
|
23 | uint16_t ptherme EEMEM = 0000;
|
24 | void entprellung( volatile uint8_t *port, uint8_t maske ) {
|
25 | uint8_t port_puffer;
|
26 | uint8_t entprellungs_puffer;
|
27 |
|
28 | for( entprellungs_puffer=0 ; entprellungs_puffer!=0xff ; ) {
|
29 | entprellungs_puffer<<=1;
|
30 | port_puffer = *port;
|
31 | _delay_us(150);
|
32 | if( (*port & maske) == (port_puffer & maske) )
|
33 | entprellungs_puffer |= 0x01;
|
34 | }
|
35 | }
|
36 |
|
37 | ISR (TIMER1_OVF_vect)
|
38 | { if(count==timerdelay)
|
39 | {
|
40 | //PORTD ^= (1<<PD3); //Toggeln
|
41 | count=0;
|
42 | t_overflow = 1;
|
43 | TIMSK &= ~(1<<TOIE1);
|
44 | }
|
45 | count++;
|
46 | }
|
47 |
|
48 | ISR (INT0_vect)
|
49 | {
|
50 | entprellung( &PIND, (1<<PIND2) ); // ggf. Prellen abwarten
|
51 | if( PIND & (1<<PIND2) ) // dann stabilen Wert einlesen
|
52 | {
|
53 | PORTD ^= (1<<PD3); //Toggeln
|
54 |
|
55 | //key_pressed =1; // Taste wurde gedrückt
|
56 |
|
57 | }
|
58 | else
|
59 | {
|
60 |
|
61 | // mach was anderes
|
62 | }
|
63 |
|
64 | }
|
65 |
|
66 |
|
67 |
|
68 | uint16_t CRC_ausgeben(uint8_t *werte,uint8_t j)
|
69 | {
|
70 | uint8_t i;
|
71 | uint16_t CRC_r = 0x0000;
|
72 | unsigned short crc =0xFFFF;
|
73 |
|
74 | for(i=0;i<j; i++)
|
75 | {
|
76 | if(i==0)
|
77 | CRC_r=update_crc_16(crc,werte[i]);
|
78 | else
|
79 | CRC_r=update_crc_16(CRC_r,werte[i]);
|
80 | }
|
81 | return CRC_r;
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | int main(void)
|
89 | {
|
90 |
|
91 | uint8_t sendstring[6];
|
92 | uint8_t i;
|
93 | uint16_t crc =0xFFFF;
|
94 | uint16_t ergebnis;
|
95 |
|
96 | uint8_t c;
|
97 | uint8_t wr_typ = 0;
|
98 | //Initialisierung
|
99 | //USART initialisieren
|
100 | uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
|
101 |
|
102 | //interrupt an Pin INT0 konfigurieren
|
103 | DDRD &= ~(1<< DDD2); //Eingang
|
104 | PORTD |= (1<< PD2);
|
105 | GICR |= (1<<INT0);
|
106 | sei();
|
107 | //PIN 3 Port D auf Ausgang
|
108 | DDRD |= (1<<DDD3);
|
109 | PORTD |= (1<<PD3); //Einschalten
|
110 | // 16-bit Timer aktivieren Überlauf alle 8,388 Sekunden
|
111 | TCCR1B |= (1<<CS12)| (1<<CS10);
|
112 | TIMSK |= (1<<TOIE1);
|
113 | //Programmstart
|
114 | timerdelay = 0; // 4 = ca. 30 Sekunden warten bis WR bereit
|
115 |
|
116 | sendstring[0]=0x02;
|
117 | sendstring[1]=0x04;
|
118 | sendstring[2]=0x00;
|
119 | sendstring[3]=0x00;
|
120 | sendstring[4]=0x00;
|
121 | sendstring[5]=0x02;
|
122 | /*
|
123 | * Transmit single character to UART
|
124 | */
|
125 | uart_putc('\r'); //<-- wird gesendet
|
126 |
|
127 |
|
128 | ergebnis = CRC_ausgeben( sendstring,6);
|
129 |
|
130 | i = ergebnis;
|
131 | uart_putc(i);//<-- wird gesendet
|
132 | i = ergebnis >> 8;
|
133 | uart_putc(i);//<-- wird gesendet
|
134 | // eeprom_busy_wait();
|
135 | //eeprom_write_word(&ptherme, 0xffff);
|
136 |
|
137 | uart_putc('\r'); // <-- wird nicht gesendet
|
138 | //eeprom_busy_wait();
|
139 | // ergebnis = eeprom_read_word(&ptherme);
|
140 |
|
141 | uart_putc('\r'); // <-- wird nicht gesendet
|
142 | uart_putc(ergebnis); // <-- wird nicht gesendet
|
143 | uart_putc(ergebnis>>8); // <-- wird nicht gesendet
|
144 | PORTD ^= (1<<PD3); //Toggeln // LED geht aber aus, wurde während der Initialisierung eingeschaltet
|