Kasutaja tarvikud

Lehe tööriistad


kursused:roboti_algkursused:2017.1:usart

USART

/*
 * USART main.c
 *
 */
 
#include <avr/io.h>
#include <util/delay.h>
 
void send_string(uint8_t *data)
{
    for (; *data != '\0'; data++)
    {
        while (!(UCSR0A & (1<<UDRE0)));
        UDR0 = *data;
    }
}
 
void send_char(uint8_t c)
{
    // Wait till data buffer empty
    while(!(UCSR0A & (1<<UDRE0)));
    // put data to buffer
    UDR0 = c;
}
 
void rec_string(uint8_t *arr, uint8_t len)
{
    uint8_t top = len - 1;
    uint8_t i;
 
    for (i = 0; i < top; i++)
    {
        while (!(UCSR0A & (1 << RXC0)));
        arr[i] = UDR0;
    }
 
    arr[i] = '\0';
}
 
uint8_t rec_char()
{
    // Wait data buffer
    while(!(UCSR0A & (1<<RXC0)));
    // get data from buffer
    return UDR0;
}
 
int main(void)
{
    UBRR0 = 103; // baud 9600
    /* Enable Tx and Rx */
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);
    /* Set frame format: 8data, 2stop bit */
    UCSR0C = (1<<USBS0)|(3<<UCSZ00);
    DDRD = (1<<1); // Tx as output
 
    while(1)
    {
        /*tagastab saadud väärtuse*/
        send_char(rec_char());
    }
}
kursused/roboti_algkursused/2017.1/usart.txt · Viimati muutnud: 2018/02/23 14:42 persoon erki.meinberg