Solved: After much debugging I found out that it was the UART that was not working correctly (not sure why yet) and that port I used to turn on LEDs did not work (PORTC). So there were never any problem.
Original question
Hello I'm doing a project where I need to send some data over X10 from one microcontroller (specifically an ATmega32) to another microcontroller (again an ATmega32). Both are clocked at 3.8646 MHz and runs on a STK500.
It would seem that I send my data correctly, but my decoder seems to have trouble decoding the data.
First, when I receive my data (1 byte) over X10 I save every Manchester bit in a unsigned char array (1/0 as startbit and and rest is the data). I then save the data in a another unsigned char array (called buffer). For testing purposes I show that data on a terminal through UART. The relevant code can be found here.
unsigned char buffer[9];
index = 2;
for (int i = 0; i < BYTE; i++)
{
// If bit 1 is received save that
if (reciever[index] != 0 && reciever[index+1] == 0)
{
buffer[i] = 1;
index += 2;
}
// If bit 0 is received save that
else if (reciever[index] == 0 && reciever[index+1] != 0)
{
buffer[i] = 0;
index += 2;
}
else
break;
}
for (int i = 0; i < BYTE; i++)
{
SendChar(buffer[i]);
}
mode = MODE_IDLE;
This seems to work, if for example the transmitter sends decimal 150, the terminal shows [120 0 0 120 0 120 120 0] (for some reason '1' won't save as a '1', but '0' will save as a '0'), which would suggest that the data IS sent correctly.
However if I then try to decode the buffer to a singular char byte I never get what was intended. In the code below I again save the received data in a buffer and then I try show them on a terminal and some LEDs on the STK500.
unsigned char buffer[9];
index = 2;
for (int i = 0; i < BYTE+1; i++)
{
// If bit 1 is received save that
if (reciever[index] != 0 && reciever[index+1] == 0)
{
buffer[i] = 1;
index += 2;
}
// If bit 0 is received save that
else if (reciever[index] == 0 && reciever[index+1] != 0)
{
buffer[i] = 0;
index += 2;
}
else
break;
}
unsigned char shifter = 0;
unsigned char byte = 0;
for (int i = 0; i < BYTE+1; i++)
{
if (buffer[i] != 0)
{
shifter = (1 << (BYTE - (i+1)));
byte += shifter;
}
else if (buffer[i] = 0)
{
shifter = 0;
byte += shifter;
}
}
mode = MODE_IDLE;
SendChar(byte);
writeAllLEDs(byte);
On the terminal I get 128 and 120 and on the STK500 only led7 and led1 lights up, which would suggest that my decoding is off. However if try to debug through Atmel Studio, the variable byte always comes out with correct values I have chosen.
I could really use some help, because I can't figure out what is going on.
writeAllLEDs()
PORTC = ~pattern;
SendChar()
// Wait for transmitter register empty (ready for new character)
while ( (UCSRA & (1<<5)) == 0 )
{}
// Then send the character
UDR = Tegn;
EDIT 1: There does not seem to be any problems filling the buffer.
I'm filling reciever array in ISR when Timer2 overflows:
// Stop Timer2
TCCR2 = 0;
TCNT2 = 24;
if (mode == MODE_IDLE)
{
if (PIND & (1 << PD6))
{
reciever[index++] = 1;
}
else if (PIND & ~(1 << PD6))
{
if (reciever[0] == 1)
{
reciever[index++] = 0;
mode = MODE_RECEIVING;
}
}
}
else if (mode == MODE_RECEIVING)
{
if (PIND & (1 << PD6))
{
reciever[index++] = 1;
if (index == 19) mode = MODE_RECEIVED;
}
else if (PIND & ~(1 << PD6))
{
reciever[index++] = 0;
if (index == 19) mode = MODE_RECEIVED;
}
}
EDIT 2: Forgot to add how the values receiver, index are declared. They of course global:
volatile unsigned char mode = MODE_IDLE;
volatile unsigned int reciever[19] = {0};
volatile unsigned int index = 0;