回答

收藏

ATmega328P-Xplained 串口打印Hello,world

#其他 #其他 1606 人阅读 | 0 人回复 | 2015-02-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.}效果如下:
关注下面的标签,发现更多相似文章
分享到:
回复

使用道具 举报

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

本版积分规则

关闭

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