全部文件:提取码:azxy 这个模拟题主要容易出错的地方是通道1和3的切换时只会读取通道1的值,只要把IIC_SendAck(0)改成IIC_SendAck(1)就可以了。
#include "STC15F2K60S2.h"
#include "iic.h"
typedef unsigned char u8;
typedef unsigned int u16;
sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;
u8 code duan[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
u8 model=1;
u16 pl=0,pl_hc=0;
u16 tt=0;
u16 uu=0,uu_hc=0;
u8 td=1;
u16 count=0;
u8 count1=0;
u16 count2=0;
u8 led_close=0;
void delay(u16 i)
{
while(i--);
}
void Delay5ms()
{
unsigned char i, j;
i = 59;
j = 90;
do
{
while (--j);
} while (--i);
}
void TimerInit(void)
{
AUXR |= 0x40;
TMOD = 0x16;
TL1 = 0xA0;
TH1 = 0x15;
TL0 = 0xff;
TH0 = 0xff;
TR1 = 1;
TR0 = 1;
ET0 = 1;
ET1 = 1;
EA = 1;
}
void Service_T0() interrupt 1
{
count++;
}
void Service_T1() interrupt 3
{
count1++;
if(count1==200)
{
pl=count;
count=0;
count1=0;
}
}
void Chan_hc573(chose)
{
switch(chose)
{
case 4:
P2 = (P2 & 0x1f) | 0x80; break;
case 5:
P2 = (P2 & 0x1f) | 0xa0; break;
case 6:
P2 = (P2 & 0x1f) | 0xc0; break;
case 7:
P2 = (P2 & 0x1f) | 0xe0; break;
}
}
void Init_system()
{
Chan_hc573(5);
P0=0x00;
Chan_hc573(4);
P0=0xff;
}
void Smg_display(u8 com,u8 dat)
{
Chan_hc573(7);
P0=0xff;
Chan_hc573(6);
P0=0x01<<com;
Chan_hc573(7);
P0=dat;
}
void Close_smg()
{
Chan_hc573(6);
P0=0x00;
Chan_hc573(7);
P0=0xff;
}
void Display_f_n_u()
{
if(model==1)
{
Smg_display(0,0x8e); delay(1000);
Smg_display(1,duan[10]); delay(1000);
Smg_display(2,duan[10]); delay(1000);
if(pl>9999) {Smg_display(3,duan[pl/10000]); delay(1000);}
if(pl>999) {Smg_display(4,duan[(pl/1000)%10]); delay(1000);}
if(pl>99) {Smg_display(5,duan[(pl/100)%10]); delay(1000);}
if(pl>9) {Smg_display(6,duan[(pl/10)%10]); delay(1000);}
Smg_display(7,duan[pl%10]); delay(1000);
Close_smg();
}
else if(model==2)
{
tt=1000000/pl;
Smg_display(0,0xc8); delay(1000);
Smg_display(1,duan[10]); delay(1000);
Smg_display(2,duan[10]); delay(1000);
if(tt>9999) {Smg_display(3,duan[tt/10000]); delay(1000);}
if(tt>999) {Smg_display(4,duan[(tt/1000)%10]); delay(1000);}
if(tt>99) {Smg_display(5,duan[(tt/100)%10]); delay(1000);}
if(tt>9) {Smg_display(6,duan[(tt/10)%10]); delay(1000);}
Smg_display(7,duan[tt%10]); delay(1000);
Close_smg();
}
else if(model==3)
{
Smg_display(0,0xc1); delay(1000);
Smg_display(1,duan[11]); delay(1000);
Smg_display(2,duan[td]); delay(1000);
Smg_display(3,duan[10]); delay(1000);
Smg_display(4,duan[10]); delay(1000);
Smg_display(5,duan[(uu/100)%10]+0x80); delay(1000);
Smg_display(6,duan[(uu/10)%10]); delay(1000);
Smg_display(7,duan[uu%10]); delay(1000);
Close_smg();
}
}
void Key_down()
{
if(S4==0)
{
delay(100);
if(S4==0)
{
while(S4==0) {Display_f_n_u();}
model++;
if(model==4) {model=1;}
}
}
if(S5==0)
{
delay(100);
if(S5==0)
{
while(S5==0) {Display_f_n_u();}
if(model==3)
{
if(td==1) {td=3;}
else if(td==3) {td=1;}
}
}
}
if(S6==0)
{
delay(100);
if(S6==0)
{
while(S6==0) {Display_f_n_u();}
if(td==3) {uu_hc=uu;}
}
}
if(S7==0)
{
delay(100);
if(S7==0)
{
while(S7==0) {Display_f_n_u();count2++;Delay5ms();}
if(count2<=200) {count2=0;pl_hc=pl;}
else if(count2>200) {count2=0;led_close=~led_close;}
}
}
}
void Read_td()
{
if(td==1) {uu=read_pcf8591(1)/0.51;}
else if(td==3) {uu=read_pcf8591(3)/0.51;}
}
void Led()
{
Chan_hc573(4);
if(led_close==0)
{
if(uu>uu_hc) {P00=0;}
if(pl>pl_hc) {P01=0;}
if(model==1) {P02=0;}
else if(model==2) {P03=0;}
else if(model==3) {P04=0;}
}
else P0=0xff;
}
void main()
{
Init_system();
TimerInit() ;
while(1)
{
Led();
Read_td();
Key_down();
Display_f_n_u();
}
}
|