Use of AVR serial port getchar(), getchar(), printf() and other functions

Publisher:会弹琴的鲸鱼3312Latest update time:2015-10-12 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Use of AVR serial port getchar(), getchar(), printf() and other functions

AVR microcontrollers can use getchar(), getchar(), printf() and other functions, and their operations are connected to the serial port by default.

You need to include stdio.h. ICC will include this file by default, but it is still recommended that you write this sentence #include
 
CODE:

//ICC-AVR application builder : 2007-5-25 21:35:24
// Target : M16
// Crystal: 7.3728Mhz
// Purpose: Demonstrate the use of getchar(), getchar(), printf() and other functions.
// Author: Gu Xin
// AVR and Virtual Instruments [url]http://www.avrvi.com[/url]
// Use 7.3728M external crystal oscillator, note: please short jumper JP2 1 and 2, power jumper connected to 3.3V or 5V
// Serial port connection method using JP4: 1 and 2 short, use RS232, please pay attention to the schematic diagram next to the jumper
// Development board connection: no connection

#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();            //Initialize the serial port
printf("[url]www.avrvi.com[/url]rn"); //Output string
while(1)
{
i=getchar();            //Read a character
putchar((signed char)i);    //Output character
if(i=='a') printf("OKrn");//If the input is a, output OK, add a carriage return and a line feed.
if(i=='A') printf("GOODt");//If the input is A, output GOOD, add a TAB symbol.
}
}
[Copy to clipboard]
The effect in the serial port debugging assistant is as follows


[Reprint] AVR serial port <wbr>getchar(), getchar(), printf() and other functions


Reference address:Use of AVR serial port getchar(), getchar(), printf() and other functions

Previous article:PWM application of 16-bit timer of ATMage16
Next article:M8 and CX20106A ultrasonic distance measurement

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号