查看: 999|回复: 0

Atmega328p Xplained mini-串口测试

[复制链接]
  • TA的每日心情
    开心
    2019-11-30 19:48
  • 签到天数: 981 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2015-2-2 08:45:39 | 显示全部楼层 |阅读模式
    分享到:
    建立新工程

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

    配置调试仿真口

    下载仿真结果
    回复

    使用道具 举报

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

    本版积分规则

    关闭

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

    手机版|小黑屋|与非网

    GMT+8, 2024-4-29 11:28 , Processed in 0.120024 second(s), 18 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.