Proteus simulation experiment of single chip microcomputer communication
[Copy link]
This experiment is to set eight buttons to be pressed on the upper computer, and the lower computer will execute after receiving the data. Control eight LED lights respectively. There are some introductions about the use of ASCII displayable characters. I changed the value of the lower display character from 1=31. I changed it to /=2F... It is easy to understand. You can try to change it to other things.
The simulation schematic diagram is as follows.
The source code of the microcontroller is as follows:
/********************************************************************
Function: Send one byte of data to the serial port (can be displayed through the hyperterminal, the baud rate of the hyperterminal is selected as 9600).
After the hyperterminal is set up, each time the S2/S3/S4/S5 key is pressed, abcd will be displayed, and after releasing it, a letter "a" will be displayed on the hyperterminal.
Entry parameter: d: byte data to be sent.
********************************************************************/
#include <reg52.h>
#define jingzhen 11059200UL /*Use 22.1184M crystal*/
#define botelv 9600UL /*Baud rate is defined as 9600*/
//Define the characters to be displayed abcd
unsigned char zifua='/'; //Character a to be displayed.
unsigned char zifub='2'; //Character to be displayed.
unsigned char zifuc='3'; //Character to be displayed.
unsigned char zifud='4'; //Character to be displayed.
unsigned char zifue='5'; //Character to be displayed.
unsigned char zifuf='6'; //Character to be displayed.
unsigned char zifug='7'; //Character to be displayed.
unsigned char zifuh='8'; //Character to be displayed.
volatile unsigned char sending;
//Set eight independent buttons
sbit s1=P2^0; //button
sbit s2=P2^1; //button
sbit s3=P2^2; //button
sbit s4=P2^3; //button
sbit s5=P2^4; //button
sbit s6=P2^5; //button
sbit s7=P2^6; //button
sbit s8=P2^7; //button
//Set for delay function
void delay(unsigned char i)
{
unsigned char j,k;
for(j=i;j>0;j--)
for(k=90;k>0;k--);
}
void init(void) //Serial port initialization
{
EA=0; //Temporarily disable interrupt
TMOD&=0x0F; //Timer 1 mode control in high 4 bits
TMOD|=0x20; //Timer 1 works in mode 2, automatic reload mode
SCON=0x50; //Serial port works in mode 1
TH1=256-jingzhen/(botelv*12*16); //Calculate timer reload value
TL1=256-jingzhen/(botelv*12*16);
PCON|=0x80; //Double the serial port baud rate
ES=1; //Serial interrupt enable
TR1=1; //Start timer 1
REN=1; //Allow receiving
EA=1; //Allow interrupts
}
//Send a byte of data, parameter d is the data to be sent.
void send(unsigned char d)
{
SBUF=d; //Write data to the serial port buffer
sending=1; //Set the send flag
while(sending); //Wait for sending to complete
}
void main()
{
init();
while(1)
{
if(s1==0) //If S2 is pressed
{
delay(20); //Delay
if(!s1) //Judge whether it is pressed
{
while(!s1); //Loop to check whether S2 is pressed and execute the next one
send(zifua); //Send a
}
// else(s1==1);
}
if(s2==0)
{
delay(20);
if(!s2)
{
while(!s2);
send(zifub);
}
}
if(s3==0)
{
delay(20);
if(!s3)
{
while(!s3);
send(zifuc);
}
}
if(s4==0)
{
delay(20);
if(!s4)
{
while(!s4);
send(zifud);
}
}
if(s5==0)
{
delay(20);
if(!s5)
{
while(!s5);
send(zifue);
}
}
if(s6==0)
{
delay(20);
if(!s6)
{
while(!s6);
send(zifuf);
}
}
if(s7==0)
{
delay(20);
if(!s7)
{
while(!s7);
send(zifug);
}
}
if(s8==0)
{
delay(20);
if(!s8)
{
while(!s8);
send(zifuh);
}
}
}
}
void uart(void) interrupt 4 //Serial port sending interrupt
{
if(RI) //Received data
{
RI=0; //Clear interrupt request
}
else //After sending one byte of data
{
TI=0;
sending=0; //Clear sending flag
}
}
|