#define key_start 0x40//按鍵用P3.6按鍵開始
#define key_pause 0x80//探按兩下暫停
int P2table[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,0x02,0x04,0x08,0x10,0x20,0x40};
char const P0table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
char bdata flag;
sbit Mode0_flag=flag^0;
sbit Mode1_flag=flag^1;
sbit up_flag=flag^2;
int task;
int anti_cck_i;
int pili_led_i;
int i=2;
int ii;
int j=0;
int count=0;
char k=0;
int time=0;
char Trg;
char Cont;
unsigned char zero_cnt;
sbit buzzer = P3^5;
char bcd[2]={0,0};
char P1table[]={0xFE,0xfD,0xFF};
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 init()
{
int task=1;//
if(task==1)
{
P0=~0xFF;P1=0XFF;
task1_seg7_rotate3_cck();//
P0=~0xFF;P1=0XFF;//OFF
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 T0_int(void) interrupt 1
{
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
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;
time++;
if(time %2==0)
{
count++;
if(count==100) count=0;
bcd[1]=count/10;
bcd[0]=count%10;
}
}
void KeyRead()
{
unsigned char ReadData = P3^0xff; // 1
ReadData &= 0xc0; //按鍵只讀取 P3.6 及 P3.7不一定要0XC0
/*
11000000 11000000
&)10000000 &01000000
10000000 01000000都沒有按為0X00,有壓為1沒壓為0
*/
Trg = ReadData & (ReadData ^ Cont); // 2
Cont = ReadData; // Cont =為最後按鍵值(沒有放開也是ReadData)=ReadData例如0x40 0x80
Trg=0, cont=readdata
/*
不是0x80不然就是0x40
*/
}
void KeyProc()
{
if(Trg & key_start) TR1=1;//Trg & 只會做一次, 按著不放也是0
/*
Triger只會出現(判斷)一次,Triger沒有按皆為0,cont也是0,
按一次開始計數 P3.6 0x40 & 0x40 =0x40
*/
if(Trg & key_pause)
/*按一次開始計數 P3.7 0x80 & 0x80=0x80 */
{ //短按兩次停止計數
TR1=1;
k++;
if(k == 2)
TR1=0;
}
if(!Trg & !Cont) //按一次放開後 k 為零,按一下k=1, 不執行任何動作,反相Trg 反相cont
{ //沒壓也執行此段
/*
10000000 &
*/
zero_cnt++;
if(zero_cnt>10)//放開執行k清除
{
zero_cnt=0;
k=0;;
}
}
if (Cont & key_pause)//按著不放開是0
/*
長按按鍵停止計數並歸零
0x80 & 0x80
*/
{
zero_cnt++;
if(zero_cnt>50)
{
zero_cnt=0;
TR1=0;
count=0;
bcd[1]=0;
bcd[0]=0;
}
}
}
void main()
{
init();
P3=0xFF;
IE=0x8A;
TMOD=0x11;
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
Mode0_flag=1;
TR0=1; TR1=0;
while(1)
{
if(Mode0_flag==1)
{
delay_1ms(80); //防止按鍵彈跳,delay時間配合短按兩下的時間調整
KeyRead();
KeyProc();
}
}
}
沒有留言:
張貼留言