ATMEGA128

ATMEGA128, CodeVisonAVR, UART, printf 사용하기

CoyoteUgly 2018. 8. 28. 23:56

ATMEGA128, CodeVisonAVR, UART, printf 사용하기



UART0
    - GND
    - RX : PE0
    - TX : PE1


0. 참고사이트

  • C:\cvavr2\examples\2USART_LCD  ( CodeVisonAVR 설치 디렉토리 )

1. USART0 > Transmitter

BaudRate : 9600

일반적으로 115200 baudrate를 쓰겠지만
하지만 이 baudrate에서는 통신 손실률이 커서 9600을 사용합니다.


2. Project > Configure


3. C Compiler > printf Features

float, width, precisor


4. 소스 설명


#include <mega128.h>

// Standard Input/Output functions
#include <stdio.h>

// Declare your global variables here

// Bit definitions from the USART control registers
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// Specify that a new putchar function will be used instead of the one from stdio.h
#define _ALTERNATE_PUTCHAR_

// Define the new putchar function
void putchar(char c)
{
    while ((UCSR0A & DATA_REGISTER_EMPTY)==0);
    UDR0=c;
}

void main(void)
{
    // Declare your local variables here
    unsigned char tmpString[] = "Good";

    // USART0 initialization
    // Communication Parameters: 8 Data, 1 Stop, No Parity
    // USART0 Receiver: Off
    // USART0 Transmitter: On
    // USART0 Mode: Asynchronous
    // USART0 Baud Rate: 9600
    UCSR0A=0x00;
    UCSR0B=0x08;
    UCSR0C=0x06;
    UBRR0H=0x00;
    UBRR0L=0x67;

    // USART1 initialization
    // USART1 disabled
    UCSR1B=0x00;
              
    // printf output will be directed to USART0
    printf("\r\nThis is a test of USART0.\r\n");
    printf("String Test1 %s\r\n", tmpString);
    printf("String Test2 %s\r\n", "Good");          // Fail!!!
    printf("Char Test %c%c%c%c\r\n", 'G', 'o', 'o', 'd');
    printf("Digit Test %d\r\n", 10);
    printf("Float Test %f\r\n", 10.123456);
   
    while (1)
    {
    // Place your code here

    }
}
 

6. 보드 연결 및 결과

printf("String Test2 %s\r\n", "Good");

이 구문을 사용하면 아래처럼 쓰레기값이 출력되는데
다른 해결책이 있는지는 모르겠습니다.