回答

收藏

Atmega328p Xplained mini-串口测试

#其他 #其他 1429 人阅读 | 0 人回复 | 2015-02-02

建立新工程

选择片子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);
    }
}
编译通过

配置调试仿真口

下载仿真结果
关注下面的标签,发现更多相似文章
分享到:
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条