一、功能描述
让8个LEE灯发光
二、程序
/****
**********************************************************************/
#include "reg52.h"
sbit D1 = P1^0;
sbit D2 = P1^1;
sbit D3 = P1^2;
sbit D4 = P1^3;
sbit D5 = P1^4;
sbit D6 = P1^5;
sbit D7 = P1^6;
sbit D8 = P1^7;
unsigned char cmd = 0;
unsigned char F_Recv = 0;
/*=======================´®¿Ú³õʼ»¯º¯Êý========================*/
void Init_Uart()
{
//ÌرðÌáÐÑ£ºÈç¹ûҪʹÓÃ9600±ÈÌØÂÊ£¬±ØÐëÍâ½Ó11.0596MHz¾§Õñ
//1-ÉèÖò¨ÌØÂÊ£º12M,2400
TMOD = 0x20;
TH1 = 0xF3;
TL1 = 0xF3;
TR1 = 1;
//2-ÉèÖô®¿ÚµÄ¹¤×÷·½Ê½
SCON = 0x50; //ģʽ1£º8λUARTģʽ£¬ÔÊÐí½ÓÊÕ
//3-ʹÄÜ´®¿ÚÏà¹ØµÄÖжÏ
ES = 1;
EA = 1;
}
/*======================´®¿ÚÖжϷþÎñº¯Êý=======================*/
void Service_Uart() interrupt 4
{
if(RI == 1 ) //Èç¹ûΪ½ÓÊÕÊý¾ÝÖжÏ
{
RI = 0 ; //Çå³ýÊý¾Ý½ÓÊÕÖжϱê־λ
cmd = SBUF; //½Ó½ÓÊÕµ½µÄÊý¾Ý¶Á³ö
F_Recv = 1; //±êÖ¾³É¹¦¶Á³öÒ»¸ö´®¿Ú½ÓÊÕÊý¾Ý
}
}
/*======================´®¿Ú×Ö½Ú·¢Ëͺ¯Êý=======================*/
void SendByte(unsigned char dat)
{
SBUF = dat;
while(TI == 0);
TI = 0;
}
/*======================´®¿Ú×Ö·û´®·¢Ëͺ¯Êý=====================*/
void SendString(unsigned char *str)
{
while(*str != '\0')
{
SendByte(*str++);
}
}
/*===================ÃüÁî½âÎöÓëµÆ¹â¿ØÖƺ¯Êý====================*/
void Control_LED()
{
switch(cmd)
{
case 0xa1:
D1 = 0;
SendString("LEDµÆD1µãÁÁÁË£¡\r\n");
break;
case 0xa2:
D2 = 0;
SendString("LEDµÆD2µãÁÁÁË£¡\r\n");
break;
case 0xa3:
D3 = 0;
SendString("LEDµÆD3µãÁÁÁË£¡\r\n");
break;
case 0xa4:
D4 = 0;
SendString("LEDµÆD4µãÁÁÁË£¡\r\n");
break;
case 0xa5:
D5 = 0;
SendString("LEDµÆD5µãÁÁÁË£¡\r\n");
break;
case 0xa6:
D6 = 0;
SendString("LEDµÆD6µãÁÁÁË£¡\r\n");
break;
case 0xa7:
D7 = 0;
SendString("LEDµÆD7µãÁÁÁË£¡\r\n");
break;
case 0xa8:
D8 = 0;
SendString("LEDµÆD8µãÁÁÁË£¡\r\n");
break;
}
}
/*==========================Ö÷º¯Êý============================*/
void main()
{
Init_Uart() ; //³õʼ»¯´®¿Ú
while(1)
{
if(F_Recv == 1) //³É¹¦¶Á³öÒ»¸ö´®¿Ú½ÓÊÕÊý¾Ý
{
F_Recv = 0;
Control_LED(); //½âÎöÃüÁî²¢¿ØÖÆLEDµÆ
}
}
}
三、电路图