#include
#define uchar unsigned char
#define uint unsigned int
sbit P0_0 = P0^0;
sbit P0_1 = P0^1;
sbit P0_4 = P0^4;//RST引脚
sbit P1_0 = P1^0;
sbit P1_1 = P1^1;
sbit P1_2 = P1^2;
sbit P1_3 = P1^3;
sbit P1_4 = P1^4;
sbit P1_5 = P1^5;
sbit P1_6 = P1^6;
sbit P1_7 = P1^7;//低电平使系统上电
sbit P2_0 = P2^0;
sbit P2_1 = P2^1;
sbit P2_2 = P2^2;
sbit P2_3 = P2^3;
sbit CS = P2^4;//片选
sbit SCK = P2^5;
sbit DI = P2^6;//串行数据入
sbit DO = P2^7;
sbit P3_2 = P3^2;//外部中断0
sbit P3_3 = P3^3;//外部中断1
sbit P3_4 = P3^4;//计数器0外部输入
sbit P3_5 = P3^5;//计数器1外部输入
sbit P3_6 = P3^6;
sbit P3_7 = P3^7;
bit flag1; bit flag5; bit flag9; bit flag13;
bit flag2; bit flag6; bit flag10; bit flag14;
bit flag3; bit flag7; bit flag11; bit flag15;
bit flag4; bit flag8; bit flag12;
bit timerout; bit keypress;
uchar scankey;
uint count;uint count1; uint count2;
uint count3; uint count4;
uint i1; uint i2;
void delay(uint delaytime)
{
uint j=0;
for(;delaytime>0;delaytime--)
{for(j=0;j<100;j++)//0.1ms延时
{;}
}
}
void int1() interrupt 2
{
}
void time0() interrupt 1//计数器T0
{
TH0=(256-255)/16;
TL0=(256-255)%16;
if(flag3==1){ count2++;}
if(flag4==1){ count2--;}
if(flag5==1){ count3++;}
if(flag6==1){ count3--;}
if(flag7==1){ if(count2<5){count2++;}else{P1_0=0;P1_2=1;}}
if(flag8==1){ count2--;}
if(flag9==1){ count3--;}
if(flag10==1){ count3++;}
if(flag14==1){ if(count2<10){count2++;}else{P1_0=0;P1_2=1;}}
if(flag15==1){ if(count2>0){count2--;}else{P1_1=0;}}
if(flag11==1){ if(count2
if(flag12==1){ if(count2
if(flag13==1){ if(count2
}
WriCmd(uchar OpAddH,OpAddL)
{
uchar i,m;
CS=1;delay(1);
m=OpAddH;
for(i=0;i<8;i++)
{
DI=m & 0x80;
delay(1);SCK=1;
delay(1);SCK=0;
m=m<<1;
}
m=OpAddL;
for(i=0;i<8;i++)
{
DI=m & 0x80;
delay(1);SCK=1;
delay(1);SCK=0;
m=m<<1;
}
CS=0;
}
WriData(uchar cmd,uchar add,uchar mydata)
{
uchar i,m;
CS=1;delay(1);
m=cmd;
for(i=0;i<8;i++)
{
DI=m & 0x80;
delay(1);SCK=1;
delay(1);SCK=0;
m=m<<1;
}
m=add;
for(i=0;i<6;i++)
{
DI=m & 0x20;
delay(1);SCK=1;
delay(1);SCK=0;
m=m<<1;
}
m=mydata;
for(i=0;i<16;i++)
{
DI=m & 0x8000;
delay(1);SCK=1;
delay(1);SCK=0;
m=m<<1;
}
DO=0;
while(DO==0);
}
Read9346(uchar add)
{
uchar i,m, dump;
CS=1;delay(1);
m=0x06;
for(i=0;i<8;i++)
{
DI=m & 0x80;
delay(1);SCK=1;
delay(1);SCK=0;
m=m<<1;
}
m=add;
for(i=0;i<6;i++)
{
DI=m & 0x20;
delay(1);SCK=1;
delay(1);SCK=0;
m=m<<1;
}
dump=0;
DO=1;
for(i=0;i<16;i++)
{
dump =dump<<1;
delay(1);SCK=1;
delay(1);SCK=0;
delay(1);
dump|=(bit)DO;
}
CS=0;
return(dump);
}
Writ9346(uchar add,mydata)
{
DI=DO=0;CS=0;
WriCmd(0x04,0xc0);
WriData(0x05,add,mydata);
WriCmd(0x04,0x00);
}
void time1() interrupt 3//计数器T1
{
TH1=(256-255)/16;
TL1=(256-255)%16;
if(flag1==1){ count1++;}
if(flag2==1){ count1--;}
if(flag5==1){ count4++;}
if(flag6==1){ count4--;}
if(flag7==1){ count1++;}
if(flag8==1){ if(count1>0){count1--;}else{P1_2=0;P1_1=1;}}
if(flag9==1){ count4++;}
if(flag10==1){ count4--;}
if(flag15==1){ if(count1>0){count1--;}else{P1_2=0;P1_1=1;}}
if(flag14==1){ if(count1<20){count1++;}else{P1_2=0;}}
if(flag11==1){ if(count1
if(flag12==1){ if(count1
if(flag13==1){ if(count1
}
void time2() interrupt 4//定时器中断time2,每10ms运行一次
{
TH2=(65536-10000)/256;
TL2=(65536-10000)%256;//定时器2初始,10ms
TF2=0;
timerout=1;//定时器中断标志
scankey=~(P2<<4);
if(scankey&0xf0) {keypress=1;}//按键按下标志
else {keypress=0;}
}
void main(void)
{
uchar key_new;
uint key_val;
uchar key_cur;
uchar line;
uchar temp;
count=0;
P0=0x00;
P0=0x0C;
P1=0x00;
P2=0x0f;
TMOD=0x66;//计数器工作方式2
T2CON=0x00;
TR2=1;
EA=1;
ET2=1;//开定时器T2中断
while(1)
{
if(P3_7==0|P3_6==0){P1=0x80;}//检测电压过大关闭Q1电源供应(红灯亮)P1_7=1
if(timerout)
{
timerout=0;
count++;
P0_4=1;
if(count==10)
{
P0_4=0;//100ms喂狗
count=0;
}
if(keypress==0)
{ P1=0x00; P2=0x0f;key_val=0; }//
if(keypress==1)
{
key_new=0;
line=11100100;
while(line!=0)
{
P0=(line&0x03)|(P0&0xfc);
key_new<<=4;
key_new/=(~P2&0x0f);
line>>=2;
}//P1低2位循环置00,01,10,11使按键矩阵逐行置零
if(key_cur==key_new) {key_val=key_new;}
else {key_cur=key_new;}
switch(key_val)
{
case 0x1000: flag12=1;P1_0=1;i1=Read9346(0x02);i2=Read9346(0x03);break;//B功能键
case 0x2000: flag3=1;P1_0=1;break;
case 0x4000: flag4=1;P1_4=1;delay(50);P1_1=1;break;
case 0x8000: flag13=1;P1_0=1;i1=Read9346(0x04);i2=Read9346(0x05);break;//C功能键
case 0x0100: flag15=1;P1_5=1;P1_4=1;delay(50);P1_3=1;break;
case 0x0200: flag7=1;P1_0=1;break;
case 0x0400: flag8=1;P1_5=1;P1_4=1;delay(50);P1_3=1;break;
case 0x0800: flag14=1;P1_0=1;break;
case 0x0010: flag10=1;P1_6=1;P1_4=1;delay(50);P1_1=1;P1_2=1;break;
case 0x0020: flag5=1;P1_6=1;delay(50);P1_0=1;P1_2=1; break;
case 0x0040: flag6=1;P1_5=1;P1_4=1;P1_6=1;delay(50);P1_1=1;P1_3=1; break;
case 0x0080: flag9=1;P1_6=1;P1_5=1;delay(50);P1_0=1;P1_3=1; break;
case 0x0001: flag11=1;P1_0=1;i1=Read9346(0x00);i2=Read9346(0x01);break;//A功能键
case 0x0002: flag1=1;P1_2=1;break;
case 0x0004: flag2=1;P1_5=1;delay(50);P1_3=1; break;
case 0x0008: //S键做组合键使用无功能定义
case 0x0009: Writ9346(0x00,count1);Writ9346(0x01,count2);break;//A功能键与S键组合使用S+A 记忆功能A
case 0x1008: Writ9346(0x02,count1);Writ9346(0x03,count2);break;//B功能键与S键组合使用S+B 记忆功能B
case 0x8008: Writ9346(0x04,count1);Writ9346(0x05,count2);break;//C功能键与S键组合使用S+C 记忆功能C
}
temp=key_val;
while(temp!=0){temp=key_val;}
}
}
}
} |