2
votes

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;
1
Hej Mikkel. Maybe you should ask the question at electronics.stackexchange.com too...JoSSte

1 Answers

0
votes

Have you tried using another port for the LEDs? Maybe the hardware is faulty.