#include <AT89X52.h>
#include <intrins.h>
#include <stdio.h>
bit FUN,Shift_on,Add_on,Sub_on,fun_flag;
bit dspflagz1,dspflagz2,dspflagz3;
unsigned char z1,z2,td3,working,DSP_m,DSP_n;
unsigned char l1,l2,r,m;
bit flagv,flagy,flags,flag0,flagx,beepflag1,beepflag2,flagt,flagw;
bit working_on,function_on, AINTSYR_on, AINTSYW_on,BINTSYR_on,BINTSYW_on ;
bit program_on,program_woking,up_flag,down_flag,up_on,down_on;
unsigned char AINTSY_state, BINTSY_state, function_state;
unsigned char tx1,tx2,tx3,tw,TX,t;
unsigned char display_state ,x1,x2,w1,w2;
unsigned char program_state,program_working;
unsigned char clk_number;
unsigned char code DS[]={0,33,63,86,100,86,63,33};
unsigned char ds_curtime[6]={0x05,0x03,0x02,0x01,0x07,0x06};
unsigned char clk_data[6]={06,1,1,0,0,1};
unsigned char set_time[6];
sbit switch_work= 0xB0;
sbit switch_function=0xB1;
sbit switch_program=0xB2;
sbit switch_up=0x90;
sbit switch_down=0x91;
sbit switch_AINTSYR=0x92;
sbit switch_AINTSYW=0x93;
sbit switch_BINTSYR=0x94;
sbit switch_BINTSYW=0x95;
sbit RS=0xA2;
sbit RW=0xA1;
sbit E=0xA0;
sbit CS2=0xA3;
sbit CS1=0xA4;
sbit DACS1=0xA7;
sbit DACS2=0xA6;
sbit DACS3=0xA5;
sbit ds_sclk=0xB3 ; /*初始化变量*/
sbit ds_io=0xB4;
sbit ds_rst=0xB5;
/*蜂鸣一次程序*/
void buzzer()
{
beepflag1=1;
}
/*工作完成蜂鸣程序*/
void buzzer_two()
{
beepflag2=1;
}
void WRINS1(unsigned char LCDCOM)/*写左片指令函数*/
{
while(P0_7==1)
{
P0=0xFF;
RW=1;
CS1=1;
CS2=0;
RS=0;
E=1;
/* P0_7=0;*/
}
E=0;
RW=0;
CS1=1;
CS2=0;
RS=0;
P0=LCDCOM;
E=1;
E=0;
}
void WRINS2(unsigned char LCDCOM)/*写右片指令函数*/
{
while(P0_7==1)
{
P0=0xFF;
RW=1;
CS1=0;
CS2=1;
RS=0;
E=1;
/* P0_7=0; */
}
E=0;
RW=0;
CS1=0;
CS2=1;
RS=0;
P0=LCDCOM;
E=1;
E=0;
}
void WRDATA1(unsigned char LCDDAT)/*写左片字节数据*/
{
while(P0_7==1) /*判忙否?*/
{
P0=0xFF;
RW=1;
CS1=1;
CS2=0;
RS=0;
E=1;
/* P0_7=0; */
}
E=0;
RW=0;
CS1=1;
CS2=0;
RS=1;
P0=LCDDAT;
E=1;
E=0;
}
void WRDATA2(unsigned char LCDDAT)/*写右片字节数据*/
{
while(P0_7==1)
{
P0=0xFF;
RW=1;
CS1=0;
CS2=1;
RS=0;
E=1;
/* P0_7=0; */
}
E=0;
RW=0;
CS1=0;
CS2=1;
RS=1;
P0=LCDDAT;
E=1;
E=0;
}
void CLRAM()
{
unsigned char R3, R4,L;
R4=0x00;
for(R4=0x00;R4<0x08;R4++)
{
L=(R4|(0xB8));
WRINS1(L); /*设置行启起始地址*/
WRINS2(L);
WRINS1(0x40); /*设置页起始地址*/
WRINS2(0x40);
R3=0;
for(R3=0;R3<64;R3++)
{
WRDATA1(0x00);
WRDATA2(0x00);
}
}
}
void LCDINIT()/*初始化*/
{
WRINS1(0x3E);
WRINS2(0x3E);
WRINS1(0x3F);
WRINS2(0x3F);
CLRAM();
WRINS1(0xC0);
WRINS2(0xC0);
WRINS1(0xB8);
WRINS2(0xB8);
WRINS1(0x40);
WRINS2(0x40);
}
void WR_STRING1(unsigned char code *SD,unsigned char R0)
{
unsigned char R1;
for(R1=0;R1<R0;R1++)
{
WRDATA1(*SD);
SD++;
}
}
void WR_STRING2(unsigned char code *SD,unsigned char R0)
{
unsigned char R1;
for(R1=0;R1<R0;R1++)
{
WRDATA2(*SD);
SD++;
}
}
{
ds_sclk=0;
ds_rst=0;
ds_rst=1;
}
void ds_wbyte(unsigned char dsw_byte) /* 字节写入函数*/
{
unsigned char i;
for(i=0;i<8;i++)
{
ds_io=0;
if(dsw_byte & 0x01)
ds_io=1;
ds_sclk=0;
ds_sclk=1;
dsw_byte>>=1;
}
}
unsigned char ds_rbyte() /*字节读取函数*/
{
unsigned char m;
unsigned char dsr_byte;
unsigned char ds_tmpbyte;
dsr_byte=0x00;
ds_io=1;
for(m=0;m<8;m++)
{
ds_sclk=1;
ds_sclk=0;
ds_tmpbyte=(unsigned char)ds_io;
ds_tmpbyte<<=7;
dsr_byte>>=1;
dsr_byte|=ds_tmpbyte;
}
return(dsr_byte);
}
void ds_clkwbyte(unsigned char ds_add ,unsigned char ds_data) /*时钟字节写入函数*/
{
ds_rest();
ds_wbyte(ds_add);
ds_wbyte(ds_data);
ds_rst=0;
ds_io=1;
}
unsigned char ds_clkrbyte(unsigned char ds_add1) /*时钟字节读出函数*/
{
unsigned char ds_rdata;
ds_rest();
ds_wbyte(ds_add1);
ds_rdata=ds_rbyte();
ds_rst=0;
return(ds_rdata);
}
void ds_settime(unsigned char *ds_pda) /*设定时间初值函数*/
{
unsigned char k;
unsigned char ds_add2=0x82;
ds_clkwbyte(0x8e,0x00); /*将控制寄存器设置为0,WP=0,允许写*/
for(k=0;k<6;k++)
{
ds_clkwbyte(ds_add2,*ds_pda);
ds_pda++;
ds_add2+=2;
}
ds_clkwbyte(0x8e,0x80); /*写保护,防止干扰影响时间值*/
}
void ds_clkget(unsigned char dscurtime[]) /*读取时间数值函数*/
{
unsigned char n,j;
unsigned char ds_add3=0x83;
unsigned char bdata ds_sec;
for(n=0;n<6;n++)
{
dscurtime[n]=ds_clkrbyte(ds_add3); /*格式为:分,时,日,月,星期,年*/
ds_sec=dscurtime[n]; /*将BCD码转化为10进制*/
j=ds_sec>>4;
j*=10;
ds_sec=ds_sec&0x0f;
ds_sec+=j;
dscurtime[n]=ds_sec;
ds_add3+=2;
}
}
void init_bl()
{
working=0;
flag0=0;
function_state=1;
program_state=1;
P1_7=1;
P1_6=0;
beepflag1=0;
beepflag2=0;
TX=2;
flagx=1;
fun_flag=0;
x1=0;
x2=0;
t=200;
display_state=20;
clk_number=1;
dspflagz1=1;
dspflagz2=1;
dspflagz3=1;
DSP_m=0;
DSP_n=0;
r=0;
m=0;
}
void init_working()
{
init_bl();
TMOD=0x11;
TH0=0xFF;
TL0=0xFC;
TH1=0xFF;
TL1=0xFC;
T2MOD=0x00;
T2CON=0x04;
TH2=0xFC;
TL2=0x18;
RCAP2H=0xFC;
RCAP2L=0x18;
AINTSY_state=60;
BINTSY_state=20;
EA=1;
ET2=1;
flagv=1;
flagy=1;
flagt=1;
flagw=1;
P3_0=1;
P3_1=1;
P3_2=1;
P1_0=1;
P1_0=1;
P1_2=1;
P1_3=1;
P1_4=1;
P1_5=1;
FUN=0;
LCDINIT();
}
/*部分程序段*/
void text(void)
{
bit Flag_add; /*加一标志*/
bit Flag_sub; /*减一标志*/
unsigned char max_value; /*各时间单位的最大值*/
unsigned char min_value; /*各时间单位的最小值*/
/*if(FUN==1)
{ */ /*定义标志位*/
if(switch_work==0) /*移位键*/
{
if(Shift_on==0)
{
Shift_on=1;
buzzer();
clk_number++;
if(clk_number>6)clk_number=1;
}
}
else
Shift_on=0;
if(switch_up==0) /*加一键*/
{
if(Add_on==0)
{
Add_on=1;
buzzer();
Flag_add=1;
}
}
else
Add_on=0;
if(switch_down==0) /*减一键*/
{
if(Sub_on==0)
{
Sub_on=1;
buzzer();
Flag_sub=1;
}
}
else
Sub_on=0;
switch(clk_number)
{
case 1: max_value=99;min_value=0;break;
case 2: max_value=12;min_value=1;break;
case 3: if(clk_data[1]==1||
clk_data[1]==3||
clk_data[1]==5||
clk_data[1]==7||
clk_data[1]==8||
clk_data[1]==10||
clk_data[1]==12)
max_value=31; /*1,3,5,7,8,10,12*/
else if(
clk_data[1]==4||
clk_data[1]==6||
clk_data[1]==9||
clk_data[1]==11)
max_value=30; /*4,6,9,11*/
else if((clk_data[0]%4==0)||clk_data[0]==0)
max_value=29; /*闰年*/
else
max_value=28;
min_value=1;
break;
case 4: max_value=12;min_value=1;break;
case 5: max_value=59;min_value=0;break;
case 6: max_value=7;min_value=1;break;
}
if(Flag_add==1)
{
clk_data[clk_number-1]++;
Flag_add=0;
if(clk_data[clk_number-1]>max_value)
clk_data[clk_number-1]=min_value;
}
else if(Flag_sub==1)
{
clk_data[clk_number-1]--;
Flag_sub=0;
if(clk_data[clk_number-1]<min_value||clk_data[clk_number-1]==0xff)
clk_data[clk_number-1]=max_value;
}
if(switch_program==0)
{
while(switch_program==0)
FUN=0;
set_time[0]=(clk_data[5]/10)*0x10+(clk_data[5]%10);
set_time[1]=(clk_data[3]/10)*0x10+(clk_data[3]%10);
set_time[2]=(clk_data[2]/10)*0x10+(clk_data[2]%10);
set_time[3]=(clk_data[1]/10)*0x10+(clk_data[1]%10);
set_time[4]=(clk_data[4]/10)*0x10+(clk_data[4]%10);
set_time[5]=(clk_data[0]/10)*0x10+(clk_data[0]%10);
ds_settime(set_time);
}
/*}*/
}
void check_switch()
{
if(FUN==0)
{
if(switch_work==0)
{
if(working_on==0)
{
working_on=1;
buzzer();
if(working==1)
{
working=0;
flag0=0;
}
else
{
working=1;
flag0=1;
}
}
}
else
{
working_on=0;
}
if(switch_function==0)
{
if(function_on==0)
{
function_on=1;
buzzer();
if(function_state==1)
{
function_state=2;
}
else
{
if(function_state==2)
{
function_state=3;
}
else
{
if(function_state==3)
{
function_state=4;
FUN=1;
fun_flag=1;
}
else
{
function_state=1;
}
}
}
}
}
else
{
function_on=0;
}
if(switch_program==0)
{
if(program_on==0)
{
program_on=1;
buzzer();
program_working=1;
flagv=1;
flagy=1;
flagt=1;
flagw=1;
}
}
else
{
program_on=0;
}
if(switch_up==0)
{
if(up_flag==0)
{
up_flag=1;
up_on=1;
buzzer();
}
}
else
{
up_flag=0;
}
if(switch_down==0)
{
if(down_flag==0)
{
down_flag=1;
down_on=1;
buzzer();
}
}
else
{
down_flag=0;
}
if(switch_AINTSYR==0)
{
if(AINTSYR_on==0)
{
AINTSYR_on=1;
buzzer();
if(AINTSY_state<=63)
{
AINTSY_state=AINTSY_state+1;
}
}
}
else
{
AINTSYR_on=0;
}
if(switch_AINTSYW==0)
{
if(AINTSYW_on==0)
{
AINTSYW_on=1;
buzzer();
if(AINTSY_state>=1)
{
AINTSY_state=AINTSY_state-1;
}
}
}
else
{
AINTSYW_on=0;
}
if(switch_BINTSYR==0)
{
if(BINTSYR_on==0)
{
BINTSYR_on=1;
buzzer();
if(BINTSY_state<=63)
{
BINTSY_state=BINTSY_state+1;
}
}
}
else
{
BINTSYR_on=0;
}
if(switch_BINTSYW==0)
{
if(BINTSYW_on==0)
{
BINTSYW_on=1;
buzzer();
if(BINTSY_state>=1)
{
BINTSY_state=BINTSY_state-1;
}
}
}
else
{
BINTSYW_on=0;
}
}
else
{
text();
}
}
void prepare_work()
{
if(working==1)
{
ET0=1;
TR0=1;
ET1=1;
TR1=1;
if(program_working==1)
{
program_working=0;
if(function_state==1)
{
if(program_state<7)
{
program_state=program_state+1;
}
else
{
program_state=1;
}
}
else
{
if(function_state==2)
{
if(program_state<4)
{
program_state=program_state+1;
}
else
{
program_state=1;
}
}
else
{
if(program_state==1)
{
program_state=2;
}
else
{
program_state=1;
}
}
}
}
}
else
{
ET0=0;
TR0=0;
ET1=0;
TR1=0;
flag0=0;
}
if(up_on==1)
{
up_on=0;
if(display_state<99)
{
display_state=display_state+1;
}
else
{
display_state=0;
}
}
if(down_on==1)
{
down_on=0;
if(display_state>=1)
{
display_state=display_state-1;
}
else
{
display_state=99;
}
}
if(flag0==1)
{
if(flagx==1)
{
x1=tw;
x2=tw;
flagx=0;
}
else
{
x2=tw;
}
if(((x2-x1)>=1)|((x2=0)&&(x1>0)))
{
flagx=1;
display_state=display_state-1;
}
if(display_state<=0)
{
flagx=1;
buzzer_two();
if(flags==1)
{
flags=0;
init_bl();
}
}
}
}
void T2ISR() interrupt 5
{
ET2=0;
/* TH2=0xFC;
TL2=0x18;*/
t--; /*0. 1秒中断次数减1*/
if(t<=0)
{
t=200;
tx3=tx3+1; /* 0.1秒 计时单位*/
td3=td3+1;
}
if(tx3>=10)
{
tx3=0;
tx2=tx2+1;
}
if(tx2>=60) /*到60秒*/
{
tx2=0;
tx1=tx1+1;
tw=tw+1; /*分钟加1*/
}
if(tx1>=60) /*到60分*/
{
tx1=0;
}
if(beepflag1==1)
{
P1_7=0;
l1=l1+1;
if(l1>=200)
{
P1_7=1;
l1=0;
beepflag1=0;
}
}
if(beepflag2==1)
{
if(P1_7==0)
{
l1=l1+1;
if(l1>=200)
{
P1_7=1;
TX=TX-1;
l1=0;
}
}
else
{
l2=l2+1;
if(l2>=200)
{
P1_7=0;
l2=0;
}
}
if(TX<=0)
{
flags=1;
TX=2;
beepflag2=0;
}
}
TF2=0;
ET2=1;
}
void continu_square1() using 3
{ static c1=0x00;
if(c1==0x00)
{
c1=0xFF;
}
else
{
c1=0x00;
}
P0=c1;
return;
}
void inter_square1(unsigned char i) using 3
{
static k1=0;
static c2=0x00;
if(k1<1)
{
c2=0x00;
}
else
{
if(k1<2)
{
c2=0xFF;
}
else
{
if(k1<i)
{
c2=0x80;
}
else
{
k1=0;
return;
}
}
}
P0=c2;
k1=k1+1;
}
void zfsquare1(unsigned int m,unsigned int n) using 3
{
static int g2=0;
static c3=0xFF;
if(g2<m)
{
if(g2%2==0)
{
if(c3==0xFF)
{
c3=0x80;
}
else
{
c3=0xFF;
}
}
}
else
{
if(c3==0x80)
{
c3=0x00;
}
else
{
c3=0x80;
}
}
P0=c3;
if(g2==m+n)
{
g2=0;
return;
}
g2=g2+1;
}
void square1(unsigned char m,unsigned char n) using 3
{
static td=0;
static c4=0x00;
if(td<m)
{
c4=0x00;
}
else
{
c4=128;
}
P0=c4;
td++;
if(td==m+n)
td=0;
return;
}
void square2(unsigned char m,unsigned char n) using 0
{
unsigned char c0,d0,e0,f0;
static g1=0;
if(g1<m)
{
P2_7=1;
P2_6=0;
P2_5=1;
c0=AINTSY_state*4;
P0=c0;
P2_7=1;
P2_6=1;
P2_5=0;
d0=BINTSY_state*4;
P0=d0;
}
else
{
P2_7=1;
P2_6=0;
P2_5=1;
e0=0x00;
P0=e0;
P2_7=1;
P2_6=1;
P2_5=0;
f0=0x00;
P0=f0;
}
g1++;
if(g1==m+n)
g1=0;
return;
}
void sin_wave2() using 0
{
unsigned int a1,a2;
unsigned int b1,b2;
static k=0;
P2_7=1;
P2_6=0;
P2_5=1;
a1=AINTSY_state*DS[k];
a2=a1/25;
P0=a2;
P2_7=1;
P2_6=1;
P2_5=0;
b1=BINTSY_state*DS[k];
b2=b1/25;
P0=b2;
if(k<=6)
{
k=k+1;
}
else
{
k=0;
}
TH1=0xFB;/*定时0.619毫秒*/
TL1=0x2A;
}