ATmega328P-Xplained 串口打印Hello,world

作者: 狱锁狂龙
上传时间为: 2015-02-06 05:26 PM
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.
}
效果如下:

全部评论 ()
条评论
写评论

创建讨论帖子

登录 后参与评论
系统提示