1 | #include <avr/io.h>
|
2 | #include <avr/interrupt.h>
|
3 | #include <stdio.h>
|
4 |
|
5 | #include <util/delay.h> // F_CPU und -Os nicht vergessen!
|
6 | #define DEBUG 0 // steuert print Ausgabe
|
7 | #define DUMMY 1 // weil em4095.h und em4095_read_tag() fehlen
|
8 |
|
9 | #if DUMMY
|
10 | uint8_t em4095_read_tag(uint8_t *k)
|
11 | {
|
12 | return 1;
|
13 | }
|
14 | #else
|
15 | #include "em4095.h"
|
16 | #endif
|
17 |
|
18 | #define KEYS 2
|
19 | #define BAUD 9600UL
|
20 |
|
21 | /*EEPROM schreiben*/
|
22 | void EEPROM_write(unsigned int uiAddress, uint8_t ucData)
|
23 | {
|
24 | /* Wait for completion of previous write */
|
25 | while(EECR & (1<<EEPE))
|
26 | ;
|
27 |
|
28 | /* Set up address and data registers */
|
29 | EEAR = uiAddress;
|
30 | EEDR = ucData;
|
31 | /* Write logical one to EEMPE */
|
32 | EECR |= (1<<EEMPE);
|
33 | /* Start eeprom write by setting EEPE */
|
34 | EECR |= (1<<EEPE);
|
35 |
|
36 | /* Wait for completion of previous write */
|
37 | while(EECR & (1<<EEPE))
|
38 | ;
|
39 | }
|
40 |
|
41 |
|
42 | /*EEPROM lesen*/
|
43 | uint8_t EEPROM_read(unsigned int uiAddress)
|
44 | {
|
45 | /* Wait for completion of previous write */
|
46 | while(EECR & (1<<EEPE))
|
47 | ;
|
48 |
|
49 | /* Set up address register */
|
50 | EEAR = uiAddress;
|
51 | /* Start eeprom read by writing EERE */
|
52 | EECR |= (1<<EERE);
|
53 | /* Return data from data register */
|
54 |
|
55 | return EEDR;
|
56 | }
|
57 |
|
58 |
|
59 | void uart_putc(uint8_t c)
|
60 | {
|
61 | loop_until_bit_is_set(UCSRA, UDRE);
|
62 | UDR = c;
|
63 | }
|
64 |
|
65 |
|
66 | /*Funktion zum Bitweisen-Vergleichen der TAG IDs*/
|
67 | uint8_t cmp_tag(uint8_t *a, uint8_t *b)
|
68 | {
|
69 | int i;
|
70 | for (i=0;i<5;i++)
|
71 | {
|
72 | if(*a++ != *b++)
|
73 | return 0;
|
74 | }
|
75 | return 1;
|
76 | }
|
77 |
|
78 |
|
79 | /*RS232-Ausgabe */
|
80 | void print(char *text)
|
81 | {
|
82 | while (*text)
|
83 | uart_putc(*text++);
|
84 | }
|
85 |
|
86 | void nibbleout(uint8_t wert)
|
87 | {
|
88 | if(wert < 10)
|
89 | uart_putc(wert+'0');
|
90 | else
|
91 | uart_putc(wert+'A'-10);
|
92 | }
|
93 |
|
94 |
|
95 | void hexout(uint8_t wert)
|
96 | {
|
97 | nibbleout((wert&0xf0)>>4);
|
98 | nibbleout(wert&0xf);
|
99 | }
|
100 |
|
101 |
|
102 | void tagout(uint8_t *a)
|
103 | {
|
104 | int i;
|
105 | for (i=0;i<5;i++)
|
106 | {
|
107 | hexout(*a++);
|
108 | }
|
109 | print("\n\r");
|
110 | }
|
111 |
|
112 |
|
113 | int main(void)
|
114 | {
|
115 | // Lokale Variablen
|
116 | uint8_t tag[5]; // eingelesener Tag
|
117 | uint8_t key[5]; // Schlüssel (= gespeicherter Tag)
|
118 | unsigned char EEPROM_Inhalt;
|
119 | #if 0
|
120 | volatile uint32_t d;
|
121 | #else
|
122 | uint8_t d;
|
123 | #endif
|
124 |
|
125 | // Ports initialisieren
|
126 | DDRD = 1<<PD2;
|
127 | PORTD = 1<<PD2;
|
128 | //Leuchtdiodenport
|
129 | DDRB = 1<<PB6|1<<PB7;
|
130 |
|
131 | // UART initialisieren
|
132 | UCSRB = 1<<TXEN;
|
133 | UCSRC = 1<<UCSZ1 | 1<<UCSZ0;
|
134 | UBRRH = (uint8_t) (F_CPU / (BAUD * 16) - 1) / 256;
|
135 | UBRRL = (uint8_t) (F_CPU / (BAUD * 16) - 1);
|
136 |
|
137 | // Timer0 initialisieren
|
138 | TCCR0B = 1<<CS01 | 1<<CS00; //Timer/Counter0 prescaler 64
|
139 | MCUCR |= 1<<ISC10; //Any logical change on INT1 generates an interrupt request
|
140 | GIMSK |= 1<<INT1; //External interrupt request 1 enable (demod_out from RFID chip)
|
141 | sei();
|
142 |
|
143 | while (1)
|
144 | {
|
145 | /*
|
146 | Erstes Byte aus dem EEPROM einlesen
|
147 | Wenn 1. byte aus EEPROM UNGLEICH 0x1F
|
148 | ist, dann key mit em4095_read_tag()
|
149 | einlesen und in EEPROM speichern
|
150 | */
|
151 | EEPROM_Inhalt = EEPROM_read(0);
|
152 |
|
153 | if (EEPROM_Inhalt != 0x1F)
|
154 | {
|
155 | uint8_t KeyEingelesen = 0;
|
156 | while (!KeyEingelesen)
|
157 | {
|
158 | if (em4095_read_tag(key))
|
159 | {
|
160 | #if DEBUG
|
161 | print("Schluessel eingelesen: ");
|
162 | #endif
|
163 | tagout(key);
|
164 | KeyEingelesen = 1;
|
165 | #if DEBUG
|
166 | print("\n\r");
|
167 | #endif
|
168 | }
|
169 | }
|
170 | EEPROM_write(0,key[0]);
|
171 | EEPROM_write(1,key[1]);
|
172 | EEPROM_write(2,key[2]);
|
173 | EEPROM_write(3,key[3]);
|
174 | EEPROM_write(4,key[4]);
|
175 | }
|
176 | else
|
177 | {
|
178 | // key bereits im EEPROM vorhanden
|
179 | key[0]=EEPROM_read(0);
|
180 | key[1]=EEPROM_read(1);
|
181 | key[2]=EEPROM_read(2);
|
182 | key[3]=EEPROM_read(3);
|
183 | key[4]=EEPROM_read(4);
|
184 | }
|
185 |
|
186 | /*
|
187 | Weitere tags einlesen und mit key
|
188 | vergleichen und Ergebnis an LEDs anzeigen
|
189 | Abbruch durch Druck auf Taster
|
190 | */
|
191 | uint8_t abbruch = 0;
|
192 | while (!abbruch)
|
193 | {
|
194 | if (em4095_read_tag(tag))
|
195 | {
|
196 | tagout(tag);
|
197 | if (cmp_tag(key,tag))
|
198 | PORTB = 1<<PB6; // Led an Port PB6 aus und Led an PB7 an
|
199 | else
|
200 | PORTB = 1<<PB7; // Led an Port PB7 aus und Led an PB6 an
|
201 | }
|
202 | else
|
203 | {
|
204 | #if 0
|
205 | PORTB = 1<<PB7; //Led an Port PB7 aus und Led an PB6 an
|
206 | #else
|
207 | PORTB = (1<<PB7) | (1<<PB7); //Led an Port PB7 und PB6 aus
|
208 | #endif
|
209 | }
|
210 |
|
211 | #if 0
|
212 | for (d=0; d<50000; d++);
|
213 |
|
214 | if (PIND & (1<<PD4))
|
215 | {
|
216 | EEPROM_write(0,0x0);
|
217 | abbruch = 1;
|
218 | }
|
219 | #else
|
220 | /*
|
221 | User ca. 3s (120*25=3000) Zeit geben,
|
222 | um Taster zu drücken. Tastendruck selbst
|
223 | wird alle 25ms gepollt
|
224 |
|
225 | Tastendruck löscht 1. Byte vom EEPROM
|
226 | (key ungültig machen)
|
227 | */
|
228 | for (d = 0; d < 120; d++)
|
229 | {
|
230 | // active-high Taster gedrückt?
|
231 | if (PIND & (1<<PD4))
|
232 | {
|
233 | EEPROM_write(0,0x0);
|
234 | abbruch = 1; // while(!abbruch) verlassen
|
235 | break; // for sofort verlassen
|
236 | }
|
237 | _delay_ms(25);
|
238 | }
|
239 | #endif
|
240 | } // while(!abbruch)
|
241 | }
|
242 | }
|