//設一些旗標來區分不同按鍵各執行不同的程式頻率請自行調整
#include <at89x52.h>
char reset_P0table[]={0xFE, 0XFC, 0XF8, 0XF0, 0XE0, 0XC0, 0X00, 0xc0};
/* a ab abc abcd abcde abcdef
1111 1110 a
1111 1100 ab
1111 1000 abc
1111 0000 abcd
1110 0000 abcde
1100 0000 abcdef
0000 0000 abcdefg
1100 0000 abcdef
*/
int add=-1;
char x;
sbit sw1=P1^0;
sbit sw2=P2^1;
sbit output=P2^0;
bit change_flag=0;
bit sw1_flag;
bit sw2_flag;
bit breath_flag;
unsigned int breath_count;
unsigned int rpt_count;
char disp_seg7[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x89,0xc9,0xb6,};
/*-------------------------------*/
void delay_1ms(int k)
{
int i,j;
for(i=0;i<k;i++)
for(j=0;j<114;j++) ;
}
/*-------------------------------*/
void cck_run_7seg()//逆時針a ab abc...
{
int i;
for(i=0;i<8;i++) { P0=reset_P0table[i]; delay_1ms(500); }
}
/*-------------------------------*/
void delay_2ms(int k)//約500hz
{
int i,j;
for(i=0;i<k;i++)
for(j=0;j<20;j++)
;
}
void delay_4ms(int k)//245~248hz約250hz
{
int i,j;
for(i=0;i<k;i++)
for(j=0;j<43;j++)
;
}
main()
{
cck_run_7seg();
while(1)
{
if(breath_flag)
{
breath_count++;
if(breath_count%90==0)
{
add++;
if(add>10) add=0;
}
}
if(sw1==0)
{
sw1_flag=1;
sw2_flag=0;
delay_1ms(20);while(sw1!=1);delay_1ms(20);
add++;
if(add>10) add=0;
}
if(sw2==0)
{
sw1_flag=0;
sw2_flag=1;
P2=0x00;
change_flag=~change_flag;
rpt_count=0;
breath_flag=0;
output=1;
delay_1ms(20);
while(sw2!=1)
{
rpt_count++;
if(rpt_count>100)
{
breath_flag=1;
output=0;
}
delay_1ms(20);
}
if(change_flag==1)
{
P0=disp_seg7[11]; delay_1ms(20);
}
else
{
P0=disp_seg7[12]; delay_1ms(20);
}
}
/*--------------------------------------------------*/
if(sw1_flag)
{
if(add==0)
{
P0=disp_seg7[0];
x=0;
output=0;delay_4ms(x);
output=1;delay_4ms(10-x);
}
else if(add==1)
{
x=1;
P0=disp_seg7[1];
output=0;delay_4ms(x);
output=1;delay_4ms(10-x);
}
else if(add==2)
{
x=2;
P0=disp_seg7[2];
output=0;delay_4ms(x);
output=1;delay_4ms(10-x);
}
else if(add==3)
{
P0=disp_seg7[3];
x=3;
output=0;delay_4ms(x);
output=1;delay_4ms(10-x);
}
else if(add==4)
{
P0=disp_seg7[4];
x=4;
output=0;delay_4ms(x);
output=1;delay_4ms(10-x);
}
else if(add==5)
{
P0=disp_seg7[5];
x=5;
output=0;delay_4ms(x);
output=1;delay_4ms(10-x);
}
else if(add==6)
{
P0=disp_seg7[6];
x=6;
output=0;delay_4ms(x);
output=1;delay_4ms(10-x);
}
else if(add==7)
{
P0=disp_seg7[7];
x=7;
output=0;delay_4ms(x);
output=1;delay_4ms(10-x);
}
else if(add==8)
{
P0=disp_seg7[8];
x=8;
output=0;delay_4ms(x);
output=1;delay_4ms(10-x);
}
else if(add==9)
{
P0=disp_seg7[9];
x=9;
output=0;delay_4ms(x);
output=1;delay_4ms(10-x);
}
else if(add==10)
{
P0=disp_seg7[10];
x=10;
output=0;delay_4ms(x);
output=1;delay_4ms(10-x);
}
}
//--------------------------------------
if(sw2_flag)
{
if(change_flag)
{
x=0; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=1; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=2; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=3; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=4; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=5; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=6; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=7; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=8; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=9; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=10;output=0;delay_4ms(x); output=1;delay_4ms(10-x);
}
else
{
x=0; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=1; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=2; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=3; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=4; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=5; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=6; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=7; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=8; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=9; output=0;delay_4ms(x); output=1;delay_4ms(10-x);
x=10;output=0;delay_4ms(x); output=1;delay_4ms(10-x);
}
}
}
}
沒有留言:
張貼留言