Serial communication experiment based on 51 single chip microcomputer

/**************************************************** ****************************************
*                    Serial communication experiment  *
Realization phenomenon: send to the microcontroller through the serial port Decimal number, display the hexadecimal number on the nixie tube.
Note: The range of decimal numbers is 0000~9999 , and HEX must be used to send ****************************** **************************************************** ******/ #include "reg52.h"  //This file defines some special function registers of the microcontroller typedef unsigned int u16;  //Define the data type typedef unsigned char u8; sbit LSA=P2 ^2; sbit LSB=P2^3; sbit LSC=P2^4; u16 shu,shu1,shu2; u8 a1,a2,b1,b2,num;  



















u8 code smgduan[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//Display the value of 0~F


/ **************************************************** ****************************
* Function name: delay
* Function   : delay function, when i=1, the approximate delay 10us
************************************************ ********************************/
void delay(u16 i)
{
while(i--); } /*** **************************************************** **************************** * Function name : DigDisplay * Function   : digital tube dynamic scanning function, cyclically scan 8 digital tubes to display ** **************************************************** ****************************/ void DigDisplay() {









u8 i;
for(i=0;i<8;i++)
{
switch(i) //Bit selection, select the lighted nixie tube,
{
case(0):
LSA=0;LSB=0;LSC=0; P0=smgduan[shu%4096%256%16];delay(100);P0=0x00; break;//Display the 0th digit
case(1):
LSA=1;LSB=0;LSC=0;P0=smgduan [shu%4096%256/16];delay(100);P0=0x00; break;//Display the first digit
case(2):
LSA=0;LSB=1;LSC=0;P0=smgduan[shu% 4096/256];delay(100);P0=0x00; break;//Display the second digit
case(3):
LSA=1;LSB=1;LSC=0;P0=smgduan[shu/4096];delay( 100);P0=0x00; break;//Display the third digit
case(4):
LSA=0;LSB=0;LSC=1;P0=smgduan[6];delay(100);P0=0x00; break; //Display the fourth
case(5):
LSA=1;LSB=0;LSC=1;P0=smgduan[1];delay(100);P0=0x00; break;//Display the fifth
case(6 ):
LSA=0;LSB=1;LSC=1;P0=smgduan[0];delay(100);P0=0x00; break;//Display the sixth digit
case(7):
LSA=1;LSB=1;LSC =1;P0=smgduan[0];delay(100);P0=0x00; break;//Display the 7th bit } } } /******************* **************************************************** ********** * Function name: UsartInit() * Function : Set serial port * Input: None * Output         : None ******************** **************************************************** **********/ void UsartInit() { SCON=0X50; //Set to working mode 1 TMOD=0X20; //Set counter working mode 2 PCON=0X80; //Double the baud rate TH1= 0XF3; //Set the initial value of the counter, note that the baud rate is 4800

















TL1=0XF3;
ES=1;    //Turn on receive interrupt
EA=1; //Turn on total interrupt
TR1=1; //Turn on counter
}


/****************** **************************************************** **********
* Function Name: main
* Function Function : Main Function
* Input: None
* Output     : None
******************** **************************************************** *********/
void main()
{ UsartInit(); // serial port initialization while(1) {    DigDisplay(); // digital tube display function } } /********* **************************************************** ********************









* Function name: Usart() interrupt 4
* Function : Serial communication interrupt function
* Input: None
* Output         : None
**************************** **************************************************** ****/
void Usart() interrupt 4
{
num++;
if(num==1)
{
    a1=SBUF;
RI=0;
b1=a1;
shu1=b1/16*1000+b1%16*100;
} if (num==2) {     a2=SBUF; b2=(a1<<8)|a2; RI=0; num=0; shu2=b2/16*10+b2%16; } shu = shu1+shu2;









Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325171065&siteId=291194637