Several registers used by 51 UART
SCON:SM0 SM1 SM2 REN RB8 TX8 ONLY
PCON: SMOD -- -- -- --- --- PD IDLE
T2CON: TF2 EXF2 RCLK TCLK EXEN2 TR2 C/_T CP/_RL2
TH2,TL2
Baud rate: 9600bps
avr:atmega64's two USARTs
Registers used
Uart0 is used here, so the relevant registers should be set during initialization:
UCSR0A : RXC TXC OUTPUT UPE U2X MPCM
UCSR0C :-- UMSEL UPM1 UPM0 USBS UCSZ1 UCSZ0 UCPOL
UBRR0H 、 UBRR0L、
UCSR0B :RXCIE TXCIE HITS RXEN TXEN UCSZ2 RXB8 TXB8
Don't ask me what these mean, if you don't understand, go look it up in a book.
Proteus simulation is as follows:
The simulation results are shown in the figure below:
It should be noted that:
Because I can only select 8MHz in the CKSEL Fuse of atmega64 in the simulation, the initialization baud rate in the AVR program is calculated based on 8MHz. So simulation is simulation, pay attention to the actual application.
When the 51 MCU uses timer 1 to generate the baud rate, it is said in the book that in mode 2, the maximum can only reach 4800bps at 12MHz. Here we have to use timer 2. But it is also very useful. . The 8052 core is used in the simulation. . Otherwise, don't blame me if there is no response. .
Finally, I attached the program. Some variables are declared but not used. It was just an experiment.
51's:
#include "reg52.h"
#define AA 0x61
#define commun_symbol 0x31
sbit LED=P2^0;
unsigned char Tx[]={"my name is seven!"};
void uart_init(void)
{
SCON = 0x50;
RCAP2H = 0xFF;
RCAP2L = 0xD9;
TH2 = 0xFF;
TL2 = 0xD9;
T2CON = 0x34;
}
void uart_send(unsigned char byData)
{
TI=0;
SBUF=byData;
while ( TI == 0 ) ;
TI=1;
} }
unsigned char uart_receive(void)
{
RI=0;
while(RI==0);
RI=1;
return(SBUF);
}
void main()
{
unsigned char byBuff,i;
uart_init();
uart_send(commun_symbol);
while(1)
{
byBuff=uart_receive();
LED=1;
if(byBuff==0x31)
{
for(i=0;i<20;i++)
{
P1=byBuff;
uart_send(Tx[i]);
}
}
}
}
Program for atmega64:
Two files, one is to modularize the function, and the other is the main function, calling (- -! Recently I have become accustomed to modularizing the program...)
//------------------uart.c---------------------
//----The communication function is modularized here------------
#include
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x33; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x18;
}
void uart0_Transmit( unsigned char da
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<
/* Copy ninth bit to TXB8 */
UCSR0B &= ~(1<
//UCSR0B |= (1<
UDR0 = da
}
unsigned char uart0_Receive( void )
{
/* Wait for receiving data*/
while ( !(UCSR0A & (1<
/* Get and return data from the buffer*/
return UDR0;
}
//--------------main.c-----------
//--------------------------------
#include
#include "spi.h"
#define commun_symbol 0x31
//-----------send a commun_symbol-----
//-----------receive a commun_symbol--
// <--no,continue receive||||||yes-->receive the da
void main()
{
unsigned char bybuff;
DDRB=0xff;
PORTB=0xff;
uart0_init();
{
do
{
bybuff=uart0_Receive();
}
while (bybuff!=commun_symbol);//commun_symbol);
while(1)
{
uart0_Transmit(bybuff);
bybuff=uart0_Receive();
PORTB=(0xff|bybuff);
}
}
}
Previous article:Design of LED dot matrix display system based on STC89C55RD+ single chip microcomputer
Next article:Design of electronic password lock based on single chip microcomputer
Recommended ReadingLatest update time:2024-11-16 15:33
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- Single-chip microcomputer C language programming and simulation
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Single chip microcomputer control technology (Li Shuping, Wang Yan, Zhu Yu, Zhang Xiaoyun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- MSP430 MCU Development Record (1)
- AD, how to draw dot-dash lines on the mechanical layer?
- Capacitive touch MSP430FR2676 MCU motherboard
- ESP32 simple Bluetooth support shared by foreign netizens
- Tutorial: How to use Vivado HLS to accelerate algorithm development on FPGA
- How is the brightness of LCD segment code screen defined?
- Thank you for being there - I am grateful for the forum which has given me a lot of technical help and helped me grow very fast.
- fpga download program
- nios data transmission problem
- The embedded operating systems I have come into contact with