In a recent project, the blogger encountered a function that required the use of USART serial port for communication. First of all, there is one very different thing between USART and UART:
UART: universal asynchronous receiver and transmitter
[Bus Signal] TX , RX
USART: universal synchronous asynchronous receiver and transmitter
[Bus Signal] TX, RX, CK
USART supports synchronous mode, so USART needs to synchronize the signal USART_CK (such as STM32 microcontroller). Usually, the synchronous signal is rarely used, so the general microcontroller UART and USART are used in the same way, both using asynchronous mode.
In the blogger's current project, communication is mainly carried out through the RS485 protocol. RS485 communication is a typical communication protocol for half-duplex asynchronous communication that is very common in industry. Its characteristics are that the communicating devices are mounted on the main line, and differential signals are used to determine the reception and transmission. At the same time, only one function (receiving or sending information) can be performed. Then the blogger's software processing method is as follows;
First, we need to imagine the basic framework of the entire project through 485 communication: define the signal port (because 485 does not have TXRX instead of UART); then initialize USART--set the baud rate, the number of data to be transmitted, enable the IO port, and set whether to set the parity bit, etc. Is there an automatic send and receive setting for signal transmission? If so, do we need to write an interrupt for complete reception and complete transmission? And so on. Different projects may have different requirements. All of these need to be thought out in advance.
(1) Due to half-duplex communication, one thing must be noted: there is always only one signal line. The difference is that the processing results of this signal line may be different at different times. It may be sending data or receiving data.
So naturally, we will think of using an IO port to define the signal port. So the blogger first defines an IO port as the signal processing port, and it is best to write the delay into it:
#define Tx PORTC.1=1;delay_ms(10) //Define C1 as the signal receiving and transmitting line
#define Rx PORTC.1=0;delay_ms(10)
(2) Set each USART register and initialize it. If CVAVR is used, the code can be automatically generated very conveniently. As shown below.
//USART initialization Universal Synchronous and Asynchronous Serial Receiver and Transmitter (USART)
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600 Data Register UDR USART and Status Register ABC
void uartinit(void)
{
UCSRA=0x00; //No speed increase
UCSRB=0xD8; //Receive completion interrupt enabled, transmit completion interrupt enabled, receive data enabled, transmit data enabled, 1101 1000
UCSRC=0x86; //8-bit data bit
UBRRH=0x00; //Configure baud rate
UBRRL=0x33;
}
After the configuration is completed, we have a basic communication frame structure, and all communication structures can be parsed and sent through this frame structure.
(3) Configure the communication send and receive interrupts. Again, when using the CVAVR software, you can naturally add the code as follows
// USART Receiver interrupt service routine serial port receive interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
u16 crctemp;
char status,data;
status=UCSRA;
data=UDR;
t0cnt=0;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
。 。
}
}
// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void) //Transmit pin interrupt function
{
if(--txcnt) //When txcnt==1
{。 。
}
else
{
。 。
RX; //Pull the signal line up.
}
}
One thing to note here is that after sending, pay attention to the state of the communication signal line. If it is in the receiving state, keep it. If it is in the state where sending has been completed, pay attention to changing the state of the signal line. The method of changing is the macro definition written by the blogger above. RX.
(4) Use of UDR
It can be clearly seen in the data sheet that UDR is a data buffer register, which plays a "leader" role when sending or receiving data. Once it is filled with the first byte of the interrupt, the data will be sent frame by frame.
Remember to hang up the signal line when sending.
Tx;
txbuf[0]=modid; //Mode selection
txbuf[1]=SBNUM; //Device model
txbuf[2]=0x02;
txbuf[3]=0x02;
txbuf[4]=0x4F;
txbuf[5]=0x4B;
crctemp=getcrc16(&txbuf[0],6);
txbuf[6]=crctemp>>8;
txbuf[7]=crctemp;
txcnt=8;
UDR=txbuf[0]; //Fill the first letter of the interrupt
Previous article:Some thoughts on parity check code for AVR ATmega8 serial data protocol transmission
Next article:Exploration of AVR Timer
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- 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
- The Android software has been unable to successfully upload the example to the board. Is this a bug?
- Based on TI Sitara Cortex-A8 ARM AM335x evaluation board serial port
- 【RT-Thread】Reading Notes (3) Critical Section Protection, Objects, and Containers
- I'm planning to buy a current probe.
- How to solve the chip shortage dilemma? Find out from Yunhan Chip City!
- Three ways to write fennel beans
- My own DIY download website, a small website placed on the router, please support
- CSR8670 - What are ANC, CVC, and DSP noise reduction?
- Battery real-time capacity test
- Share a wave of DIY reference designs for the Internet of Things