#include <at89x52.h>
int P2table[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,0x02,0x04,0x08,0x10,0x20,0x40};
static const char P0table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
int zero_cnt=0;
int task;
int i,ii;
int x=0;
int anti_cck_i;
int pili_led_i;
int i=2;
int j=0;
int btn1_f1=0;
int func;
int count;
int add=0;
sbit btn1=P3^5;//5.按下按鈕開始上數
sbit btn2=P3^6;//6.短按兩下按鈕暫停上數char count=0
sbit buzzer = P3^4;
sbit chg_mode=P2^1;//8.按下按鈕 P2_1 切換至 Mode1 模式,再按一下按鈕切換回 Mode0 模式
int mode=0;
sbit init_btn=P2^0;
char bcd[2]={0,0};
char P1table[]={1,2};
void delay_1ms(int k)
{
int i,j;
for(i=0;i<k;i++)
for(j=0;j<110;j++)
;
}
void task1_seg7_rotate3_cck()//逆時針左旋三次
{
for(anti_cck_i=0;anti_cck_i<3;anti_cck_i++)
{
P1=0XFE;P0=0XFE;delay_1ms(50);//亮a 個位數
P1=0XFD;P0=0XFE;delay_1ms(50); //亮a十位數
P1=0XFB;P0=0XFE;delay_1ms(50); //亮a百位數
P1=0XF7;P0=0XFE;delay_1ms(50); //亮a千位數
P1=0XF7;P0=0XDF;delay_1ms(50); //亮f千位數
P1=0XF7;P0=0XEF;delay_1ms(50); //亮e千位數
P1=0XF7;P0=0XF7;delay_1ms(50); //亮d千位數
P1=0XFB;P0=0XF7;delay_1ms(50); //亮d百位數
P1=0XFD;P0=0XF7;delay_1ms(50); //亮d十位數
P1=0XFE;P0=0XF7;delay_1ms(50); //亮d個位數
P1=0XFE;P0=0XFB;delay_1ms(50); //亮c個位數
P1=0XFE;P0=0XFD;delay_1ms(50); //亮b個位數
}
P1=0XFE;P0=0XFE;delay_1ms(500); //亮a個位數
}
void task2_8LED_rotate3()
{
for(ii=0;ii<3;ii++)
{
for(pili_led_i=0;pili_led_i<14;pili_led_i++)
{
P2=~P2table[pili_led_i];delay_1ms(50);
}
}
}
void task3_buzzer_beep3(int count,int TH,int TL)
{
int i;
for(i=0;i<count;i++)
{
buzzer=0;
delay_1ms(TL);
buzzer=1;
delay_1ms(TH);
}
}
void mode0()
{
P0=P0table[0];P1=~1;
P0=P0table[0];P1=~2;
TR1=0;
count=0;
bcd[1]=0;
bcd[0]=0;
}
void init()
{
int task=1;
if(task==1)
{
P0=~0xFF;P1=0XFF;
task1_seg7_rotate3_cck();
P0=~0xFF;P1=0XFF;
task=2;
}
if(task==2)
{
task2_8LED_rotate3();
P2=~0x80;delay_1ms(500);P2=0xFF;
task=3;
}
if(task==3)
{
int i;
for(i=0;i<3;i++)
{
task3_buzzer_beep3(100,1,1);delay_1ms(10);
}
}
}
void main()
{
init();
IE=0x8a;
TMOD=0x10;
TH0=(8192-5000)/32;
TL0=(8192-5000)%32;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
TR0=1;
TR1=1;
mode0();
while(1)
{
// if(func==0){init();mode0();}
// if(chg_mode==0)
// {
// func++;
// if (func>1) func=0;
// }
// if(init_btn==0 && x==0) {mode0();x=1;}
if(btn1==0 && x==0)
{
count=0;
delay_1ms(20);while(btn1!=1);delay_1ms(20);
TR1=1;
x=1;
}
if(btn1==0 && x==1)//uncomplete
{
zero_cnt++;
if(zero_cnt >= 500)
{
zero_cnt=0;
TR1=0;
count=0;
bcd[1]=0;
bcd[0]=0;
}
}
if(btn2==0) //按下第2個按鈕, 要判斷 2短按 1短按, 表示 Pause 還是 Resume
{
add++;
delay_1ms(20);while(btn2!=1);delay_1ms(20);
if(add % 2==0){TR1=0;}//取mod奇數為繼續數,偶數為暫停
if(add % 2==1){TR1=1;}
}
}
}
void T0_int(void) interrupt 1
{
TH0=(8192-5000)/32;
TL0=(8192-5000)%32;
P0=P0table[ bcd[j] ];
P1=~P1table[j];
j++;
if(j==2)
j=0;
}
void T1_int(void) interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
i = i-1;
if(i==0)
{
i=2;
count++;
if(count==100)
count=0;
bcd[1]=count/10;
bcd[0]=count%10;
}
}
沒有留言:
張貼留言