AVR串口 getchar(),getchar(),printf()等函数的使用

时间:2016-11-26来源:网络
AVR串口 getchar(),getchar(),printf()等函数的使用

AVR单片机可以用getchar(),getchar(),printf()等函数,他们的操作默认会和串口连接起来。

需要包含stdio.h,ICC默认会包含这个文件,但是依然建议你写上这句#include
CODE:

//ICC-AVR application builder : 2007-5-25 21:35:24
// Target : M16
// Crystal: 7.3728Mhz
// 用途:演示getchar(),getchar(),printf()等函数的使用。
// 作者:古欣
// AVR与虚拟仪器 [url]http://www.avrvi.com[/url]
// 使用7.3728M外部晶振,注意:请短接跳线JP2的1和2,电源跳线连接3.3V或者5V
// 串口使用JP4的连接方法:1和2短接,使用RS232,请注意查看跳线旁边的示意图
// 开发板连接:无连线

#include
#include
#include

//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x2F; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x18;
}

void main(void)
{
unsigned char i;
uart0_init(); //初始化串口
printf("[url]www.avrvi.com[/url]rn"); //输出字符串
while(1)
{
i=getchar(); //读入一个字符
putchar((signed char)i); //输出字符
if(i==a) printf("OKrn");//如果输入的是a,输出OK,加回车换行。
if(i==A) printf("GOODt");//如果输入的是A,输出GOOD,加一个TAB符号。
}
}
[Copy to clipboard]
串口调试助手中的效果如下图




关键词: AVR串口getchar()printf(

加入微信
获取电子行业最新资讯
搜索微信公众号:EEPW

或用微信扫描左侧二维码

相关文章

查看电脑版