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
//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.
}
}
Reference address: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();
printf("[url]www.avrvi.com[/url]rn"); //Output string
while(1)
{
i=getchar();
putchar((signed char)i);
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
Previous article:PWM application of 16-bit timer of ATMage16
Next article:M8 and CX20106A ultrasonic distance measurement
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
Guess you like
- WB-MQTT Debug Assistant
- Image storage and display system based on SDRAM——Study record
- I stayed up all night again, my friend's mood or my own health
- The concept of channel in AD conversion
- Announcement: Participants in the second round of drawing for 11.11 yuan to purchase a multimeter, soldering station, and development board
- LCD1602 dynamic display--based on MSP430F149 single chip microcomputer
- Operating handle failure
- In-depth reveal of how many ground vias are needed next to the signal hole
- Electronic Design Tips
- [First come first served double gift] Become the first 100 pioneers to experience the Fluke New Era 640 Infrared Thermal Imager