您当前的位置:五五电子网电子知识单片机-工控设备PIC单片机中断+数码管动态扫描+3*4矩阵按键 正文
中断+数码管动态扫描+3*4矩阵按键

中断+数码管动态扫描+3*4矩阵按键

点击数:7476 次   录入时间:03-04 11:45:20   整理:http://www.55dianzi.com   PIC单片机

这是我这几天写的程序,是初学者的话,可以做一下参考,PICC网上的例子不是很多!


注解不一定对,我可能没更改过来


//K程序主要是测试配置位的使用要调用87X.H杳看
//定时中断
//原来4的程序是可能为没有按键放开的程序
//程序5修正按键松开的程序
//biao是指示是否有按键按下如果没有的话就指示为显示4个8
#include <pIC.h>
#include <pic1687x.h>
#define PORTDIT(add,bit) ((unsigned)(&add)*8+(bit))
#define PORTBIT(add,bit) ((unsigned)(&add)*8+(bit))
//__IDLOC(1233) ;
//__CONFIG (XT&PWRTEN) ;
static bit PORT_0 @ PORTDIT(PORTD,0); /*定义PORTD 0位*/
static bit PORT_1 @ PORTDIT(PORTD,1); /*定义PORTD 1位*/
static bit PORT_2 @ PORTDIT(PORTD,2); /*定义PORTD 2位*/
static bit PORT_3 @ PORTDIT(PORTD,3); /*定义PORTD 3位*/
static bit PORT_4 @ PORTDIT(PORTD,4); /*定义PORTD 4位*/
static bit PORT_5 @ PORTDIT(PORTD,5); /*定义PORTD 5位*/
static bit PORT_6 @ PORTDIT(PORTD,6); /*定义PORTD 6位*/
static bit PORT_7 @ PORTDIT(PORTD,7); /*定义PORTD 7位*/
//-------------------------------------------------
static bit PORTD_0 @ PORTBIT(PORTB,0); /*定义PORTB 0位*/
static bit PORTD_1 @ PORTBIT(PORTB,1); /*定义PORTB 1位*/
static bit PORTD_2 @ PORTBIT(PORTB,2); /*定义PORTB 2位*/
static bit PORTD_3 @ PORTBIT(PORTB,3); /*定义PORTB 3位*/
static bit PORTD_4 @ PORTBIT(PORTB,4); /*定义PORTB 4位*/
static bit PORTD_5 @ PORTBIT(PORTB,5); /*定义PORTB 5位*/
static bit PORTD_6 @ PORTBIT(PORTB,6); /*定义PORTB 6位*/
static bit PORTD_7 @ PORTBIT(PORTB,7); /*定义PORTB 7位*/
//const char table[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x7C};
unsigned char s; /*每0.125S累加1*/
unsigned char ss,fan; /*每1秒累加1*/
int i=0,tt;


unsigned char LEDadr,biao;
static unsigned char k,led1,led2,led2,led3,led4;
static unsigned char adrshow,count,show1,show2,show3,show4;//显示位控制
const char table[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39}; //A~F
/*0,1,2,3,4,5,6,7,8,9,A,b,C,c,d,E,*/
void keysCAN();
void delay10ms(unsigned char time);
void Dispaly(unsigned char k);
unsigned char key,temp;
void tmint(void)
{ T0CS=0;
PSA=0;
PS2=0;
PS1=1;
PS0=1;
T0IF=0;
T0IE=1;
}
void interrupt clkint(void) //定时器中断函数
{ if(T0IF=1) //增加是定时中断时进入
{
TMR0=0x5b;
T0IF=0;
lEDAdr=ledadr+1;
if(biao==0)


{ if (ledadr>count)
{ledadr=0;}
}
if ((ledadr==1)&(show1==1||biao==1))
{if(biao==1)
{PORTC=0XFF;
PORTA=0;
RA0=1;}
else {PORTC=led1;
PORTA=0;
RA0=1;}
}
if ((ledadr==2)&(show2==1||biao==1))
{ PORTA=0;
if(biao==1)
{PORTC=0XFF;
PORTA=0;
RA1=1;}
else {
PORTA=0;
PORTC=led2;
RA1=1;}
}
if ((ledadr==3)&(show3==1||biao==1))
{PORTA=0;
if (biao==1)
{PORTC=0xff;
PORTA=0;
RA2=1;}
else {
PORTA=0;
PORTC=led3;
RA2=1;}
}
if ((ledadr==4)&(show4==1||biao==1))
{PORTA=0;
if (biao==1)
{PORTC=0XFF;
PORTA=0;
RA3=1;
ledadr=0;}
else{
PORTC=led4;
PORTA=0;
RA3=1;
ledadr=0;}
}
if(s++>8){ s=0;
if(ss++>05)/*每分钟清0*/
ss=0;
}
/* RC3=(bit)(s>4);//可改变占空比1秒闪一次
RC1=(bit)(!ss);//1分闪一次
RC2=(bit)(s==0 || s==2 || s== 4 || s== 6);//0.25秒闪一次*/
}
}
main(void)
{
TRISD=0x0F;//低四位是输入,高四位为输出
TRISC=0;
// PORTD=0X55;
TRISB=0x00;
TRISC=0x00;
PORTB=0X00;
PORTC=0x00;
RB0=1;RB2=0;//接两个LED,开始为全灭
PCFG3=0;
PCFG2=1;
PCFG1=1;
PCFG0=1;
TRISA=0;
PORTA=0;
//RTC=0xff;
T0CS=0;
RA4=0;
ledadr=0;
tmint(); //调用定时器初始化程序
GIE=1;
ledadr=0;
count=0;
// show1=1;biao=0;
// ledadr=2;
//if ((ledadr==2)&(show1==1||biao==1))
// {asm("nop");}

//show=0;//显示清零
s=0 ;
ss=0;
adrshow=1;
TMR0=0x06; //装入定时器初始值
biao=1;led1=led2=led3=led4=0xff;
show1=show2=show3=show4=0;
while(1)
{
// ledadr=0;
keyscan();
if(key!=0)
{ if (count<=4)
{ count=count+1;
if (count==1)
{ led1=table[key];
key=0;
show1=1;show2=0;show3=0;show4=0;biao=0;}
if(count==2)
{ led2=table[key];
key=0;
show2=1;show1=1;show3=0;show4=0;biao=0; }
if(count==3)
{ led3=table[key];
key=0;
show1=1;show2=1;show3=1;show4=0;biao=0;}
if(count==4)
{led4=table[key];
key=0;
show1=1;show2=1;show3=1;show4=1;biao=0;}
}
else count=0;

}
}
}
/* RC3=(bit)(s>4);//可改变占空比1秒闪一次
RC1=(bit)(!ss);//1分闪一次
RC2=(bit)(s==0 || s==2 || s== 4 || s== 6);//0.25秒闪一次 */

