Home > other >  Why use a serial port communication 2 can only receive the two bytes of data, but the single-chip mi
Why use a serial port communication 2 can only receive the two bytes of data, but the single-chip mi

Time:09-23

# include "reg51. H"
# include "intrins. H"

Typedef unsigned char BYTE;
Typedef unsigned int the WORD;

11059200 l//# define FOSC system frequency
9600//# define BAUD serial port BAUD rate

# define NONE_PARITY 0//not check
1//# define ODD_PARITY odd parity
2//# define EVEN_PARITY parity
3//# define MARK_PARITY calibration
tag4//# define SPACE_PARITY blank check

# define PARITYBIT EVEN_PARITY//define the check digit

SFR AUXR=0 x8e;//auxiliary register
SFR S2CON=0 x9a;//UART2 control register
SFR S2BUF=0 x9b;//UART2 data register
SFR T2H=0 xd6;//timer 2 high eight
SFR T2L=0 xd7;//timer 2 low 8
SFR IE2=0 xaf;//the interrupt control register 2

# define S2RI 0 x01//S2CON. 0
# define S2TI 0 x02//S2CON. 1
2 # define S2RB8 0 x04//S2CON.
# define S2TB8 0 x08//S2CON. 3

SFR P_SW2=0 xba;//peripheral function switch register 2
# define S2_S0 0 x01//P_SW2. 0

Bit busy;
BYTE BUFFER [10].
int i;
Void SendData publishes the event (BYTE dat);
Void SendString (char * s);

Void main ()
{
P_SW2 & amp;=~ S2_S0;//S2_S0=0 (P1.0/RxD2, P1.1/TxD2)
//P_SW2 |=S2_S0;//S2_S0=1 (P4.6/RxD2_2, P4.7/TxD2_2)

# if (PARITYBIT==NONE_PARITY)
S2CON=0 x50;//8-bit variable baud rate
# elif (PARITYBIT==ODD_PARITY) | | (PARITYBIT==EVEN_PARITY) | | (PARITYBIT==MARK_PARITY)
S2CON=0 xda;//nine variable baud rate, parity bit initial 1
# elif (PARITYBIT==SPACE_PARITY)
S2CON=0 xd2;//nine variable baud rate, parity bit initial 0
# endif

T2L=(65536 - (FOSC/4/BAUD));//set the baud rate reshipment value
T2H=(65536 - (FOSC/4/BAUD)) & gt;> 8;
AUXR=0 x14;//T2 for 1 t mode, and start the timer 2
IE2=0 x01;//can make a serial port interrupt
2EA=1;

//SendString (" STC15F2K60S2 \ r \ nUart2 Test! \r\n");
while(1);
}

/* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
UART2 interrupt service routines
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - */
Void Uart2 () interrupt 8 using 1
{
If (S2CON & amp; S2RI)
{
S2CON & amp;=~ S2RI;//remove S2RI a
BUFFER [I]=S2BUF;//P0 display serial data
i++;
}
If (S2CON & amp; S2TI)
{
S2CON & amp;=~ S2TI;//remove S2TI a
Busy=0; Busy//clear sign
}
}

/* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Send a serial port data
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - */
Void SendData publishes the event (BYTE dat)
{
While (busy);//wait for completion of the previous data
The ACC=dat;//get parity P (PSW. 0)
If (P)//set the parity bit according to the P
{
# if (PARITYBIT==ODD_PARITY)
S2CON & amp;=~ S2TB8;//set the parity bit is 0
# elif (PARITYBIT==EVEN_PARITY)
S2CON |=S2TB8;//set the parity bit is 1
# endif
}
The else
{
# if (PARITYBIT==ODD_PARITY)
S2CON |=S2TB8;//set the parity bit is 1
# elif (PARITYBIT==EVEN_PARITY)
S2CON & amp;=~ S2TB8;//set the parity bit is 0
# endif
}
Busy=1;
S2BUF=ACC;//write data to the UART2 data register
}

/* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sends a string
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - */
Void SendString (char * s)
{
While (* s)//terminated testing string
{
SendData publishes the event (* s++);//send the current character
}
}

CodePudding user response:

Before each receiving, I have no clear 0

CodePudding user response:

What didn't see you send in the application,

CodePudding user response:


I put the "//SendString (" STC15F2K60S2 \ r \ nUart2 Test! \r\n"); "Comments, test is normal,

CodePudding user response:

I have two films is their data bus together, when I sent to him after a period of instruction, he has return instructions please tell me whether is unusual, but I receive instructions can only be received seven top two data in the data,

CodePudding user response:

Baud rate is the same

CodePudding user response:

reference 5 floor hhhh63 reply:
baud rate is the same

Receiving the reset, the baud rate is 9600

CodePudding user response:

reference xuyaqi029 reply: 3/f

I put the "//SendString (" STC15F2K60S2 \ r \ nUart2 Test! \r\n"); "Comments removed, the test is normal,

I have two films is their data bus together, when I sent to him after a period of instruction, he has return instructions please tell me whether is unusual, but I receive instructions can only be received seven top two data in the data,

CodePudding user response:

reference 1/f, worldy response:
before each receiving, I have no clear 0

I is zero, it's just in this program didn't write

CodePudding user response:

refer to 7th floor solitary zhuo seek more reply:
Quote: refer to the third floor xuyaqi029 response:

I put the "//SendString (" STC15F2K60S2 \ r \ nUart2 Test! \r\n"); "Comments removed, the test is normal,

I have two films is their data bus together, when I sent to him after a period of instruction, he has return instructions please tell me whether is unusual, but I receive instructions can only be received seven top two data in the data,


Painting a figure tell everybody the hardware connection.
  • Related