Joke Collection Website - Blessing messages - How to transmit the key signal of 5 1 single chip microcomputer experimental board to PC? For example, if you press a key, 5 1 will send an "A" character to the PC.

How to transmit the key signal of 5 1 single chip microcomputer experimental board to PC? For example, if you press a key, 5 1 will send an "A" character to the PC.

Hello, classmate, let me help you. You can use a serial port, such as 1: to give you a reference code for sending data from a PC to an MCU, and then the MCU sends it to the PC as it is: 2: Send a certain (pin) button of 5 1 to the PC;

1::::::::::::::

# include & ltREG52。 H & gt

Bit flag;

Unsigned integers R_D, S _ D;;

Unsigned character I;

void usart _ init(void); ///serial port initialization

Invalid primary (invalid) {

usart _ init(void); ///serial port initialization

while( 1)

{

if (Flag== 1)

{

SBUF = S _ D; //SUBF receive/send buffer (also called serial communication special function register)

And (! TI); //Equidistant data transmission (TI sending interrupt flag)

TI = 0; //Clear the data transmission flag

flag = 0;

}

}

}

}

Invalid ser_int (void) interrupt 4 with 1

{

If (ri =1)//ri accepts the interrupt flag.

{

RI = 0; //Clear the RI accept interrupt flag.

R _ D = SBUF//SUBF receive/send buffer.

S _ d = r _ d////Return to PC to send.

flag = 1;

}

}

Void usart_init(void)/// serial port initialization

{

SCON = 0x 50; //REN= 1 allows the serial port to accept the status, and the serial port working mode is 1.

TMOD | = 0x 20; //Timer working mode 2

PCON | = 0x 80;

th 1 = 0x F3; ////Porter *2 /* Baud rate 4800, data bit 8, stop bit 1. Invalid (12M)

TL 1 = 0x F3;

tr 1 = 1;

ES = 1; //Open serial port interrupt

EA = 1; //Open host interrupt

}

2:::::::::::::

# include & ltREG52。 H & gt

sbit p34=p3^4; /////Define a key.

Bit flag =1;

Unsigned integer S _ D;;

Unsigned character I;

void usart _ init(void); ///serial port initialization

Invalid delay (unsigned integer z); //delay program z times milliseconds

void key _ p34(void); ////key check

Invalid primary (invalid) {

usart _ init(void); ///serial port initialization

key _ p34(void); ////Check the key status once after power-on.

If (Flag== 1)// The state of sending the key once after power-on.

{

SBUF = 1; //SUBF receive/send buffer (also called serial communication special function register)

And (! TI); //Equidistant data transmission (TI sending interrupt flag)

TI = 0; //Clear the data transmission flag

}

other

{

SBUF = 0; //SUBF receive/send buffer (also called serial communication special function register)

And (! TI); //Equidistant data transmission (TI sending interrupt flag)

TI = 0; //Clear the data transmission flag

}

while( 1)

{

key _ p34(void);

if (Flag== 1)

{

SBUF = 1; //SUBF receive/send buffer (also called serial communication special function register)

And (! TI); //Equidistant data transmission (TI sending interrupt flag)

TI = 0; //Clear the data transmission flag

}

other

{

SBUF = 0; //SUBF receive/send buffer (also called serial communication special function register)

And (! TI); //Equidistant data transmission (TI sending interrupt flag)

TI = 0; //Clear the data transmission flag

}

}

}

Void usart_init(void)/// serial port initialization

{

SCON = 0x 50; //REN= 1 allows the serial port to accept the status, and the serial port working mode is 1.

TMOD | = 0x 20; //Timer working mode 2

PCON | = 0x 80;

th 1 = 0x F3; ////Porter *2 /* Baud rate 4800, data bit 8, stop bit 1. Invalid (12M)

TL 1 = 0x F3;

tr 1 = 1;

ES = 1; //Open serial port interrupt

EA = 1; //Open host interrupt

}

Void delay (unsigned int Z)// Delay program z times ms.

{

Unsigned integers x, y;

for(x = Z; x & gt0; x -)

for(y = 1 10; y & gt0; y-);

}

Voidkey _ p34 (void)///key detection

{

If (p34==0)

{Delay (10);

If (p34==0)

{

And (! p34); ////Wait for the button to be released.

flag = 0;

}

}

}

I don't know how to welcome communication.