-8
#include <avr/io.h>
#include <stdint.h>

// Ceramic Resonator
#ifndef F_CPU
#define F_CPU 3686400 // 4MHz
#endif

// UART
#define UART_BAUD_RATE 9600
#define UART_BAUD_CALC(UART_BAUD_RATE,F_OSC) ((F_CPU)/((UART_BAUD_RATE)*16L)-1)

int main(void)
{
    // USART
    UBRR0H =(uint8_t) (UART_BAUD_CALC(UART_BAUD_RATE,F_CPU) >>8);
    UBRR0L =(uint8_t) UART_BAUD_CALC(UART_BAUD_RATE,F_CPU);

    UCSR0B = (1<<RXEN0) | (1<<TXEN0); // enable receiver and transmitter,
    UCSR0C = (3<<UCSZ00); // 8 bit (default: asynchronous, no parity, 1 stop-bit)

    DDRC = (1<<5); // set data direction register bit 5 to one, this means PC5 is configured as output
    PORTC = (1<<5); // set output value of PC5 to High-Level (Source Current, 5V to ground)
    // VARIABLES
    //uint8_t get;
    // PROGRAM

    unsigned char code[3] = {'x','y','z'}; // Here you need to write your code
    unsigned char rcv[3]={'0','0','0'}; // received data

    int i = 0;

    while(1)
    {
        i = 0;
        for(i=0;i<=2;i++)
        {
            // wait for empty transmit buffer
            //while (!(UCSR0A & (1<<UDRE0)));
            // wait for data to be received
            while (!(UCSR0A & (1<<RXC0)));
            /* put data into buffer, sends the data*/
            {  
                code[i]= UDR0  ;
            }

            PORTC ^= (1<<5); //this is for LED

            // get received data from buffer
            rcv[i] = code[i];
        }
        // wait for empty transmit buffer
        while (!(UCSR0A & (1<<UDRE0)));
        // put data into buffer, sends the data
        if ((rcv[0] == rcv[1]) && (rcv[0] == rcv[2]) && (rcv[1] == rcv[2]))
        UDR0 = 00;             
        else UDR0 = 01;
    }
}

This is my program where I send a data from PC to micro controller (Atmega 168PA by Docklight). I send three identical bytes. Then the micro controller must compare them and send me a boolean value, indicating whether they're equal (like 00) or not (like 01).

Next task is to verify where is the error happens or which byte is an error.

Also when the micro controller receives a data it stores three bytes as one data (data[i]) it is just repeated three times.

So if i send a data somehow('18' '19' '18') it must tell me where the error occured...

djf
  • 6,592
  • 6
  • 44
  • 62
Abay
  • 11
  • 1
  • 5
    This question is really incomprehensible. – Daniel Hilgarth Jul 09 '13 at 08:14
  • 2
    I don't know what the question is, but did you mean to put the semi colons on the ends of the whiles, eg "while (!(UCSR0A & (1< – doctorlove Jul 09 '13 at 08:15
  • @doctorlove I removed them, cause they're obviously misplaced (and meant for code highlighting). – Mario Jul 09 '13 at 08:18
  • 1
    Looks to me like a barebone program for the controller and te OP (student?) has to implement the comparison/validating part. At least that's what I assume reading the comments. – Mario Jul 09 '13 at 08:20
  • 1
    @doctorlove In this case, it's OK because it's a busy wait for a bit to flip in a register of the uC. (Just as the comments right in front of the while-statements suggest) Abay: Do you have an actual question or did you just want to share your exercise with us? – junix Jul 09 '13 at 08:31

1 Answers1

0

looks like you have setup the usart on usart0, set baudrate and enable tx and rx with no parity and 1 stop bit. Inside while loop you are taking data from usart read buffer using without interrupts mechanism and checking some condition and giving 01 or 00 as output. You should rather try UDR0 = 0x00 else UDR0 = 0x01;

Ishmeet
  • 1,540
  • 4
  • 17
  • 34