Lefty, so true. But still: if a newbie like me is welcome to ask these questions (which I was pretty reluctant to do) everyting works out in the end.
BTW: Maybe some expert can look over that code: sending works perfect but I'm not able to receive a valid ack from the box:
#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);
}
This is a connection to a "Jeti Box" which I will use for telemtry in my RC project. This code does nothing else but sending a certain data package over and over again and after sending the package it should read what the box answeres.
The com is a little weird: 9600 9E1. They obviously need the 9th bit as a control bit.
Plus this is a one wire communication: the only serial line is connected to the RX pin 0. Pin 0 to Pin 1 (RX to TX) is shortened with a 100 Ohm resistor to prevent any destructive current.
Sending works perfect. But I don't receive a valid ack. 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). He also has no idea.
Any ideas most welcome. Thank you very much for having a look at the code!
Cheers Alexander