본문 바로가기
프로그래밍 언어

printf 로 uart 출력방법(AVR)

by 청운추월 2023. 9. 14.
반응형
printf 처럼 uart로 출력하는 방법입니다.
 
프로그램시 디버깅을 하려면 UART로 로그를 많이 보내게 되는데
문자열로 만들어서 UART를 전송하는 방법입니다.
간단하게 아래와 같이 코딩하시면 디버깅이 편하겠죠?
 
#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif
 
#include <stdio.h>
#include <stdarg.h>
#include <avr/io.h>
#include <avr/iom128.h>
#include <util/delay.h>
 
void uart_send_byte(unsigned char byte)
{
 while(!(UCSR0A & (1 << UDRE)));
 UDR0 = byte; 
}
unsigned char usart_receive(void)
{
 while(!(UCSR0A & (1 << RXC)));
 return UDR0;
}
 
static int uart0_debug_putChar(char c, FILE *stream);
static FILE uart0_debug_out = FDEV_SETUP_STREAM(uart0_debug_putChar, NULL,_FDEV_SETUP_WRITE);
static int uart0_debug_putChar(char c, FILE *stream)
{
 uart_send_byte(c);
 return 0;
}
 
int debug_printf( const char *fmt, ...)
{
 va_list al;
 va_start(al, fmt);
 return vfprintf(&uart0_debug_out, fmt, al);
}
 
int main(void)
{
 //19200 bps
 UBRR0L = 0x33;//(unsigned char)BAUD_RATE_L; // baud rate 설정
 UBRR0H = 0x00;
 
 UCSR0A = 0x00;
 UCSR0B = 0x18;
 UCSR0C = 0x06;
 

 int count = 0;
 while(1)
 {
  debug_printf("\r\n count %d",count++); 
  _delay_ms(1000);
 }
}
반응형