51 MCU T2 timer serial communication

Publisher:快乐旅人Latest update time:2022-07-06 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The 52 chip has one more timer than the 51. So I also read a lot of information on the Internet, many of which could not run. After making some modifications, I finally realized the simulation test on Keli and Proteus. The following is my code and designed circuit.

           

/*-----------------------------------------------------------------------------

* Experiment name: RS232 serial port test

* Experimental description: The microcontroller serial port receives data to control the light on and off

* Connection method: 

* Note: The crystal used in this test is 11.0592

***********************************************************************************/

 

#include

#include

 

#define uchar unsigned char

#define uint unsigned int

 

uchar PuZh[36] = "hello world";

 

//--Define the IO used--//

 

sbit LED=P2^0;

 

 

//--Declare global functions--//   

void UsartConfiguration();

void Delay_1ms(uint i);

 

 

/********************************************************************

* Name: Com_Int()

* Function: Serial port interrupt subfunction

* Input: None * Output: None

***************************************************************************/

void Com_IntIsr(void) interrupt 4

{

    uchar i;

  uchar receive_data;

    

  EA = 0;

    

  if(RI == 1) //When the hardware receives a data, RI will be set

    {         

        RI = 0;

        receive_data = SBUF; //Received data

            

        if(receive_data == '1')     

        {

                LED =0; //Lights up when receiving 1

        }

        else

        {

                LED =1; //light off in other cases

        }

        

    }

        

        for(i=0; i<36; i++)

        {

            SBUF = PuZh[i]; //Put the data to be sent into the send register

            while(!TI); //Wait for sending data to complete

            TI=0; //Clear the send completion flag

            Delay_1ms(1);

        }

        EA = 1;

}

 

/***********************************************************************************

* Function name: main

* Function: Main function

* Input: None

* Output: None

***********************************************************************************/

 

void main()

{

 

    UsartConfiguration(); //Initialize the serial port

    

    while(1);    

}

    

/***********************************************************************************

* Function name: UsartConfiguration()

* Function: Set the serial port

* Input: None

* Output: None

***********************************************************************************/

 

void UseArtConfiguration()

{    

 

    T2MOD = 0x01; //Automatically reload

    T2CON = 0x30; //T2 is used as the sending and receiving clock

    TH2 = 0xFF; //9600 baud rate, 11.0592Mhz crystal

    TL2 = 0xDC; 

    RCAP2H = 0xFF; 

    RCAP2L = 0xDC; 

    SCON = 0x50; //Serial port mode 1, 1 start bit, 1 stop bit, 8-bit data, variable baud rate

    PCON = 0X00; //Baud rate is not doubled

     TR2 = 1; //Start T2 

    ES = 1; //Open serial port interrupt

      EA = 1; // Enable general interrupt

}

 

 

/********************************************************************

* Name: Delay_1ms()

* Function: Delay subroutine, delay time is 1ms

* x * Input: x (number of millisecond delays)

* Output: None

***************************************************************************/

void Delay_1ms(uint i) //1ms delay

{

  uchar x,j;

  

  for(j=0;j    for(x=0;x<=148;x++);

}


Since I didn't use my own computer to draw the picture, I can't post the picture, but if I have time, I will definitely post the picture. But I will also talk about the general idea of ​​the design, which is a minimum microcontroller system RX terminal connected to the RX terminal of the compim, TX connected to the TX terminal of the compim, and then the LED is connected to the 0 terminal of the P2 port.


Reference address:51 MCU T2 timer serial communication

Previous article:89c51 single chip microcomputer 32 heart-shaped water lights
Next article:51 MCU timer TMOD and TCON, SCON

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号