1 | /*
|
2 | * Ampel_code_empfaenger.c
|
3 | *
|
4 | * Created: 20.05.2016 16:46:31
|
5 | * Author : Marvin
|
6 | */
|
7 |
|
8 |
|
9 |
|
10 | /* Dies ist der Code zur Ampelsteuerung des FC Pfeil Broistedt.
|
11 | Geschrieben von Marvin Wenzel im Frühjahr 2016.
|
12 |
|
13 | Dieser Codeteil ist für die AMPELN, d.h. die Empfänger.
|
14 |
|
15 | Verwendet wird ein Atmega328PU sowie ein NRF24L01+ Funkmodul.
|
16 | Betrieben wird beides mit 3,3V. Getaktet wird mit den internen 8 MHz.
|
17 |
|
18 | Im folgenden sind die Pinbelegungen aufgeführt:
|
19 |
|
20 | PB0: CE
|
21 | PB1: CSN
|
22 | PB2: SSnot
|
23 | PB3: MOSI
|
24 | PB4: MISO
|
25 | PB5: SCK
|
26 | PB6:
|
27 | PB7:
|
28 |
|
29 | PC0:
|
30 | PC1:
|
31 | PC2:
|
32 | PC3:
|
33 | PC4:
|
34 | PC5:
|
35 | RESETnot: 3,3V
|
36 |
|
37 | PD0:
|
38 | PD1:
|
39 | PD2:
|
40 | PD3:
|
41 | PD4:
|
42 | PD5:
|
43 | PD6:
|
44 | PD7: IRQ (PCint23)
|
45 | */
|
46 |
|
47 | #ifndef F_CPU //Define F_CPU if not done
|
48 | #define F_CPU 16000000UL
|
49 | #endif
|
50 |
|
51 | #define BAUD 9600UL // Baudrate
|
52 |
|
53 | // Berechnungen
|
54 | #define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1) // clever runden
|
55 | #define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1))) // Reale Baudrate
|
56 | #define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // Fehler in Promille, 1000 = kein Fehler.
|
57 |
|
58 | #if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
|
59 | #error Systematischer Fehler der Baudrate groesser 1% und damit zu hoch!
|
60 | #endif
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | #include <avr/io.h>
|
66 | //#include <avr/interrupt.h>
|
67 | #define F_CPU 16000000UL
|
68 | #include <util/delay.h>
|
69 | #include "wl_module.h"
|
70 | #include "nRF24L01.h"
|
71 | #include "spi.h"
|
72 | #include <stdlib.h>
|
73 |
|
74 |
|
75 | void atmega_init();
|
76 | void uart_init(void);
|
77 | int uart_putuint8_1(uint8_t c);
|
78 | void uart_putuint8_s(uint8_t *s);
|
79 | int uart_putc(char c);
|
80 | void uart_puts (char *s);
|
81 |
|
82 | uint8_t empfangsdaten[wl_module_PAYLOAD] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3";
|
83 | uint8_t status_funkmodul = 0;
|
84 | uint8_t counter = 0;
|
85 | volatile uint8_t PTX;
|
86 | char stringbuffer[33];
|
87 | uint8_t addr[5];
|
88 |
|
89 | int main(void)
|
90 | {
|
91 | int8_t i;
|
92 | uart_init();
|
93 | uart_puts("Beginne Initialisierung...\r\n");
|
94 |
|
95 | atmega_init(); // Atmega initialisieren
|
96 | wl_module_init();
|
97 | _delay_ms(50);
|
98 | //sei();
|
99 | wl_module_rx_config();
|
100 | wl_module_config_register(EN_AA,ENAA_P0); //Auto Acknowledgment bei P0
|
101 |
|
102 |
|
103 | uint8_t status_funk = 0;
|
104 | // Status lesen
|
105 | uart_puts("Status: ");
|
106 | status_funk = wl_module_get_status();
|
107 | itoa(status_funk,stringbuffer,2);
|
108 | uart_puts(stringbuffer);//(status_funk + '0');
|
109 | uart_puts(" \r\n");
|
110 |
|
111 | uart_puts("RX_payload: ");
|
112 | status_funk = wl_module_get_rx_pw(0);
|
113 | itoa(status_funk,stringbuffer,10);
|
114 | uart_puts(stringbuffer);//(status_funk + '0');
|
115 | uart_puts(" \r\n");
|
116 |
|
117 | uart_puts("rf_setup: ");
|
118 | status_funk = wl_module_get_rf_setup();
|
119 | itoa(status_funk,stringbuffer,2);
|
120 | uart_puts(stringbuffer);//(status_funk + '0');
|
121 | uart_puts(" \r\n");
|
122 |
|
123 | uart_puts("rf_ch: ");
|
124 | status_funk = wl_module_get_rf_ch();
|
125 | itoa(status_funk,stringbuffer,10);
|
126 | uart_puts(stringbuffer);
|
127 | uart_puts(" \r\n");
|
128 |
|
129 | uart_puts("rx_addr: ");
|
130 | wl_module_get_rx_addr(addr,0,5);
|
131 | for (i=5; i>0; i--)
|
132 | {
|
133 | status_funk = addr[i-1];
|
134 | itoa(status_funk,stringbuffer,16);
|
135 | uart_puts(stringbuffer);
|
136 | uart_puts(", ");
|
137 | }
|
138 | uart_puts(" \r\n");
|
139 | _delay_ms(1500);
|
140 |
|
141 | PORTD = 0x00;
|
142 | // ACHTUNG: Playload steht momentan auf 32
|
143 | while (1)
|
144 | {
|
145 | uart_puts("Warte_auf Empfang...(RX_FIFO_DATA_READY_FLAG)\r\n");
|
146 |
|
147 | while(!wl_module_data_ready()); // Auf neue Daten warten (RX_DR)
|
148 | status_funkmodul = wl_module_get_data(empfangsdaten); // neue daten Abfragen
|
149 |
|
150 | switch (empfangsdaten[wl_module_PAYLOAD-1]-'0')
|
151 | {
|
152 | case 0: PORTD = 0x01; break;
|
153 | case 1: PORTD = 0x02; break;
|
154 | case 2: PORTD = 0x04; break;
|
155 | case 3: PORTD = 0x08; break;
|
156 | case 4: PORTD = 0x10; break;
|
157 | default: PORTD = 0x1F; break;
|
158 | }
|
159 |
|
160 | uart_puts("Empfangen:\r\n");
|
161 | uart_putuint8_s(empfangsdaten);
|
162 | uart_puts("\r\n");
|
163 | }
|
164 | }
|
165 |
|
166 |
|
167 | void atmega_init()
|
168 | {
|
169 | // Auf 8 MHz intern umstellen
|
170 | //CLKPR = 0x80; // Aenderungen erlauben
|
171 | //CLKPR = 0x00; // internen CLK nicht teilen = 8 MHz
|
172 |
|
173 |
|
174 | // Zum testen
|
175 | DDRD = 0xFF;
|
176 | PORTD = 0x00;
|
177 | }
|
178 |
|
179 | void uart_init(void)
|
180 | {
|
181 | UBRR0H = UBRR_VAL >> 8;
|
182 | UBRR0L = UBRR_VAL & 0xFF;
|
183 |
|
184 | UCSR0B |= (1<<TXEN0); // UART TX einschalten
|
185 | UCSR0C = (1<<UCSZ01)|(1<<UCSZ00); // Asynchron 8N1
|
186 | }
|
187 |
|
188 | int uart_putuint8_1(uint8_t c)
|
189 | {
|
190 | while (!(UCSR0A & (1<<UDRE0))) /* warten bis Senden moeglich */
|
191 | {
|
192 | }
|
193 |
|
194 | UDR0 = c; /* sende Zeichen */
|
195 | return 0;
|
196 | }
|
197 |
|
198 | void uart_putuint8_s(uint8_t *s)
|
199 | {
|
200 | while (*s)
|
201 | { /* so lange *s != '\0' also ungleich dem "String-Endezeichen(Terminator)" */
|
202 | uart_putc(*s);
|
203 | s++;
|
204 | }
|
205 | }
|
206 |
|
207 | int uart_putc(char c)
|
208 | {
|
209 | while (!(UCSR0A & (1<<UDRE0))) /* warten bis Senden moeglich */
|
210 | {
|
211 | }
|
212 |
|
213 | UDR0 = c; /* sende Zeichen */
|
214 | return 0;
|
215 | }
|
216 |
|
217 | void uart_puts(char *s)
|
218 | {
|
219 | while (*s)
|
220 | { /* so lange *s != '\0' also ungleich dem "String-Endezeichen(Terminator)" */
|
221 | uart_putc(*s);
|
222 | s++;
|
223 | }
|
224 | }
|