AVR單片機按鍵控制數碼管+1、-1、我已經寫好了,但是遞增跟遞減不會寫,求教
#include <iom88v.h>
unsigned char output[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef}; //定義一個有小數點的數組給個位
unsigned char output1[]={0x3f,0x06,0xd5b,0xc4f,0xe66,0xe6d,0x7d,0x07,0x7f,0x6f}; //定義一個沒有數組的給百十位
unsigned int shuzhi=100;
unsigned char dispaynum=1;
unsigned int System_Time = 0;
unsigned char key;
unsigned char flag;
unsigned char flag1;
unsigned char flag2;
unsigned char flag3;
void delay(unsigned int yanshi)
{
unsigned int x,z;
for(x=yanshi;x>0;x--)
{
for(z=110;z>0;z--);
}
}
void port_init(void) //端口初始化
{
PORTD = 0xff;
DDRD = 0xff; //端口D輸出
PORTC = 0xff;
DDRC = 0xff; //端口C輸出
PORTB = 0x0f;
DDRB = 0xf0;
}
/*void yy(void)
{
unsigned char i;
DDRB = 0xf0; //端口B輸入
PORTB = 0x0f;
i=PINB;
i&0x0f;
if(i!=0x0f) key=1;
else key=0;
}
*/
void keyy(void)
{
unsigned int i;
//---------------------------------------------K1
if(!(PINB&(1<<0)))
{
if(!(PINB&(1<<0)))
{
if(flag==0){shuzhi++;flag=1;}
}
}
if(PINB&(1<<0)){flag=0;}
//-----------------------------------------K2
if(!(PINB&0x02))
{
if(!(PINB&0x02))
{
if(flag1==0){shuzhi--;flag1=1;}
}
}
if(PINB&0x02){flag1=0;}
//------------------------------------------K3
if(!(PINB&0x04))
{
delay(2);
if(!(PINB&0x04))
{
if(flag2==0)
{
while(1)
{shuzhi++;
if(dispaynum==1)
{
PORTD=output[shuzhi%10]; //取數值的個位數
PORTC=0xfb; //使LED3使能
dispaynum=2;
}
else if(dispaynum==2)
{
PORTD=output1[(shuzhi%100)/10]; //取數值的十位數
PORTC=0xfd; //使LED2使能
dispaynum=3;
}
else if(dispaynum==3)
{
PORTD=output1[shuzhi/100]; //取數值的百位數
PORTC=0xfe; //使LED1使能
dispaynum=1;
}
flag2=1;}
}
}
}
if(PINB&0x04){flag2=0;}
}
void dispay (void)
{
if(dispaynum==1)
{
PORTD=output[shuzhi%10]; //取數值的個位數
PORTC=0xfb; //使LED3使能
dispaynum=2;
}
else if(dispaynum==2)
{
PORTD=output1[(shuzhi%100)/10]; //取數值的十位數
PORTC=0xfd; //使LED2使能
dispaynum=3;
}
else if(dispaynum==3)
{
PORTD=output1[shuzhi/100]; //取數值的百位數
PORTC=0xfe; //使LED1使能
dispaynum=1;
}
}
void main(void)
{
port_init();
//yy();
while(1)
{
dispay();
keyy();
/*System_Time++; //防止數據跳動過快,延時一段時間數值再減一
if (System_Time > 1000)
{
System_Time = 0;
shuzhi--;
}
if(shuzhi==0) //循環數值再到999
{
PORTC=0xef;
shuzhi=999;
}*/
}
}
|