Blue Bridge Cup preparation---------------UART serial communication----------------Man-machine interaction system (good at)

Source: Internet
Author: User

#include <reg52.h>
#include <intrins.h>
SFR auxr= 0x8e;
unsigned char txtemp[10];
unsigned char rxtemp[10];
unsigned char readflag;
unsigned char ch_i;
Bit overflag;
void Delay ()
{
unsigned char I, j, K;

_nop_ ();
_nop_ ();
i = 43;
j = 6;
k = 203;
Do
{
Do
{
while (--K);
} while (--J);
} while (i.);
}
Timer initialization/////////////////////
void Timer0_init ()
{
tmod|=0x01;
th0= (65535-2000)/256;
tl0= (65535-2000)%256;
Ea=1;
Et0=1;
Tr0=1;
}

Serial Port Initialization///////////////////////
void Uartinit (void)//9600bps@11.0592mhz
{
 pcon &= 0x7f;  //baud rate does not speed
 scon = 0x50;  // 8-bit data, variable baud rate
&NBSP;AUXR &= 0xbf;  //timer 1: for FOSC/12, i.e. 12T
&NBSP;AUXR &= 0xfe;  // Serial 1 Select timer 1 for baud rate generator
 tmod &= 0x0f;  //Clear timer 1 mode bit
 tmod |= 0x20;  // Set timer 1 to 8-bit auto-reload mode
&NBSP;TL1 = 0xfd;  //Set Timing initial value
 th1 = 0xfd;  //Set Timer reload values
 et1 = 0 ;   //Disable Timer 1 interrupt
&NBSP;TR1 = 1;  //start timer 1
 es=1;
}
//Send string via serial port
Void uart_sendstring (unsigned char *str)
{
    unsigned char *p;
&NBSP;&N bsp; 
    p = str;
    while (*p! = ')
    {
&NBSP;&N bsp;      sbuf = *p;
  while (TI = = 0); //wait for Send flag position bit
  ti = 0;
         p++;
   }
}

void Main ()
{
P2= (p2&0x1f) |0xa0;
p0=0x00;
p2=p2&0x1f;

Uartinit ();
Timer0_init ();

while (1)
{
if (Overflag)
{
overflag=0;
ch_i=0;
Uart_sendstring (rxtemp);
}
}

}

void Timer0 () Interrupt 1
{
static unsigned char num;
th0= (65535-2000)/256;
tl0= (65535-2000)%256;
num++;
if (num==100)
{
num=0;
readflag=1;
}
}
void Uart () Interrupt 4
{
if (RI)
{
ri=0;
Rxtemp[ch_i]=sbuf;
ch_i++;
if (rxtemp[ch_i-1]== ' G ') overflag=1;
}
}

This is the most basic interrupt to accept the string notation. According to this can extend the various host computer instructions need to change parameters, send commands and other operations .....

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.