Serial communication program writing steps
UART communication program can use query, interrupt and DMA mode. We introduce the writing of UART communication program by using more interrupt methods. The simple way is to refer to the example program for writing UART communication program.
Select the channel through the function Uart_Select(); select UART0~UART2;
Select the baud rate and baud rate generator clock. The baud rate is selected through the function Uart_Pclk_En(int ch, int baud) or Uart_Pclk_En(int ch, int baud). The clock is UCLK, rUCON0|=0x400; the clock is PCLK, rUCON0&=0x3ff.
Communication protocol (rULCON0) setting, if normal communication, one stop bit, 8 data bits, no parity check: rULCON0=(0<<6)|(0<<3)|(0<<2)|(3);
Communication control word (rUCON0) settings, such as clock selection ULK as baud rate generator; Tx interrupt pulse trigger, Rx interrupt pulse trigger; receive timeout interrupt enabled; generate receive error interrupt; send in normal mode:
rUCON0|=(TX_INTTYPE<<9)|(RX_INTTYPE<<8)|(0<<7)|(0<<6)|(0<<5)|(0<<4)|(1<<2)|(1);
I/O port initialization, because UART communication uses the second function of H port, so H port pull-up is disabled: rGPHUP|=0x1ff. H port control register nRTS1, nCTS1 function enable, rGPHCON&=0x3c0000, rGPHCON|=0x2faaa;
Set the interrupt service function entry address, assign the interrupt service function entry address to the function pointer PISR_UARTn, note that the receiving interrupt service function entry address and the sending interrupt service function entry address are the same, in the interrupt service function according to
The status of UTRSTATn[1] and UTRSTATn[0] determines whether it is a transmit interrupt or a receive interrupt.
Open the overall interrupt mask and sub-interrupt mask and wait for the interrupt:
rINTMSK=~(BIT_UART0);
rINTSUBMSK=~(BIT_SUB_TXD0);
After entering the interrupt, first shield the sending and receiving interrupts to prevent new interrupts from interfering with our normal sending and receiving. After the normal sending and receiving are completed, clear the interrupt pending and interrupt source pending registers: ClearPending(BIT_UART0), rSUBSRCPND=(BIT_SUB_TXD0 (sending), rSUBSRCPND=(BIT_SUB_RXD0|BIT_SUB_ERR0) (receiving);
Cancel interrupt mask and wait for the next interrupt.
The following is a serial communication program using the query method (FL2440 development board)
#include "2440addr.h" // This program is a PC that sends the four numbers 1234 to the development board through the serial port tool to control the on and off of the four LEDs
int TSmain()
{
char buf,i;
rULCON0 &=0XFFFFFF00;
rULCON0 |= 0X03; //1 start bit, 8 data bits
rUCON0 = 0x05; //0X0805; //Serial port clock PCLK, query method East: PCLK is 50M
rUBRDIV0 =325; //0X1A; //Baud rate 115200****325 is set to 9600
rGPBCON = 0x1dd7fc; //GPB5,6,8,10 are set to output
rGPBDAT|=0x560; //4 LEDs are all off
while(1)
{
if(rUTRSTAT0 & 0X01) //Is receiving completed? =1 End
{
buf=rURXH0; //Read data
while(!(rUTRSTAT0 & 0X04));//Is sending allowed? = 1: Allowed
rUTXH0=buf;
if(buf=='1')//Judge which number is received
i=1;
else if(buf=='2')
i=2;
else if(buf=='3')
i=3;
else if(buf=='4')
i=4;
switch(i){ //Turn the corresponding LED on and off
case 1:
rGPBDAT^=(1<<5);
i=0; // Clear i to prevent interference when receiving other data next time
break;
case 2:
rGPBDAT^=(1<<6);
i=0;
break;
case 3:
rGPBDAT^=(1<<8);
i=0;
break;
case 4:
rGPBDAT^=(1<<10);
i=0;
break;
default:break;
}
}
}
return 0;
}
The following is a serial communication program using interrupts
#include "2440addr.h"
void __irq UART0RX_isr()
{
char buf,i;
rINTMSK=0xffffffff;
ClearPending(BIT_UART0);
if(rUTRSTAT0 & 0X01) //Is receiving completed? =1 End
{
ClearSubPending(BIT_SUB_RXD0);
buf=rURXH0; //Read data
while(!(rUTRSTAT0 & 0X04));//Is sending allowed? = 1: Allowed
rUTXH0=buf;
if(buf=='1')
i=1;
else if(buf=='2')
i=2;
else if(buf=='3')
i=3;
else if(buf=='4')
i=4;
switch(i){
case 1:
rGPBDAT^=(1<<5);
i=0; // Clear i to prevent interference when receiving other data next time
break;
case 2:
rGPBDAT^=(1<<6);
i=0;
break;
case 3:
rGPBDAT^=(1<<8);
i=0;
break;
case 4:
rGPBDAT^=(1<<10);
i=0;
break;
default:break;
}
}
EnableIrq(BIT_UART0);
EnableSubIrq(BIT_SUB_RXD0);
EnableIrq(BIT_EINT0|BIT_EINT2|BIT_EINT3|BIT_EINT4_7);
}
static void __irq Key_ISR()
{
char key; // used to identify which key is pressed
//EnterCritical(&r);
rINTMSK=0xffffffff;
if(rINTPND==BIT_EINT0) {
ClearPending(BIT_EINT0);
key=1;
}
else if(rINTPND==BIT_EINT2) {
ClearPending(BIT_EINT2);
key=2;
}
else if(rINTPND==BIT_EINT3) {
ClearPending(BIT_EINT3);
key=3;
}
else if(rINTPND==BIT_EINT4_7){
rEINTPEND=(1<<4);
ClearPending(BIT_EINT4_7);
key=4;
}
switch(key){
case 1:
rGPBDAT^=(1<<5);
break;
case 2:
rGPBDAT^=(1<<6);
break;
case 3:
rGPBDAT^=(1<<8);
break;
case 4:
rGPBDAT^=(1<<10);
break;
}
//ExitCritical(&r);
EnableIrq(BIT_EINT0|BIT_EINT2|BIT_EINT3|BIT_EINT4_7);
EnableIrq(BIT_UART0);
EnableSubIrq(BIT_SUB_RXD0);
}
int TSmain()
{
rULCON0 &=0XFFFFFF00;
rULCON0 |= 0X03; //1 start bit, 8 data bits
rUCON0 = 0x05; //0X0805; //Serial port clock PCLK, query method East: PCLK is 50M
rUBRDIV0 =325; //0X1A; //Baud rate 115200****325 is set to 9600
rGPHUP=0x1ff; //H port pull-up disabled
rGPHCON&=0x3c0000;
rGPHCON|=0x2faaa;
rGPBCON = 0x1dd7fc; //GPB5,6,8,10 are set to output
rGPBDAT|=0x560; //4 LEDs are all off
rGPFCON &=~((3<<0)|(3<<4)|(3<<6)|(3<<8)) ;
rGPFCON |= ((2<<0)|(2<<4)|(2<<6)|(2<<8)) ; //GPF0, GPF2, GPF3, GPF4 work in the second functional state, i.e., interrupt
rEINTPEND=(1<<4);
ClearPending(BIT_EINT0|BIT_EINT2|BIT_EINT3|BIT_EINT4_7);
ClearSubPending(BIT_SUB_RXD0);
ClearPending(BIT_UART0);
pISR_EINT0= pISR_EINT2 = pISR_EINT3 = pISR_EINT4_7=(int)Key_ISR;
EnableIrq(BIT_EINT0|BIT_EINT2|BIT_EINT3|BIT_EINT4_7);
EnableIrq(BIT_UART0);
EnableSubIrq(BIT_SUB_RXD0);
rEINTMASK=~(1<<4);
pISR_UART0=(unsigned) UART0RX_isr;
while(1)
{
}
return 0;
}
Previous article:(II) s3c2440——Serial port experiment
Next article:All the problems of S3C2440A ARM learning
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- The State of Open Source Hardware in 2021
- Implementation of HPI Boot Mode in TMS320C62x
- What is the physical meaning of Gauss's formula in advanced mathematics?
- 【Portable Environmental Status Detector】Compile and download function test
- Learn about TI 15.4-Stack's support for 470M frequency band
- 【Me and Yatli】+My impression of Yatli
- Advanced Algebra Concise Course
- [RT-Thread reading notes] Three timers
- AP6212 driver all firmware & nvram
- Bosch air pressure sensor gives sweeping robots more monitoring functions