-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommunication.cpp
313 lines (278 loc) · 9.15 KB
/
communication.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Technische Richtlinie FA205
// Bibliothek: communication.c
// Controller: ATMEL ATMega8 / ATmega328
// I2C und RS232 Routinen
// Version: 1.0
// erstellt am: 5.5.2015
// letzte Änderung: 17.3.2018
// Autor: Rahm
#include "communication.h"
//#include "interrupt.h"
// *****************************************************************************************
// I2C- Routinen zur Ansteuerung eins I2C-Slaves
// I2C-Bus-Funktionen i2c_init, i2c_start, i2c_stop, i2c_write, i2c_read, i2c_ack, i2c_nack
// *****************************************************************************************
// ****** Initialiserung I2C-Bus **********************************************
void i2c_init (void)
{
#ifdef _ATMEGA8_
#error not yet implemented for _ATMEGA8_ :(
#endif
#ifdef _ATMEGA328_
#ifdef _ATMEGA328PB_
TWSR0 &= ~((1<<TWPS0)|(1<<TWPS1)); // Prescaler = 1
TWBR0 = ((F_CPU / _I2C_FREQUENZ_) - 16) / 2;
#else
TWSR &= ~((1<<TWPS0)|(1<<TWPS1)); // Prescaler = 1
TWBR = ((F_CPU / _I2C_FREQUENZ_) - 16) / 2;
#endif
#endif
}
// ****** Startbedingung I2C-Bus **********************************************
void i2c_start (void)
{
#ifdef _ATMEGA8_
#error not yet implemented for _ATMEGA8_ :(
#endif
#ifdef _ATMEGA328_
#ifdef _ATMEGA328PB_
/*writing a one to TWINT clears it, TWSTA=Start, TWEN=TWI-enable*/
TWCR0 = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); // Control Register
/*wait, until start condition has been sent --> ACK*/
while (!(TWCR0 & (1<<TWINT)));
#else
/*writing a one to TWINT clears it, TWSTA=Start, TWEN=TWI-enable*/
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); // Control Register
/*wait, until start condition has been sent --> ACK*/
while (!(TWCR & (1<<TWINT)));
#endif
#endif
}
//****** Stoppbedingung I2C-Bus ***********************************************************
void i2c_stop (void)
{
#ifdef _ATMEGA8_
#error not yet implemented for _ATMEGA8_ :(
#endif
#ifdef _ATMEGA328_
#ifdef _ATMEGA328PB_
/*writing a one to TWINT clears it, TWSTO=Stop, TWEN=TWI-enable*/
TWCR0 = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN);
#else
/*writing a one to TWINT clears it, TWSTO=Stop, TWEN=TWI-enable*/
TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN);
#endif
#endif
}
//*****************************************************************************************
// * Byte ausgeben an I2C-Bus , Rückgabewert = ack = ACK/NACK
// ****************************************************************************************
uint8_t i2c_write (uint8_t byte)
{
#ifdef _ATMEGA8_
#error not yet implemented for _ATMEGA8_ :(
#endif
#ifdef _ATMEGA328_
#ifdef _ATMEGA328PB_
/*TWDR contains byte to send*/
TWDR0 = byte;
/*send content of TWDR*/
TWCR0 = (1<<TWINT) | (1<<TWEN);
/*wait, until byte has been sent --> ACK*/
while (!(TWCR0 & (1<<TWINT)));
// Abfrage der TWI-Statuscodes (ATmega128 Manual S.214)
if (((TWSR0 & 0xf8)== 0x18) || ((TWSR0 & 0xf8)== 0x28)) return ACK;
if (((TWSR0 & 0xf8)== 0x20) || ((TWSR0 & 0xf8)== 0x30)) return NACK;
return 0; //Status Register
#else
/*TWDR contains byte to send*/
TWDR = byte;
/*send content of TWDR*/
TWCR = (1<<TWINT) | (1<<TWEN);
/*wait, until byte has been sent --> ACK*/
while (!(TWCR & (1<<TWINT)));
// Abfrage der TWI-Statuscodes (ATmega128 Manual S.214)
if (((TWSR & 0xf8)== 0x18) || ((TWSR & 0xf8)== 0x28)) return ACK;
if (((TWSR & 0xf8)== 0x20) || ((TWSR & 0xf8)== 0x30)) return NACK;
return 0; //Status Register
#endif
#endif
}
//*****************************************************************************************
// * Byte einlesen vom I2C-Bus.
// ****************************************************************************************
uint8_t i2c_read (uint8_t ack)
{
#ifdef _ATMEGA8_
#error not yet implemented for _ATMEGA8_ :(
#endif
#ifdef _ATMEGA328_
#ifdef _ATMEGA328PB_
if (ack == ACK)
/*send content of TWDR; TWEA = enable ACK*/
TWCR0 = (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
else
/*send content of TWDR; NACK*/
TWCR0 = (1<<TWINT) | (1<<TWEN);
/*wait, until byte has been received --> ACK*/
while (!(TWCR0 & (1<<TWINT)));
return TWDR0;
#else
if (ack == ACK)
/*send content of TWDR; TWEA = enable ACK*/
TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
else
/*send content of TWDR; NACK*/
TWCR = (1<<TWINT) | (1<<TWEN);
/*wait, until byte has been received --> ACK*/
while (!(TWCR & (1<<TWINT)));
return TWDR;
#endif
#endif
}
// ****************************************************************************
// RS232-Routinen zur Kommunikation mit PC-Terminal
//
// ****************************************************************************
void rs232_init(void)
{
rs232_baud(BAUD);
//// Hilfsmakro zur UBRR-Berechnung ("Formel" laut Datenblatt)
//#define UART_UBRR_CALC(BAUD_,FREQ_) ((FREQ_)/((BAUD_)*16L)-1)
//#ifdef _ATMEGA8_
//UCSRB |= (1<<TXEN) | (1<<RXEN); // UART TX und RX einschalten
//UCSRC |= (1<<URSEL)|(3<<UCSZ0); // Asynchron 8N1
//UBRRH = (uint8_t)( UART_UBRR_CALC( BAUD, F_CPU ) >> 8 );
//UBRRL = (uint8_t) UART_UBRR_CALC( BAUD, F_CPU );
//#endif
//#ifdef _ATMEGA328_
//UCSR0B |= (1<<TXEN0) | (1<<RXEN0); // UART TX und RX einschalten
//UCSR0C |= (1<<USBS0) | (3<<UCSZ00); // Asynchron 8N1
//UBRR0H = (uint8_t)( UART_UBRR_CALC( BAUD, F_CPU ) >> 8 );
//UBRR0L = (uint8_t) UART_UBRR_CALC( BAUD, F_CPU );
//#endif
}
void rs232_baud ( uint32_t baud ) // Ändert die Baudrate
{
#define UART_UBRR_CALC(BAUD_,FREQ_) ((FREQ_)/((BAUD_)*16L)-1)
#ifdef _ATMEGA8_
UCSRB |= (1<<TXEN) | (1<<RXEN); // UART TX und RX einschalten
UCSRC |= (1<<URSEL)|(3<<UCSZ0); // Asynchron 8N1
UBRRH = (uint8_t)( UART_UBRR_CALC( baud, F_CPU ) >> 8 );
UBRRL = (uint8_t) UART_UBRR_CALC( baud, F_CPU );
#endif
#ifdef _ATMEGA328_
#include <util/setbaud.h>
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
UCSR0B |= (1<<TXEN0) | (1<<RXEN0); // UART TX und RX einschalten
UCSR0C |= (1<<USBS0) | (3<<UCSZ00); // Asynchron 8N1
//UBRR0H = (uint8_t)( UART_UBRR_CALC( baud, F_CPU ) >> 8 );
//UBRR0L = (uint8_t) UART_UBRR_CALC( baud, F_CPU );
#endif
}
uint8_t rs232_get ( void )
{
#ifdef _ATMEGA8_
if (!(UCSRA & (1<<RXC))) return ('\0'); // Wenn kein Zeichen im Puffer, dann 0 zurückgeben!
return UDR; // Zeichen zurückliefern
#endif
#ifdef _ATMEGA328_
if (!(UCSR0A & (1<<RXC0))) return ('\0'); // Wenn kein Zeichen im Puffer, dann 0 zurückgeben!
return UDR0; // Zeichen zurückliefern
#endif
}
void rs232_put ( uint8_t value )
{
#ifdef _ATMEGA8_
while (!(UCSRA & (1<<UDRE))); // Warten bis Sendebereit!
UDR = value;
#endif
#ifdef _ATMEGA328_
while (!(UCSR0A & (1<<UDRE0))); // Warten bis Sendebereit!
UDR0 = value;
#endif
}
void rs232_print ( uint8_t *text )
{
while (*text != '\0')
rs232_put(*text++);
}
// RS232-Erweiterungen sind nicht Teil der Technischen Richtlinie FA205!!
// Erzeugt ein Eingabeprompt am Terminal 00 .. 99
uint8_t rs232_inputdd(void )
{
uint8_t buf1,buf2;
do
{
while((buf1 = rs232_get()) == 0);
} while ((buf1 < '0') || (buf1 > '9'));
rs232_put(buf1); // Echo
do
{
while((buf2 = rs232_get()) == 0);
} while ((buf1 < '0') || (buf1 > '9'));
rs232_put(buf2); // Echo
buf1 -= '0';
buf2 -= '0';
return (buf1*10 + buf2);
}
// Ausgabe einer Dezimalzahl 00..99 auf RS232
void rs232_printdd(uint8_t value)
{
uint8_t buf;
buf = value / 10;
rs232_put(buf+'0'); // 10er-Stelle anzeigen
buf = value % 10;
rs232_put(buf+'0'); // 1er-Stelle anzeigen
}
void rs232_int(uint16_t val)
{
uint8_t buffer[5];
uint8_t n = 0;
do
{
buffer[n++] = val%10 + '0';
} while ((val /= 10) > 0);
while (n<5) // Rest von buffer mit blank füllen
{
buffer[n++] = ' ';
}
while (n > 0) // Ausgabe auf das Display (umgekehrt)
{
n--;
rs232_put(buffer[n]);
}
}
uint8_t rs232_binary_get ( void )
{
while (!rs232_is_received()); // warten, bis Byte im Puffer!
return UDR0; // wert zurückgeben
}
uint8_t rs232_readbytes(uint8_t *buf, int8_t _length)
{
uint16_t timeout = 0;
uint8_t i = 0;
//Puffer löschen
for (uint8_t n=0; n<=_length;n++) buf[n] = 0;
while(_length > 0)
{
timeout = 0;
while(!rs232_is_received())
{
if(timeout>=0xfffe) return 0;
else timeout++;
}
*buf++ = rs232_binary_get();
_length--;
i++;
}
return i;
}
uint8_t rs232_is_received( void )
{
#ifdef _ATMEGA328_
if (!(UCSR0A & (1<<RXC0))) return 0; // 0, wenn kein Byte im Puffer!
return 1; // 1, wenn ein Byte im Puffer
#endif
}