查看: 1097|回复: 0

ATmega328P-Xplained 串口打印Hello,world

[复制链接]
  • TA的每日心情
    开心
    2014-10-12 13:06
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    发表于 2015-2-6 17:26:06 | 显示全部楼层 |阅读模式
    分享到:
    看到zhzhx66发了一个串口测试程序,自己也亲身体验了一下,代码如下:其实是atmega328 Datasheet程序:


    源代码:
    #define F_CPU 16000000UL        // Clock Speed#include <stdio.h>#include <stdint.h>#include <avr/io.h>#include <util/delay.h>#define BAUD 9600                        // Baud Speed#define MYUBRR F_CPU/16/BAUD-1        void USART_Init( unsigned int 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 USART_Transmit( unsigned char data ){        /* Wait for empty transmit buffer */        while ( !( UCSR0A & (1<<UDRE0)) )        ;        /* Put data into buffer, sends the data */        UDR0 = data;}void PutStr(char *s){        while(*s)        {                USART_Transmit(*s++);        }}int main (void){        // Insert system clock initialization code here (sysclk_init()).        char str[]="\r\nHello,world!\r\n";        USART_Init(MYUBRR);                while(1)        {                PutStr(str);        }                // Insert application code here, after the board has been initialized.}效果如下:
    回复

    使用道具 举报

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

    本版积分规则

    关闭

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

    手机版|小黑屋|与非网

    GMT+8, 2024-4-29 05:04 , Processed in 0.117898 second(s), 18 queries , MemCache On.

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

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.