// This function set comes from "51 Single-Chip Microcomputer world", Author: bamboo Ding (Xiao Meng ).
// Home address http://www.mcu51.com
// The serial port interrupt service program. You only need to make a simple call to complete the serial port input and output processing.
// There is a buffer in and out, and the size can be set as needed.
// Available function names:
// Char getbyte (void); get a byte from the receiving buffer. If you do not want to wait, check whether inbufsign is 1 before calling.
// Getline (char idata * line, unsigned char N); returns a row of data and ends with a carriage return. The maximum number of input characters must be defined.
// Putbyte (char C); Put one byte into the sending Buffer
// Putbytes (unsigned char * outplace, J); put a string of data to the sending buffer, custom Length
// Putstring (unsigned char Code * puts); send a string defined in the program storage area to the serial port
// Puthex (unsigned char C); sends a hex code of one byte, which is divided into two bytes.
// Putchar (uchar C, uchar J); returns a decimal representation of the number of unsigned characters. It must mark the decimal place and automatically delete unused zeros.
// Putint (uint UI, uchar J); output a decimal representation of the unsigned integer number, which must mark the decimal point and automatically delete unused zeros.
// Delay (unsigned char D); delay N x 100ns
// Putinbuf (uchar C); manually enter a character to the input buffer
// CR; send a carriage return line break
//************************************** ***********************************
# Include <W77E58. h>
# Define uchar unsigned char
# Define uint unsigned int
# Define olen 32/* size of serial transmission buffer */
Idata unsigned char outbuf [olen];/* storage for transmission buffer */
Unsigned char idata * outlast = outbuf; // the location of the last byte that is interrupted for transmission.
Unsigned char idata * putlast = outbuf; // the location of the last byte in the sending Buffer
# Define ilen 12/* size of serial locking buffer */
Idata unsigned char inbuf [ilen];
Unsigned char idata * inlast = inbuf; // The Byte location where the last interrupt enters the receiving buffer
Unsigned char idata * getlast = inbuf; // The last byte location to be removed
Bit outbufsign0; // indicates that the last data upload Buf has been sent. The value is 0.
Bit outbufsign; // The non-null flag of the output buffer is = 1
Bit inbufsign; // The non-empty sign of the receiving buffer is = 1
Bit inbufful; // indicates that the input buffer is full = 1
# Define Cr putstring ("/R/N") // Cr = carriage return
//*****************************
// Delay N x 100ns
Void delay (unsigned char d) // define whether W77E58 or 22 is used at the beginning of the source program. 1184m Crystal Oscillator
{
Unsigned char J;
Do {
D --;
// 110592 & 89C52
# Ifndef cpuw77e58
# Ifndef xtal221184
J = 21; // K = 38 cpu80320 100us K = 21 CPU 8052
# Else
J = 42;
# Endif
# Else
# Ifndef xtal221184
J = 38;
# Else
J = 76;
# Endif
# Endif
Do {J --;} while (J! = 0 );
} While (D! = 0 );
}
//*****************************
// Put a byte into the sending Buffer
Putbyte (char C)
{
Uchar I, J;
Es = 0;/* pause the serial interrupt to avoid errors during data comparison? */
// If (outlast = putlast)
While (outlast-putlast) = 2) & (outlast> putlast) | (outlast <putlast) & (olen-(putlast-outlast) = 2 )))
{
Es = 1; C ++; c --; es = 0;
// I = (0-th1 );
// Do {I --; j = 39; do {J --;} while (J! = 0);} while (I! = 0); // I = 39
}
* Putlast = C; // put the byte into the buffer.
Putlast ++; // The sending buffer pointer plus one
If (putlast = outbuf + olen) putlast = outbuf; // pointer to top to bottom
Outbufsign = 1;
If (! Outbufsign0) {outbufsign0 = 1; Ti = 1;} // The buffer starts to be empty and sent.
Es = 1;
}
//******************************
// Put a string of data into the sending Buffer
Putbytes (unsigned char * outplace, unsigned char J)
{
Int I;
For (I = 0; I <j; I ++)
{
Putbyte (* outplace );
Outplace ++;
}
}
//******************************
// Output a decimal representation of the number of unsigned characters, which must mark the decimal point and automatically delete the unused zero
// For example, putchar (0x32, 2), output "4.8 ".
// Putchar (0x32, 3), output "0.48 ".
// Putchar (0x32, 1), output "48 ".
Putchar (uchar C, uchar J)
{
Uchar idata free [4]; uchar data I;
I = 0;
Free [I ++] = (C/100 + 0x30 );
If (j = 3) Free [I ++] = '.';
Free [I ++] = (C % 100)/10 + 0x30;
If (j = 2) Free [I ++] = '.';
If (j = 2 & free [I-3] = 0x30) Free [I-3] = 0x20;
Free [I ++] = (C % 10) + 0x30;
If (j = 1 & free [I-3] = 0x30) Free [I-3] = 0x20;
If (j = 1 & free [I-3] = 0x20 & free [I-2] = 0x30) Free [I-2] = 0x20;
Putbytes (free, I );
}
//******************************
// Output an unsigned integer in decimal format, which must mark the decimal point and automatically delete unused zeros.
Putint (uint UI, uchar J)
{
Uchar idata free [6];
Uchar data I;
I = 0;
Free [I ++] = (UI/10000 + 0x30 );
If (j = 5) Free [I ++] = '.';
Free [I ++] = (ui % 10000)/1000 + 0x30 );
If (j = 4) Free [I ++] = '.';
If (j = 4 & free [I-3] = 0x30) Free [I-3] = 0x20;
Free [I ++] = (ui % 1000)/100 + 0x30 );
If (j = 3) Free [I ++] = '.';
If (j = 3 & free [I-4] = 0x30) Free [I-4] = 0x20;
If (j = 3 & free [I-4] = 0x20 & free [I-3] = 0x30) Free [I-3] = 0x20;
Free [I ++] = (ui % 100)/10 + 0x30 );
If (j = 2) Free [I ++] = '.';
If (j = 2 & free [I-5] = 0x30) Free [I-5] = 0x20;
If (j = 2 & free [I-5] = 0x20 & free [I-4] = 0x30) Free [I-4] = 0x20;
If (j = 2 & free [I-5] = 0x20 & free [I-4] = 0x20 & free [I-3] = 0x30) free [I-3] = 0x20;
Free [I ++] = (ui % 10 + 0x30 );
If (j = 1 & free [I-5] = 0x30) Free [I-5] = 0x20;
If (j = 1 & free [I-5] = 0x20 & free [I-4] = 0x30) Free [I-4] = 0x20;
If (j = 1 & free [I-5] = 0x20 & free [I-4] = 0x20 & free [I-3] = 0x30) free [I-3] = 0x20;
If (j = 1 & free [I-5] = 0x20 & free [I-4] = 0x20 & free [I-3] = 0x20 & & free [I-2] = 0x30) free [I-2] = 0x20;
Putbytes (free, I );
}
//************************************** *
// Send a string defined in the program storage area to the serial port
Putstring (unsigned char * puts)
{
For (; * puts! = 0; puts ++) // when the stop character 0 ends
Putbyte (* puts );
}
//*************************************
// Send the hex code of one byte, which is divided into two bytes.
Unsigned char code hex _ [] = {"0123456789 abcdef "};
Puthex (unsigned char C)
{
Int ch;
Ch = (C> 4) & 0x0f;
Putbyte (hex _ [CH]);
Ch = C & 0x0f;
Putbyte (hex _ [CH]);
}
//*************************************
// Obtain a byte from the receiving buffer. If you do not want to wait, check whether inbufsign is 1 before calling.
Uchar getbyte (void)
{
Char idata C;
While (! Inbufsign); // the buffer zone is empty.
Es = 0;
C = * getlast; // get data
Getlast ++; // The Position of the last retrieved data plus one
Inbufful = 0; // the full mark of the input buffer is cleared.
If (getlast = inbuf + ilen) getlast = inbuf; // address to the top and return to the bottom
If (getlast = inlast) inbufsign = 0; // if the address is equal, the receiving buffer is empty. Check this flag before obtaining the number.
Es = 1;
Return (c); // retrieve data
}
//************************************** ***
// To receive a row of data, you must define the pointer position and size of the data string del = 0x7f, backspace = 0x08, Cr = 0x0d, LF = 0x0a
Void Getline (uchar idata * line, unsigned char N)
{
Unsigned char CNT = 0; // defines the received Length
Char C;
Do {
If (C = getbyte () = 0x0d) C = 0x00; // read a byte. If you press enter to replace it with an ending character
If (C = 0x08 | C = 0x7f) // process backspace and del
{If (CNT! = 0) // a character has been input and removed.
{CNT --; // subtract one from the total number
Line --; // subtract one from the pointer
Putbyte (0x08); // handle the screen echo
Putbyte ('');
Putbyte (0x08 );
}
}
Else
{Putbyte (* line = C); // input other characters and echo
Line ++; // pointer plus one
CNT ++; // Add one to the total number
}
} While (CNT <n-1 & C! = 0x00 & C! = 0x1b); // when the number is reached, press ENTER or ESC to stop
* Line = 0; // Add the stop character 0.
}
//****************************
// Manually enter one character into the input buffer
Putinbuf (uchar C)
{
Es = 0; If (! Inbufful)
{* Inlast = C; // put data
Inlast ++; // Add one to the last position
If (inlast = inbuf + ilen) inlast = inbuf; // address to the top and return to the bottom
If (inlast = getlast) inbufful = 1; // indicates that the receiving buffer is full.
Inbufsign = 1;
}
Es = 1;
}
//************************************** ***
// Serial interrupt handling
Serial () interrupt 4
{
If (Ti)
{
Ti = 0;
If (outbufsign)
// If (putlast = outlast) outbufsign = 0;
// Else
{
Sbuf = * outlast; // continue sending before sending
Outlast ++; // The Position of the last outgoing byte plus one
If (outlast = outbuf + olen) outlast = outbuf; // return the address to the top and back to the bottom.
If (putlast = outlast) outbufsign = 0; // indicates that the sending buffer is empty when data is sent.
}
Else outbufsign0 = 0;
}
If (RI)
{
Ri = 0;
If (! Inbufful)
{
* Inlast = sbuf; // put data
Inlast ++; // Add one to the last position
Inbufsign = 1;
If (inlast = inbuf + ilen) inlast = inbuf; // address to the top and return to the bottom
If (inlast = getlast) inbufful = 1; // indicates that the receiving buffer is full.
}
}
}
//*****************************
// Initialize the serial port 0xfd = 19200, 0xfa = 9600, 0xf4 = 4800, 0xe8 = 2400, 0xd0 = 1200
Serial_init (){
Scon = 0x50;/* Mode 1: 8-bit UART, enable receiver */
Tmod | = 0x20;/* timer 1 Mode 2: 8-bit reload */
Pcon | = 0x80; Th1 = 0xfa; // FA, // baud * 2/* reload value 19200 baud */
Tr1 = 1;/* timer 1 run */
Es = 1; ren = 1; EA = 1; SM2 = 1; // when SM2 = 1, the first 9th bits received are set to 1.
Tmod | = 0x01; // Th1 auto load 2x8, th0 1x16
Th0 = 31; tl0 = 0; // x 32 = 1 s
Tr0 = 1; // et0 = 1;
}
//*****************
// Main function used for testing
Void main (void)
{
Char C;
Idata unsigned char free [16];
Unsigned char idata * freep = free;
Serial_init ();
Putstring ("jdioptuejls; j; klj ");
Delay (10 );
While (1)
{
Putstring ("com is ready! ");}
C = getbyte ();
Putbyte (0x20 );
Puthex (C );
Switch (c)
{Case 'r ':
Putbytes (inbuf, ilen );
Break;
Case 'G ':
Getline (freep, 10 );
Putbyte (0x20 );
Putstring (freep );
Break;
Default:
Putbyte (C );
//}
}
}