用 extern 进行数组变量的跨文件操作时 需要定义数组的大小
24c02 iic的书写 用到的是0xa0和 0xa1 电位器(dac输出)数据的转换 0-127 对应的0-5v
开关设备设置标志位,根据数据来进行相应的开关不需要标志位的设置
#include <STC15F2K60S2.H>
#define uint unsigned int
#define uchar unsigned char
#include "ds1302.h"
#include "iic.h"
uint shidu;
uchar fazhi=50;
extern uchar time[7];
bit mode=0;
bit jiemian=0;
bit guangai=0;
bit buzz=1;
bit flag=0;
bit flag_1=0;
unsigned char yi,er,san,si,wu,liu,qi,ba;
uchar code smg_duan[18]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0xff,
0xbf,0x7f};
sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;
void select(uchar n)
{
switch(n)
{
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;
case 0: P2=(P2&0x1f)|0x00;
break;
}
}
void Delay1000us()
{
unsigned char i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
void display1(uchar yi,er)
{
select(6);
P0=0x01;
select(7);
P0=smg_duan[yi];
Delay1000us();
select(6);
P0=0x02;
select(7);
P0=smg_duan[er];
Delay1000us();
select(6);
P0=0xff;
select(7);
P0=smg_duan[15];
Delay1000us();
}
void display2(uchar san,si)
{
select(6);
P0=0x04;
select(7);
P0=smg_duan[san];
Delay1000us();
select(6);
P0=0x08;
select(7);
P0=smg_duan[si];
Delay1000us();
select(6);
P0=0xff;
select(7);
P0=smg_duan[15];
Delay1000us();
}
void display3(uchar wu,liu)
{
select(6);
P0=0x10;
select(7);
P0=smg_duan[wu];
Delay1000us();
select(6);
P0=0x20;
select(7);
P0=smg_duan[liu];
Delay1000us();
select(6);
P0=0xff;
select(7);
P0=smg_duan[15];
Delay1000us();
}
void display4(uchar qi,ba)
{
select(6);
P0=0x40;
select(7);
P0=smg_duan[qi];
Delay1000us();
select(6);
P0=0x80;
select(7);
P0=smg_duan[ba];
Delay1000us();
select(6);
P0=0xff;
select(7);
P0=smg_duan[15];
Delay1000us();
}
void keyscan()
{
if(S7==0)
{
display1(yi,er);display2(san,si);display3(wu,liu);display4(qi,ba);
if(S7==0)
{
if(mode==0)mode=1;
else if(mode==1)mode=0;
}while(S7==0);
}
if(S6==0)
{
display1(yi,er);display2(san,si);display3(wu,liu);display4(qi,ba);
if(S6==0)
{ if(mode==0)
{
if(jiemian==0)jiemian=1;
else if(jiemian==1){jiemian=0;write_eeprom(fazhi,0x01);}
}
else if(mode==1)
{
if(buzz==1)buzz=0;
else if(buzz==0)buzz=1;
}
}while(S6==0);
}
if(S5==0)
{
display1(yi,er);display2(san,si);display3(wu,liu);display4(qi,ba);
if(S5==0)
{
if(mode==0)
{
if(jiemian==1)
{
fazhi++;
}
}
else if(mode==1)
{
if(guangai==0)guangai=1;
}
}while(S5==0);
}
if(S4==0)
{
display1(yi,er);display2(san,si);display3(wu,liu);display4(qi,ba);
if(S4==0)
{ if(mode==0)
{
if(jiemian==1)
{
fazhi--;
}
}
else if(mode==1)
{
if(guangai==1)guangai=0;
}
}while(S4==0);
}
}
void intint()
{
select(5);
P0=0x00;
select(4);
P0=0xff;
}
void main()
{
write_time();
intint();
fazhi=read_eeprom(0x01);
while(1)
{
read_time();
shidu=read_rb();
if(mode==0)
{
select(4);P0=0xfe;
if(jiemian==0)
{
yi=time[2]/16;er=time[2]%16;san=16;si=time[1]/16;wu=time[1]%16;liu=15;qi=shidu/10;ba=shidu%10;
}
else if(jiemian==1)
{
yi=16;er=16;san=15;si=15;wu=15;liu=15;qi=fazhi/10;ba=fazhi%10;
}
if(shidu<=fazhi)
{
select(5);P0=0x10;
}
if(shidu>fazhi)
{
select(5);P0=0x00;
}
}
if(mode==1)
{
select(4);P0=0xfd;
yi=time[2]/16;er=time[2]%16;san=16;si=time[1]/16;wu=time[1]%16;liu=15;qi=shidu/10;ba=shidu%10;
if(guangai==1){select(5);P0=0x10;}
if(guangai==0){select(5);P0=0x00;}
if(buzz==1)
{
if(shidu<fazhi)
{
select(5);P0=0x40;
}
}
if(buzz==0)
{
select(5);P0=0x00;
}
}
keyscan();
display1(yi,er);display2(san,si);display3(wu,liu);display4(qi,ba);
}
}