MCU lighting port 51

Serial Port lighting

Experimental requirements

(1) using the programming software to send stc-isp "88 FB AF XX FC FC" 6 bytes of the command to the microcontroller, where "88 FB AF" and "FC FC" for the header data and the frame end, "XX "00 to 07 data.

(2) using the microcontroller serial port interrupt data reception, while the need to determine the correctness of the header and trailer.

(3) Analyzing header and trailer is completed, and then determines if the correct words "XX" data, corresponding to "XX" data LED0 ~ LED7 be lit off control; lost if the data is correct, the process proceeds to wait for reception.

 Experiment Code

#include "reg52.h"             

typedef unsigned int u16;      //对数据类型进行声明定义
typedef unsigned char u8;


/*******************************************************************************
* 函数名         :UsartInit()
* 函数功能           :设置串口
* 输入           : 无
* 输出              : 无
*******************************************************************************/
void UsartInit()
{
    SCON=0X50;            //设置为工作方式1
    TMOD=0X20;            //设置计数器工作方式2
    PCON=0X80;            //波特率加倍
    TH1=0XF3;                //计数器初始值设置,注意波特率是4800的
    TL1=0XF3;
    ES=1;                        //打开接收中断
    EA=1;                        //打开总中断
    TR1=1;                    //打开计数器
}

/*******************************************************************************
* 函数名         : Usart() interrupt 4
* 函数功能          : 串口通信中断函数
* 输入           : 无
* 输出              : 无
*******************************************************************************/
u8 i,val;
u8 arr[6];

/*******************************************************************************
* 函 数 名       : main
* 函数功能         : 主函数
* 输    入       : 无
* 输    出         : 无
*******************************************************************************/
void main()
{    
    UsartInit();  //    串口初始化
    while(1)
    {
    }        
}



void Usart() interrupt 4
{
     EA=0;
     RI=0;
     arr[i]=SBUF;
     if(arr[i]==0x88)
     {
         arr[0]=    arr[i];
        i=0;
     }
     i++;
     if(i==6)
       {
        i=0;
        if(arr[0]==0x88&&arr[1]==0xfb&&arr[2]==0xAF&&arr[4]==0xfc&&arr[5]==0xfc)
        {
            val=arr[3];
             switch (val)
            {
                case 0x00:P0=~0XFE;break;  
                case 0x01:P0=~0XFD;break;
                case 0x02:P0=~0XFB;break;
                case 0x03:P0=~0XF7;break;
                case 0x04:P0=~0XEF;break;
                case 0x05:P0=~0XDF;break;
                case 0x06:P0=~0XBF;break;
                case 0x07:P0=~0X7F;break;
            } 
        }
     }
      EA=1;

}

Pictures lost

Published 118 original articles · won praise 123 · views 60000 +

Guess you like

Origin blog.csdn.net/weixin_42776111/article/details/104431547