Key serial port

Source: Internet
Author: User

Labels: C language single-chip microcomputer serial port

/*************************************** *****************************
Function: Send one byte of data to the serial port. The data can be displayed through a Super Terminal or serial port debugging assistant,
The baud rate of the Super Terminal is 9600 ). Every time you press the S2 key, it is released on the Super Terminal.
A string of letters "abcdefg haha" is displayed ".
Entry parameter: D: The byte data to be sent.
**************************************** ****************************/




# Include <reg52.h>
# Define jingzhen 11059200ul/* use 22.1184m crystal */
# Define botelv 9600ul/* the baud rate is 9600 */
Unsigned char zifuchuan [] = "abcdefg haha"; // the character to be displayed.
Volatile unsigned char sending;
Sbit S2 = P3 ^ 4;


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 interruption
Tmod & = 0x0f; // timer 1 Mode Control in high 4-bit
Tmod | = 0x20; // timer 1 works in Mode 2, auto reinstallation Mode
Scon = 0x50; // Serial Port Works in Mode 1
Th1 = 256-jingzhen/(botelv * 12*16); // calculate the timer reinstallation Value
TL1 = 256-jingzhen/(botelv * 12*16 );
Pcon | = 0x80; // double the serial port baud rate
Es = 1; // The serial interrupt is allowed.
Tr1 = 1; // start timer 1
Ren = 1; // receive allowed
Ea = 1; // interrupt allowed
}


Void send (unsigned char d) // sends a byte of data. The parameter D is the data to be sent.
{
 
Sbuf = D; // write data to the serial port Buffer
Sending = 1; // set the sending flag
While (sending); // wait until the sending is completed
}


Void sendc (unsigned char * PD)
{
While (* PD )! = '\ 0') // sends a string until it reaches 0.
{
Send (* PD); // send one character
PD ++; // move to the next character
}
}


Void main ()
{
Init ();
While (1)
{
If (s2 = 0)
{
Delay (20 );
If (! S2)
{
While (! S2 );
Sendc (zifuchuan );
}
}
}
}


Void UART (void) interrupt 4 // serial port sending interruption
{
If (RI) // receives data
{
Ri = 0; // clear the interrupt request
}
Else // send one byte of data
{
Ti = 0;
Sending = 0; // clear the flag being sent
}

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.