Today, I will write down the serial port receiving and sending experiment. The inspiration of the experiment comes from the study of the Bluetooth module receiving and sending. When we use Bluetooth in the circuit, we actually regard Bluetooth as a transfer station. In the transparent transmission mode, this transfer station will not make any changes to the received data, but directly forward it.
Well, we just need to know that the microcontroller serial port transmission and the PC keyboard input are in the form of character ASCII code, and the PC receives and displays it in the form of normal characters.
Right now:
Keyboard typed characters (ASCII code form) -> MCU received characters (ASCII code form)
Press 3, actually type 0x33–> MCU reads buff and receives message 0x33
MCU sends characters (ASCII format) -> simulates PC receiving characters (normal format display)
The MCU sends character 3, which actually writes buff and sends 0x33–> The simulated PC displays 3 after receiving it
Without further ado, here is the simulation circuit diagram:
Code:
main.c
#include #include #define uint unsigned int #define uchar unsigned char #define ulong unsigned long sbit LSA=P2^2; sbit LSB=P2^3; sbit LSC=P2^4; //38 decoder chip select bit lanya = 0 ; //Bluetooth sending flag, MCU sends to PC uchar LanYa_DATA_count=0; //Used to assign values to each digit of the LnaYa_receive[] array, equivalent to i in [i] uchar LanYa_receive[4]; //LanYa serial port receives data cache uint Receive_Data; //The integrated data received by the MCU must be uint, because uchar cannot exceed 255 uchar Display_Data[3]; //Process the data buffer array to be displayed on the digital tube uchar Computer[4]; //Array to be sent to PC uchar T0RH = 0; // used for the following TH0 assignment uchar T0RL = 0; // used for the following TL0 assignment uchar UART_data; /*Set an intermediate variable to get the data read from sbuf, which will be used in the serial port interrupt service function*/ uchar code smgduan[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79}; /******************************************************** Function name: void delay(uint i) Function: Delay function Parameter Description: ************************************************************/ void delay(uint i){ //delay function while(i--); } /******************************************************** Function name: ConfigUart(uint baud) Function: Serial port initialization function Parameter description: baud: baud rate to be set ************************************************************/ void ConfigUart(uint baud){ SCON = 0x50; //Configure the serial port: Working mode 1, can both receive and receive TMOD |= 0x20; //Configure timer T1 to work mode 2: automatic reload; the purpose of using T1 is to be part of the baud rate generation TH1 = 256 - (11059200/12/32)/baud; //Use the formula and add the parameters to calculate the T1 reload value TL1 = TH1; //initial value equals reload value ET1 = 0; //Disable timer T1 interrupt (because it is part of the baud generator) TR1 = 1; //Start T1, even if T1 EA=1; //Open the general interrupt ES=1; //Open serial port interrupt } /******************************************************** Function name: send_char_com(uchar ch) Function: Use the serial port to send a character. What the serial port receives is actually 8-bit ASCII, that is, a character Parameter description: uchar ch: character to be sent ************************************************************/ void send_char_com(uchar ch) { SBUF=ch; //Send sbuf, send character ch while(TI==0); //TI is set to 1 by hardware to indicate successful transmission, and exit the infinite loop TI=0; //The software sets TI to zero and then prepares for the next transmission } /******************************************************** Function name: send_string_com(unsigned char *str,unsigned int strlen) Function: Send string via serial port Parameter description: unsigned char *str: pointer points to the first address of the string, unsigned int strlen: the length you want to send (the number of characters in this string) ************************************************************/ void send_string_com(uchar *str,uint strlen){ uint k=0; do { send_char_com(*(str + k)); /*This should be the first address + offset of the pointer*/ k++; } while(k < strlen); } /******************************************************** Function name: void ConfigTimer0(unsigned int ms) Function: Timer 0 initialization function, ms level timer overflow interrupt Parameter description: unsigned int ms: Set the number of ms for the timer overflow ************************************************************/ void ConfigTimer0(uint ms){ ulong tmp; tmp = 11059200/12; tmp = (tmp * ms)/1000; tmp = 65536 - tmp; tmp = tmp + 18; EA = 1; T0RH = (uchar)(tmp >> 8); T0RL = (uchar)tmp; //Just use the above lines directly TMOD &= 0xF0; TMOD |= 0x01; //Set TMOD timer 0, mode 1, need to reload the initial value TH0 = T0RH; TL0 = T0RL; ET0 = 1; //Enable the interrupt of timer 0 TR0 = 1; // Enable timer 0 } /******************************************************** Function name: void display() Function: data processing and display Parameter Description: None ************************************************************/ void display(){ uchar i; Display_Data[0]=smgduan[Receive_Data/100]; Display_Data[1]=smgduan[Receive_Data%100/10] | 0x80; //0x80 is to add a decimal point Display_Data[2]=smgduan[Receive_Data%10]; for(i=0;i<3;i++){ //First select the bits separately, loop and switch implementation switch(i){ case 0: LSA=0;LSB=0;LSC=0;break; case 1: LSA=1;LSB=0;LSC=0;break; case 2: LSA=0;LSB=1;LSC=0;break; } P0=Display_Data[2-i]; //After selecting the bit, transfer the segment selection data delay(100); //delay 1ms P0=0x00; //Blanking } } /******************************************************** Function name: void main() Function: data processing and display Parameter Description: None ************************************************************/ void main(){ //Main program uchar i; ConfigUart(9600); //Set the baud rate to 9600 ConfigTimer0(5); //Timer T0 5ms while(1){ for(i=0;i<4;i++){ Computer[i]= LanYa_receive[i]; } dispiay(); //Display on digital tube if(lanya==1&&(UART_data=='r')){//Bluetooth flag is set to 1, the microcontroller sends, and the read buff receives a carriage return character (indicating that the PC has finished sending) send_string_com(Computer,4); //Send the data received from PC to PC send_char_com('r'); //Send r, indicating line break lanya = 0; //Bluetooth flag cleared, MCU transmission ends } } } /******************************************************** Function name: void InterruptTimer0() interrupt 1 Function: Timer 0 interrupt service routine Parameter Description: None ************************************************************/ void InterruptTimer0() interrupt 1{ static uchar tmr1s = 0; TH0 = T0RH; //Reload the reload value TL0 = T0RL; tmr1s++; if(tmr1s >= 200) //This tmr1s variable is buffered { tmr1s = 0; lanya = 1; //Set the "lanya" flag to 1, and prepare the MCU to send data in main } } /******************************************************** Function name: void uart(void) interrupt 4 Function: Serial port interrupt service routine Parameter Description: None ************************************************************/ void uart(void) interrupt 4 { uchar a,b,c; if(RI) { UART_data=SBUF; if(UART_data=='r'){ //If the Enter key is detected, Receive_Data is obtained and displayed on the digital tube through the display function (the number sent by the PC to the MCU) a = LanYa_receive[0]-0x30; /*The reason why these numbers are all subtracted by 0x30 is that in microcomputer theory, the numbers 0-9 correspond to ASCII 30H-39H The ASCII code for letters AZ is 41H-5AH, and the ASCII code for letters az is 61H-7AH Subtracting 30H will get the correct number displayed*/ b = LanYa_receive[1]-0x30; c = LanYa_receive[3]-0x30; Receive_Data = a*100+b*10+c; LanYa_DATA_count=0; } else{ //Otherwise, store the string sent by PC into LanYa_receive[] array character by character LanYa_receive[LanYa_DATA_count]=UART_data; LanYa_DATA_count++; } } RI=0; } Simulation results Type ** on the keyboard. Digital tube display*.* The PC displays **.* and displays it in a loop until a new number is entered.
Previous article:51 MCU Experiment 1 - Serial Communication (TX/RX)
Next article:Xiaobai learns about HC-05 Bluetooth transparent transmission module
- 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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- DSP Learning (4) Bus Structure
- Vehicle LIN communication error frame problem
- [Technical Tips] It turns out that the ARM+Linux audio solution is so simple!
- Selling some development boards, both new and used.
- FAQ Bluetooth transmission questions and answers
- How to enable TI 15.4-Stack to support 470M frequency band
- What is millimeter wave?
- Free Download | Maxim Reference Design Download for Industrial Applications
- Analysis of common causes of copper shedding in PCB factories
- 【micropython】Bluetooth BLE routine