利用CAN总线做一个按键数码管 报告Word格式.docx
《利用CAN总线做一个按键数码管 报告Word格式.docx》由会员分享,可在线阅读,更多相关《利用CAN总线做一个按键数码管 报告Word格式.docx(13页珍藏版)》请在冰豆网上搜索。
#include<
intrins.h>
#defineucharunsignedchar
#defineuintunsignedint//宏定义
//------------------------------------数码管start------------------------//
uchard1=0;
uchard2=0;
uchard3=0;
uchard4=0;
uintp=0;
//所要显示的数据
uchardisp[4]={0};
//PE2--SCL13--SCL24--SDA1
sbitSCL1=P1^0;
sbitSCL2=P1^1;
sbitSDA1=P1^2;
//sbitLED=P1^3;
//------------------------------------4X4键盘定义start------------------------//
//键盘用PH口
sbitv1=P2^0;
//列
sbitv2=P2^1;
sbitv3=P2^2;
sbitv4=P2^3;
sbith1=P2^4;
//行
sbith2=P2^5;
sbith3=P2^6;
sbith4=P2^7;
//------------------------------------4X4键盘定义end------------------------//
uchartemp,num=0;
//ucharcodewei[]={0x01,0x02,0x04,0x08};
//四位数码管位选共阳
ucharcodewei[]={~0x01,~0x02,~0x04,~0x08};
//四位数码管位选共阴
//ucharcodeduan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//共阳数码管CA
ucharcodeduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//共阴数码管CC
//0123456789
voiddelay(uintz)//12M约为1ms
{
uintx,y;
for(x=z;
x>
0;
x--)
for(y=110;
y>
y--);
}
/*-------------------------------------4X4键盘输入&
4位数码管输出start-------------------------------------------*/
voidHC595_in(unsignedcharData)/*--74hc595串行数据输入----*/
unsignedchari;
for(i=0;
i<
8;
i++)//循环8次,刚好移完8位
SCL1=0;
//先将移位寄存器控制引脚置为低
SDA1=Data&
0x80;
//取数据的最高位
//SDA1=Data;
Data<
<
=1;
//将数据的次高位移到最高位
SCL1=1;
//再置为高,产生移位时钟上升沿,上升沿时数据寄存器的数据移位
voidHC595_out(void)/*---74hc595并行数据输出-----*/
SCL2=0;
//先将存储寄存器引脚置为低
_nop_();
SCL2=1;
//再置为高,产生移位时钟上升沿,上升沿时移位寄存器的数据进入数据存储寄存器,更新显示数据。
}
voiddigital_display(void)/*4位数码管显示数据*/
uchari;
//uintp=7367;
//显示数据p
d1=p/1000;
d2=p/100%10;
d3=p/10%10;
d4=p%10;
disp[0]=duan[d1];
disp[1]=duan[d2];
disp[2]=duan[d3];
disp[3]=duan[d4];
//LED=0;
for(i=0;
i<
4;
i++)//有四位数码管,四位依次扫描
{
HC595_in(wei[i]);
//先传位码
//HC595_in(duan[i]);
//再传段码
HC595_in(disp[i]);
HC595_out();
delay
(2);
//延迟时间2ms以内
unsignedintkeyboard()/*--keyboard--线反转法--*/
staticunsignedinta=0;
unsignedchara1=0,b1=0;
/*行线P2.4-7为输入线,列线P2.0-3为输出线*/
v1=v2=v3=v4=0;
h1=h2=h3=h4=1;
if(P2<
0xf0)/*检查行是否有按键按下*/
{
delay(20);
0xf0)
/*行线为输入线,列线为输出线*/
a1=P2;
/*行线为输出线,列线为输入线*/
h1=h2=h3=h4=0;
v1=v2=v3=v4=1;
b1=P2;
a=a1|b1;
/*与0x03相或是为了屏蔽p2.0和p2.1的影响*/
}
}
if(a==0xe7){return0;
}//
elseif(a==0xeb){return4;
elseif(a==0xed){return8;
elseif(a==0xee){return12;
elseif(a==0xd7){return1;
elseif(a==0xdb){return5;
elseif(a==0xdd){return9;
elseif(a==0xde){return13;
elseif(a==0xb7){return2;
elseif(a==0xbb){return6;
elseif(a==0xbd){return10;
elseif(a==0xbe){return14;
elseif(a==0x77){return3;
elseif(a==0x7b){return7;
elseif(a==0x7d){return11;
elseif(a==0x7e){return15;
elsereturn8888;
//
ucharkeyscan()/*----键盘扫描----*/
P2=0xfe;
temp=P2;
temp=temp&
0xf0;
while(temp!
=0xf0)
{
delay(5);
temp=P2;
temp=temp&
while(temp!
{
temp=P2;
switch(temp)
{
case0xee:
num=12;
break;
case0xde:
num=13;
case0xbe:
num=14;
case0x7e:
num=15;
}
temp=P2;
temp=temp&
}
}
P2=0xfd;
case0xed:
num=8;
case0xdd:
num=9;
case0xbd:
num=10;
case0x7d:
num=11;
P2=0xfb;
case0xeb:
num=4;
case0xdb:
num=5;
case0xbb:
num=6;
case0x7b:
num=7;
P2=0xf7;
case0xe7:
num=0;
case0xd7:
num=1;
case0xb7:
num=2;
case0x77:
num=3;
returnnum;
}
8位数码管输出end-------------------------------------------*/
//---------------------------------------------主函数-------------------------------------------------//
voidmain(void)
//unsignedchari;
while
(1)
p=keyscan();
//p=6789;
digital_display();