LED control of 51 single chip microcomputer and PC host computer serial communication

Publisher:chunxingLatest update time:2019-10-19 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The PC controls the LED of the microcontroller on and off or controls the flashing and flashing time of the LED through the upper software. The username and password of the upper software are both admin. Note that PROTEUS serial port simulation needs to be combined with virtual serial port software to work.

Software description of host computer (including source code):
Send data analysis
Byte 1, 2: 0x55 0xaa Data is correct or not judgment flag
Byte 3:
           0x01 ------ Single LED control manual control
           0x02 ------ Single LED control automatic flashing

Byte 4                     
    This byte controls LED0-LED7 from low to high, for example, 0x01 means manually light up LED0 or LED0 flashes automatically; 0x28 means manually light up LED3, LED5 or LED3, LED5 flashes automatically

Byte 5, 6: Indicates flashing time (6 high 8 bits 5 low 8 bits) Value range 0-5000

Program source code preview:

#include

#define uint8 unsigned char

#define uint16 unsigned int

uint8 receive_buffer[6]; //Store received data

uint8 i,k;


void delay1ms(uint16 m) //1ms delay program

{

        uint8 j;

        while(m--)

                for(j=0;j<112;j++);

}


void main()

{

        uint16 time;

        P0 = 0xff; //Initialize LED status, turn off

        TMOD = 0x20; //Timer 1 works in mode 2

        TH1 = 0xfd;

        TL1 = 0xfd; //Baud rate 9600

        TR1 = 1; //Start timer 1

        EA = 1; // Enable general interrupt

        SM0 = 0;

        SM1 = 1; //Serial port working mode 1

        REN = 1; //Enable serial reception

        ES = 1; // Enable serial port interrupt

        while(1)

        {

                if((receive_buffer[0] == 0x55) && (receive_buffer[1] == 0xaa)) //If 0x55 0xaa is received, it means the received data is correct

                {

                        time = ((uint16)receive_buffer[5] << 8) + (uint16)receive_buffer[4]; // flashing time

                        switch(receive_buffer[2])

                        {

                                case 0x01: //manually control the LED on and off

                                        P0 = ~receive_buffer[3];

                                        break;

                                case 0x02: //LED flashes automatically

                                        P0 = ~receive_buffer[3];

                                        delay1ms(time);

                                        P0 = 0xff;

                                        delay1ms(time);

                                        break;

                                default:

                                        break;

                        }

                }

        }

}


void uart_int() interrupt 4

{

        if(RI == 1)

        {

                RI = 0;

                receive_buffer[i] = SBUF;

                i++;

                if(i == 6)

                {

                        i = 0;

                        k = 0;

                        P0 = 0xff;

                }

        }

}



Reference address:LED control of 51 single chip microcomputer and PC host computer serial communication

Previous article:DS18B20.h LCD1602.h header file download temperature and LCD debugging ok can be called directly
Next article:Keil_uvision4 detailed tutorial (with pictures and explanations)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号