|
I use the serial port debugging assistant on my PC to send 1 to the microcontroller, but the display shows 0xF4, the send 2 shows 0xF5, the send 3 shows 0xF5, the send 4 shows 0xF4, and I think that 1 should be displayed as 0x01, and 2 is 0x02 (I assigned the received data to the P2 port and displayed it with a light-emitting diode) The relevant procedure is as follows. This procedure should be okay.
#include <reg51.h>
#include <string.h>
unsigned char ch;
void init_serialcomm ()
{
SCON = 0x50; // serial mode 1,8-bit UART, enable ucvr
TMOD = 0X20; // timer 1, mode 2, 8-bit reload
PCON = 0x80; // SMOD = 1
TH1 = 0xF4; // Baud: 4800 fosc = 11.0592M
IE = 0x90; // Enable Serial Interrupt
TR1 = 1; // timer 1 run
}
// Serial port receiving interrupt function
void serial () interrupt 4 using 3
{
if (RI)
{
RI = 0;
ch = SBUF;
}
}
main ()
{
init_serialcomm (); // Initialize the serial port
while (1)
{
P2 = ch;
Ranch
}
} |
|