建立新工程 选择片子ATmega328p 添加源代码 #define F_CPU 16000000UL #define BAUD 9600 #define PUTBAUD F_CPU/16/BAUD-1 #include <avr/io.h> #include <stdio.h> #include <stdint.h> #include <util/delay.h> void Init_USART(uint16_t ubrr) { /*Set baud rate */ UBRR0H = (unsigned char)(ubrr>>8); UBRR0L = (unsigned char)ubrr; /*Enable receiver and transmitter */ UCSR0B = (1<<RXEN0)|(1<<TXEN0); /* Set frame format: 8data, 2stop bit */ UCSR0C = (1<<USBS0)|(3<<UCSZ00); } void PutChar(char c) { /* Wait for empty transmit buffer */ while ( !( UCSR0A & (1<<UDRE0)) ); /* Put data into buffer, sends the data */ UDR0 = c; } void PutStr(char *s) { while(*s)PutChar(*s++); } int thisByte; char Str[50]; int main(void) { Init_USART( PUTBAUD); while(1) { sprintf(Str,"%s"," A~Z Character Map\r\n"); PutStr(Str); for (thisByte = 'A';thisByte<='Z';thisByte++) { sprintf(Str,"%c , DEC: %d , HEX: 0x%x\r\n ",thisByte,thisByte,thisByte); PutStr(Str); } _delay_ms(2000); } } 编译通过 配置调试仿真口 下载仿真结果 最后奉上工程源码。
UART_TEST.zip
(39.51 KB, 下载次数: 15)
|