atmega328p use printf

</pre><p>I can't use chinese in my archlinux, so....</p><p>This is my source code:</p><pre name="code" class="cpp">#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>


#define BAUDR0ATE	9600

#define BIT(bit)	(1 << bit)
#define SET(ctl, bits)	(ctl |= bits)
#define CLR(ctl, bits)	(ctl &= ~bits)
#define WAIT_USART()	while ( !( UCSR0A & BIT(UDRE0) ) )


int fputc(int data,FILE *f)  
{  
	WAIT_USART();
	UDR0 = (data=='\r') ? '\n' : data;

	return data;
}  
  

void USART_config()
{
	// 设置波特率UBRR
	UBRR0L = F_CPU/16/BAUDR0ATE -1;
	UBRR0H = (F_CPU/16/BAUDR0ATE -1)>>8;

	// 设置USART
	SET(UCSR0B, BIT(RXEN0)+BIT(TXEN0)+BIT(RXCIE0) );
	
	sei();	// 开启中断
}

int main() 
{
	fdevopen((int (*)(char, FILE*))fputc,0);
	USART_config();
	printf("Hello World!\n");

	while (1);

}

ISR(USART_RX_vect)
{
	putchar(UDR0);

}


Welcome to minicom 2.7

OPTIONS: I18n
Compiled on Sep  6 2015, 19:49:19.
Port /dev/ttyUSB0, 15:06:00

Press CTRL-A Z for help on special keys

Hello World!


That's all!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值