#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 anti_cck_i;
int pili_led_i;
int i=2;
int j=0;
int count;
int long_press=0;
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 begin_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()
{
while(1)
{
P0=P0table[0];P1=~1;
P0=P0table[0];P1=~2;
if(btn1==0) break;
}
}
void task6_pasuse()
{
TR1=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(chg_mode==0 && mode==1)//8.按下按鈕 P2_1 切換至 Mode1 模式,再按一下按鈕切換回 Mode0 模式,進 入 Mode1 模式時,七段顯示器需顯示個位數 0
{
delay_1ms(20);while(chg_mode!=1);delay_1ms(20);
TR0=0;TR1=0;
P1=~1;
P2=P0table[0];
mode=0;
}
else if(chg_mode==0 && mode==0)
{
delay_1ms(20);while(chg_mode!=1);delay_1ms(20);
TR0=1;TR1=1;
mode0();
mode=1;
}
}
}
void T0_int(void) interrupt 1
{
TH0=(8192-5000)/32;
TL0=(8192-5000)%32;
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;}
}
if(btn1==0) //200增減可改變長按時間
{
count=0;
zero_cnt++;
if(zero_cnt >= 500)
{
zero_cnt=0;
TR1=0;
count=0;
bcd[1]=0;
bcd[0]=0;
}
}
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;
}
}
沒有留言:
張貼留言