基本功能描述
通过单片机控制8个LED指示灯按照特定的顺序(工作模式)亮灭;指示灯的流转间隔可通过按键调整,亮度可由电位器RB2进行控制;各工作模式的流转间隔时间需在E2PROM中保存,并可在硬件重新上电后,自动载入。
下面附上部分程序:
#include "iic.h" unsigned char code smgduan[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF}; unsigned char code Mode[32]={0XFE,0XFD,0XFB,0XF7,0XEF,0XDF,0XBF,0X7F,//0,1,2,3,4,5,6,7 模式1 0X7F,0XBF,0XDF,0XEF,0XF7,0XFB,0XFD,0XFE,//8,9,10,11,12,13,14,15 模式2 0X7E,0XBD,0XDB,0XE7,0X7E,0XBD,0XDB,0XE7,//16,17,18,19,20,21,22,23 模式3 0XE7,0XDB,0XBD,0X7E,0XE7,0XDB,0XBD,0X7E};//24,25,26,27,28,29,30,31 模式4 unsigned char yi,er,san,si,wu,liu,qi,ba,duan[8],SMG; unsigned char RD2,Flag,brightness,choose,Run; unsigned char Start,num=1,jia,jian,level; unsigned int tt,yy,Interval=400; unsigned char P30_S7,P31_S6,P32_S5,P33_S4; void Delay1ms(unsigned int ms); void Display1(); void Display2(); void Timer0Init(void); //1微秒@11.0592MHz void ledCtr(unsigned char per);//RB2控制LED灯亮度 void Key_Scan(); void GongNeng(); void EEPROM_Storage();//储存到EEPROM void EEPROM_Gain();//读取EEPROM void allInit(); void main() { allInit(); Timer0Init(); EEPROM_Gain(); while(1) { GongNeng(); Key_Scan(); RD2=IIC_Read(0X03)*0.02; } } void allInit() { P2=0XA0; P0=0X00;//关闭蜂鸣器,继电器 P2=0X80; P0=0XFF;//关闭LED灯 P2=0XC0; P0=0XFF;//选择所有数码管 P2=0XFF; P0=0XFF;//关闭所有数码管 } void Delay1ms(unsigned int ms) //@11.0592MHz { int i,j; for(i=ms;i>0;i--) for(j=845;j>0;j--); } void Display1() { unsigned char a; for(a=0;a<8;a++) { P2=0XC0; P0=1<<a; P2=0XFF; P0=duan[a]; Delay1ms(2); } P2=0XC0;//消隐 P0=0XFF; P2=0XFF; P0=0XFF; } void Display2() { duan[0] = smgduan[yi]; duan[1] = smgduan[er]; duan[2] = smgduan[san]; duan[3] = smgduan[si]; duan[4] = smgduan[wu]; duan[5] = smgduan[liu]; duan[6] = smgduan[qi]; duan[7] = smgduan[ba]; } void Timer0Init(void) //1微秒@11.0592MHz { AUXR |= 0x80; //定时器时钟1T模式 TMOD &= 0xF0; //设置定时器模式 TL0 = 0xCD; //设置定时初值 TH0 = 0xD4; //设置定时初值 TF0 = 0; //清除TF0标志 TR0 = 1; //定时器0开始计时 ET0 = 1; EA = 1; } void Timer0() interrupt 1 { tt++; if(tt==800) { Flag++; tt=0; } if(Flag==10) { Flag=0; } yy++; if(yy==Interval) { Run++; yy=0; } if(Run==8) { Run=0; } } void ledCtr(unsigned char per)//RB2控制LED灯亮度 { unsigned char i=1; for(i=1;i<=100;i++) { P2=0X80; if(i<per) { P0=Mode[(num-1)*8+Run]; } else { P2=0X80;P0=0XFF; }//关闭灯 } } void Key_Scan() { if(P30==0) { P30_S7=1; } if(P31==0) { P31_S6=1; } if(P32==0) { P32_S5=1; } if(P33==0) { P33_S4=1; } if((P30_S7==1)&&(P30==1))//S7 { if(choose==0) { if(Start==0) { Start=1; Run=0; yy=0; } else if(Start==1) { Start=0; } SMG=0; } P30_S7=0; } if((P31_S6==1)&&(P31==1))//S6 { if(Start==0) { if(choose==0) { choose=1; SMG=1; } else if(choose==1) { choose=2; SMG=1; } else if(choose==2) { choose=0; SMG=0; EEPROM_Storage(); } } P31_S6=0; } if((P32_S5==1)&&(P32==1))//S5 { if(choose!=0) { jia=1; } P32_S5=0; } if((P33_S4==1)&&(P33==1))//S4 { if(choose!=0) { jian=1; } P33_S4=0; } } void GongNeng() { if(SMG==1) { Display1(); Display2(); if(choose==1) { yi=10;san=10;si=11; if(Flag%2==0) { er=num; } else if(Flag%2==1) { er=11; } if(Interval/1000==0) { wu=11; } else { wu=Interval/1000; } liu=Interval%1000/100;qi=Interval%100/10;ba=Interval%10; } if(choose==2) { yi=10;er=num;san=10;si=11; if(Flag%2==0) { if(Interval/1000==0) { wu=11; } else { wu=Interval/1000; } liu=Interval%1000/100;qi=Interval%100/10;ba=Interval%10; } else if(Flag%2==1) { wu=11;liu=11;qi=11;ba=11; } } } if((choose==0)&&(P33_S4==1)) { Display1(); Display2(); yi=11;er=11;san=11;si=11;wu=11;liu=11;qi=10;ba=level; } if(Start==0) { P2=0X80;P0=0XFF; } else if(Start==1) { ledCtr(brightness); } if(jia==1) { jia=0; if(choose==1) { num++; if(num==5) { num=1; } } if(choose==2) { Interval=Interval+100; if(Interval==1300) { Interval=400; } } } if(jian==1) { jian=0; if(choose==1) { num--; if(num==0) { num=4; } } if(choose==2) { Interval=Interval-100; if(Interval==300) { Interval=1200; } } } if(RD2<=1.25) { level=1; brightness=25; } else if((1.25<RD2)&&(RD2<=2.5)) { level=2; brightness=50; } else if((2.5<RD2)&&(RD2<=3.75)) { level=3; brightness=75; } else if((3.75<RD2)&&(RD2<=5)) { level=4; brightness=100; } } void EEPROM_Storage()//储存到EEPROM { unsigned char high,low; high=Interval/256; low=Interval%256; EEPROM_Write(0X01,high); Delay1ms(1); EEPROM_Write(0X02,low); Delay1ms(1); EEPROM_Write(0X03,num); } void EEPROM_Gain()//读取EEPROM { unsigned char high,low; high=EEPROM_Read(0X01); Delay1ms(1); low=EEPROM_Read(0X02); Delay1ms(1); num=EEPROM_Read(0X03); Interval=high*256+low; }
完整程序及试题文件请移步百度网盘:
链接:https://pan.baidu.com/s/1wskPcpl6hurRR_TiUiBTsQ
提取码:g9b4