UART

to use printf library, need to configuration.

go to project properties.

type "-lprintf_flt"

select "Use vprintf library"

/* Hello_AVR.cpp
*
* Created: 2017-01-07(YYYY-MM-DD) AM 5:08:05
* Author : Michael Jun-Hyuk Im([email protected])
*/

#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

volatile unsigned int ADC_Buffer, ADC_Flag;

///ISR
ISR(USART0_RX_vect)
{
    Rx0Buffer = UDR0;
    Rx0Flag = 1;
}

///USART0
void USART0_Transmit(unsigned char data)
{
    /* Wait for empty transmit buffer */
    while (!(UCSR0A & (1<<UDRE0)));
    /* Put data into buffer, sends the data */
    UDR0 = data;
}

unsigned char USART0_Receive()
{
    /* Wait for data to be received */
    while (!(UCSR0A & (1<<RXC0)));
    /* Get and return received data from buffer */
    return UDR0;
}

void USART0_String(char *str)
{
    while(*str != 0)
    {
        //한자씩 보내기
        USART0_Transmit(*str);
        str++;
    }
}

///For fdevopen printf
int USART0_Transmit_Print(char ch, FILE *fp) {
    while (!(UCSR0A && (1 << UDRE0)));
    UDR0 = ch;
    return 0;
}

void MI_USART0_Config(unsigned char ubrr, bool fd)
{
    /* Set baud rate */
    UBRR0H = (unsigned char)(ubrr>>8);
    UBRR0L = (unsigned char)ubrr;
    /* Enable receiver and transmitter and receive interrupt*/
    UCSR0B |= (1<<RXCIE0) | (0<<TXCIE0) | (1<<RXEN0) | (1<<TXEN0) | (0<<UCSZ02);
    /* Set frame format: 8data, 1stop bit */
    UCSR0C |= (1<<UCSZ01) | (1<<UCSZ00);
    if(fd==true)
        USART0_String("UART0 Init\r\n"); //FD
}

int main(void)
{
    FILE* fpStdio = fdevopen(USART0_Transmit_Print, NULL); //Printf channel select 
    MI_USART0_Config(8,false); //8 -> 115200, 103 -> 9600
    sei();
    while (1)
    {        
        _delay_ms(1000);
        printf("Hello, Printf \r\n");
        _delay_ms(1000);
        USART0_String("Hello String");

        }
}

results matching ""

    No results matching ""