Serial port receiving and sending experiment - type the number **.* on the keyboard, and the PC will receive and display **.* in a loop

Publisher:Amy啊111111Latest update time:2022-01-18 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Reference address:Serial port receiving and sending experiment - type the number **.* on the keyboard, and the PC will receive and display **.* in a loop

Previous article:51 MCU Experiment 1 - Serial Communication (TX/RX)
Next article:Xiaobai learns about HC-05 Bluetooth transparent transmission module

Latest Microcontroller Articles
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号