Hi,
I am working on a project to build a telemetry sensor for my Jeti Duplex RC System. The sensor can be connected to the so called Jeti Box which is nothing more than a dumb terminal. The data format is 9600 9O1.
I can also connect the sensor to the receiver in my RC plane and the 2x16 chars are automatically routed to the box that is connected to the transmitter. So far so good. Sending strings to the box does work. Also sending alarm beeps work.
After receiving the string the box is supposed to send an ack byte in which the pressed keys (there are four keys on the board) are pressed to allow a simple menu.
The problem is: sending works, receiving a byte does not work. I'm not able to receive a valid ack from the box: the first call of JetiGetchar returns that there was a buffer overflow and the second time I call it I always get 0xFE (which is not in the scope of what can be expected).
This is a one wire (TTL) communication: the only serial line is connected to the RX pin 0. Pin 0 and Pin 1 (RX to TX) are shortened with a 100 Ohm resistor to prevent any destructive current.
I tried everything. Double and triple checked the registers. No way. I also asked a friend who works on a similiar project (but not with Arduino). There are examples where it works.
#include <stdio.h>
#include <avr/io.h>
#define TX_COMPLETE (1<<TXC0)
#define GETCHAR_TIMEOUT_us 500000 // 50ms timeout bei Getchar
int lastJetiAnswer;
void jetiUartInit(void) {
Serial.begin(9600); // Set correct baud rate with Arduino Lib
PORTD=0x03; //UART RX->PD0, TX->PD1
DDRD=0x00; // Set all to input
UCSR0A=0x00;
UCSR0B=0x1C;
UCSR0C=B00111110; // 00=asynch, 11=Odd, 1 1Stop, 11 9 Bit , 0 default
UCSR0B &= ~(1<<TXEN0); //disable TX
// UCSR0B &= ~(1<<RXEN0); //disable RX
UCSR0B |= (1<<RXEN0); //enable RX
}
void JetiTransmitByte(unsigned char data, boolean setBit9)
{
if (setBit9 == true)
UCSR0B |= (1<<TXB80); // Set Bit 9
else
UCSR0B &= ~(1<<TXB80); //delete 9th Bit
/* Put data into buffer, sends the data */
UDR0 = data;
while ((UCSR0A & TX_COMPLETE)==0); //wait for TX complete
UCSR0A |= TX_COMPLETE; //clear TXC Flag
}
unsigned char JetiSensor(char line1[17], char line2[17], char alarmCode)
{
unsigned char ack;
int i;
UCSR0B |= (1<<TXEN0); //enable TX
// Send Alarm Code first, if alarmcode <> 0
if (alarmCode != 0) {
JetiTransmitByte(0x7E, false);
JetiTransmitByte(0x92, false);
JetiTransmitByte(0x23, false);
JetiTransmitByte(alarmCode, true);
}
// 34 Byte Data Package
JetiTransmitByte(0xFE, false);
for (i=0; i<16; i++) JetiTransmitByte(line1[i], true);
for (i=0; i<16; i++) JetiTransmitByte(line2[i], true);
JetiTransmitByte(0xFF, false);
JetiTransmitByte(0x00, false);
UCSR0B &= ~(1<<TXEN0); //disable TX
// UCSR0B &= ~(1<<RXEN0); //disable RX
// UCSR0B |= (1<<RXEN0); //enable RX
ack = JetiGetChar();
if (ack == 14) ack = JetiGetChar();
return ack;
}
unsigned char JetiGetChar(void)
{
unsigned long time = micros();
byte stat, erg;
/* Wait for data to be received */
while ( (UCSR0A & (1<<RXC0)) == 0 )
{
if (micros()-time > GETCHAR_TIMEOUT_us)
return 12; // return, if timout occures
}
/* Get and return received data from buffer */
stat = UCSR0A;
erg = UCSR0B; // first bit 9: discard
erg = UDR0;
if ( stat & (1<<FE0) ) return 13;
if ( stat & (1<<DOR0) ) return 14;
if ( stat & (1<<UPE0) ) return 15;
return erg;
}
void setup(void) {
jetiUartInit();
lastJetiAnswer= 99;
}
void loop(void) {
int i, bytecount;
char line1[17] = "FLAPS: UP "; // 17: allow \0 at the end
char line2[17] = "GEAR : DOWN ";
char alarm1[17] = "ALARM LINE 1 ";
char alarm2[17] = "ALARM LINE 2 ";
char msg[10] = " ";
char tm[10] = " ";
sprintf(msg, "%u----", lastJetiAnswer);
sprintf(tm, "%u-----", millis()/1000);
for (i=0; i<4; i++) {
if (tm[i] < 32) tm[i]=32;
line1[i+12] = tm[i];
}
for (i=0; i<4; i++) {
if (msg[i] < 32) msg[i]=32;
line2[i+12] = msg[i];
}
// Senden
if (millis()/1000 % 10 != 0)
lastJetiAnswer = JetiSensor(line1, line2, 0);
else {
// lastJetiAnswer = JetiSensor(alarm1, alarm2, 'B');
// delay(1000); // leave the alarm on the display
}
delay(20);
}
Any ideas most welcome. Thank you very much for having a look at the code!
Cheers Alexander