Summary of UART communication of AVR microcontroller ATMega16

Publisher:自在自由Latest update time:2015-03-11 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include

#include

typedef unsigned char uint8_t;


#define DF_Config_Uart0_BaudRate 9600

//UART0 initialization
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
void uart0_init(void)
{
 UCSRB = 0x00; //disable while setting baud rate
 UCSRA = 0x00;
 UCSRC = BIT(URSEL) | 0x06;

 //Configure baud rate
#if DF_Config_Uart0_BaudRate==9600

//----------11.0592M 9600kbps:
 UBRRL = 0x47; //set baud rate lo
 UBRRH = 0x00; //set baud rate hi
 
#else if DF_Config_Uart0_BaudRate==19200

 //----------11.0592M 19200kbps:
 UBRRL = 0x23; //set baud rate lo
 UBRRH = 0x00; //set baud rate hi

#endif

 UCSRB = 0x98;
}


// Send a byte
void uart0_sendByte(uint8_t dat)
{
  while(!(UCSRA&(1<   UDR=dat; //Send data
}

// Send multiple bytes of data
void uart0_sendData(uint8_t *pDat,uint8_t nCount)
{
uint8_t i;
uint8_t *p = pDat;
for (i=0; i {
uart0_sendByte(*p++);
}
}
// Send string
void uart0_sendString(uint8_t *pDat)
{
uint8_t *p = pDat;
while(*p)
{
uart0_sendByte(*p++);
}
}

// Serial port receive interrupt
#pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC
void uart0_rx_isr(void)
{
 //uart has received a character in UDR
 #if 1 // For test
  uint8_t dat=UDR;
  uart0_sendByte(dat);
 #else
 
 #endif
}

/*-----------------------------------------------------------------
Function name: void uart_receive(void)
Function function: Query mode, receive data
Parameter: 
Return value: None
-----------------------------------------------------------------*/
uint8_t uart0_receive(void) //Define the return value type, otherwise an error
{
  while(!(UCSRA&(1<   return UDR; //Get and return the received data
}

/*

Reference address:Summary of UART communication of AVR microcontroller ATMega16

Previous article:ATMEGA16 read and write iic (24c02) C language program test passed
Next article:How to define string constants in AVR assembler

Recommended ReadingLatest update time:2024-11-16 19:25

What is the difference between Atmega16 and 51 microcontrollers? Analysis from both software and hardware aspects
1. Introduction to Atmega16 microcontroller Today our topic is microcontrollers. When you talk about microcontrollers, you may first think of the 51 microcontroller, and then use external digital circuits and analog circuits to control peripheral hardware, such as PWM, ADC, I2C, etc. So what are we talking about today
[Microcontroller]
What is the difference between Atmega16 and 51 microcontrollers? Analysis from both software and hardware aspects
ATMEGA16 driver 1602 LCD program
The following table shows   the hardware connection between 16*2 LCD and  MCU : 1 VSS Grounding 2 V CC Connect to power supply , +5V 3 VO Adjust the visibility, ground it 4 RS Register selection, 1-data, 0-instruction 5 R/W Read/write selection, 1-read, 0-write. If the LCD function does not use
[Microcontroller]
Intelligent air conditioner remote controller based on Atmega16 microcontroller
  1 Introduction   In recent years, computer technology, modern communication technology and automatic control technology have developed rapidly. With the development of new technologies, intelligent home systems have also entered thousands of households. Home systems such as air conditioners, televisions, lighting
[Microcontroller]
Intelligent air conditioner remote controller based on Atmega16 microcontroller
Homemade AVR ATmega16 JTAG
Summary of homemade AVR JTAG: Collect detailed information, understand its principle, and determine a production plan. Then make sure the circuit is accurate and learn to use related software. The picture below is my prototype. I use one ATMEGA16 as JTAG controller, connected to the computer via 232 serial port. Anoth
[Microcontroller]
Homemade AVR ATmega16 JTAG
Design of serial ADC interface based on SPI of ATmega16 microcontroller
1. SPI interface of AVR microcontroller SPI (Serial Peripheral Interface) bus system is a synchronous serial peripheral interface that allows MCU to communicate and exchange data with various peripheral devices in serial mode. It is widely used in various industrial control fields. Based on this standard, the
[Microcontroller]
Design of serial ADC interface based on SPI of ATmega16 microcontroller
Atmega16 Timer 2 usage
//According to experience, the following program is correct and can make the LED light on for 1s and off for 1s #include avr/io.h #include avr/interrupt.h #define uchar unsigned char #define uint unsigned int uint count; void init() {  DDRB|=_BV(PB4)|_BV(PB5)|_BV(PB6)|_BV(PB7);  PORTB&=~(_BV(PB4)|_BV(PB5)|_B
[Microcontroller]
ATmega16 Development Board Tutorial (4) - Timer
5. Timer Here we mainly write the timing function of timer 0 and 1, which is displayed through the digital tube. Timer T0 First of all, T0 is an 8-bit timer, which is mainly composed of TCNT0 (initial value register) and TCCR0 (control register). TCCR0:FOC0,WGM00,COM01,COM00,WGM01,CS02,CS01,CS00. As shown in the
[Microcontroller]
ATmega16 Development Board Tutorial (4) - Timer
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号