/*
为自已写的独立键盘扫描
犯的错:unsigned密写成了unsigner一直提示找不到a,b
PORTD0=1;不行,要写成PORTD=0x01;才可以,如果一定要写,应写为RD0=1;
PORTD=0x03;不是第三个灯亮,而是0000 0011是两个灯亮
PORTD=0x8则是0000 1000是第4个灯亮。。。。。出错因原进制转化,刚开始写程序进制转化混掉了。
*/
#include
#define uchar unsigned char
#define uint unsigned int
__CONFIG(0x3B31);
const unsigned char table[] = {0x3f,0x6,0x5b,0x4f,0x66,0x6d,0x7c,0x7,0x7f,0x6f}; //数码管数组
void delay(uchar x) //延时函数
{
uint a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void scan(); //扫描键盘函数 声明
void init() //初始化函数
{
TRISB=0xff;
TRISD=0x00;
TRISC=0X00;
TRISD=0x00;
PORTC=0Xff;
PORTD=0x00;
}
//-------------------主函数-----------------------------------------------------------
void main()
{
init ();
while(1)
{
scan(); //扫描键盘
}
}
//-------------------------------------------------------------------------------------
void scan()
{
if(RB0==0)
{
delay(10);
while(!RB0) PORTD=0x01;
}
if(RB1==0)
{
delay(10);
while(!RB1) PORTD=0x02;
}
if(RB2==0)
{
delay(10);
while(!RB2) PORTD=0x04;
}
if(RB3==0)
{
delay(10);
while(!RB3) PORTD=0x08;
}
}
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』