void keyscan() //按键扫描子程序
{
PORTD=0xFF;
RD7=0; //低四位的第四位为0(输出)
temp=PORTD;
temp&=0x0F;//屏蔽掉高四位
if(temp !=0x0F) //如果有按键按下的话
{
delay10ms(1);
temp=PORTD;
temp&=0x0F;
if(temp !=0x0F) ;//如果确认有按键按下
{
temp=PORTD;
temp&=0x0F;//屏蔽高位位
asm("nop");
switch(temp)
{
case 0x0E: //1110000b b4有按下
key=4;break;
case 0x0D:
key=8;break;// 10110000b d
case 0x0B:
key=12;break; //11010000b
}
Dispaly(key);
asm("nop");
}
}


PORTD=0xFF;
RD6=0;//低四位的第三位
temp=PORTD;
temp&=0x0F;
if(temp !=0x0F)
{
delay10ms(1);
temp=PORTD;
temp&=0x0F;
if(temp !=0x0F)
{
temp=PORTD;
temp&=0x0F;


switch(temp)
{
case 0x0E: //1110000b b4有按下
key=3;break;
case 0x0D:// 10110000b d
key=7;break;
case 0x0B: //11010000b
key=11;break;
}
Dispaly(key);
}
}


PORTD=0xFF;
RD5=0;//第二位为0
temp=PORTD;
temp&=0x0F;
if(temp !=0x0F)
{
delay10ms(1);
temp=PORTD;
temp&=0x0F;
if(temp !=0x0F)
{
temp=PORTD;
temp&=0x0F;


switch(temp)
{
case 0x0E:
key=2;break;
case 0x0D:
key=6;break;
case 0x0B:
key=10;break;
}
Dispaly(key);
}
}


PORTD=0xFF;
RD4=0;//第1位为0
temp=PORTD;
temp&=0x0F;
if(temp !=0x0F)
{
delay10ms(1);
temp=PORTD;
temp&=0x0F;
if(temp !=0x0F)
{
temp=PORTD;
temp&=0x0F;


switch(temp)
{
case 0x0E:
key=1;break;
case 0x0D:
key=5;break;
case 0x0B:
key=9;break;
}
Dispaly(key);
}
}
}


//延时程序
void delay10ms(unsigned char time)
{
unsigned char a,b,c;
for(a=0;a<time;a++)
for(b=0;b<10;b++)
for(c=0;c<120;c++)
;
}


void Dispaly(unsigned char k) //按键放开子程序
{
unsigned char keytemp,temp;
while (1)
{
delay10ms(1);
//keytemp=(PORTD&0x0f);

keytemp=0x0f;
temp=(PORTD&0x0f);
if (keytemp==temp)
{break;}
}
}




本文关键字:动态  数码管  PIC单片机单片机-工控设备 - PIC单片机

上一篇:PIC常见问题

《中断+数码管动态扫描+3*4矩阵按键》相关文章>>>