用了个跟你差不多的,就是个三态驱动,没啥高深的
#include "User.h"
#define PIN1 PB1
#define PIN2 PA5
#define PIN3 PA4
#define PIN4 PA2
#define PIN5 PC0
#define PIN6 PC1
#define PIN1T TRISB1
#define PIN2T TRISA5
#define PIN3T TRISA4
#define PIN4T TRISA2
#define PIN5T TRISC0
#define PIN6T TRISC1
#define Key1 PC3
#define Key2 PA6
#define Key3 PA1
#define sega 1
#define segb (1<<1)
#define segc (1<<2)
#define segd (1<<3)
#define sege (1<<4)
#define segf (1<<5)
#define segg (1<<6)
unsigned char const SegMentCode[11]=
{
sega|segb|segc|segd|sege|segf,//0
segb|segc,//1
sega|segb|segd|sege|segg,//2
sega|segb|segc|segd|segg,//3
segb|segc|segf|segg,//4
sega|segc|segd|segf|segg,//5
sega|segc|segd|sege|segf|segg,//6
sega|segb|segc,//7
sega|segb|segc|segd|sege|segf|segg,//8
sega|segb|segc|segd|segf|segg, //9
segg
};
typedef struct {
unsigned int b0 : 1;
unsigned int b1 : 1;
unsigned int b2 : 1;
unsigned int b3 : 1;
unsigned int b4 : 1;
unsigned int b5 : 1;
unsigned int b6 : 1;
unsigned int b7 : 1;
}_bits;
typedef union
{
unsigned int byte; //便于储存
_bits bits;
}data_union;
data_union segbufunion;
#define segaf segbufunion.bits.b0
#define segbf segbufunion.bits.b1
#define segcf segbufunion.bits.b2
#define segdf segbufunion.bits.b3
#define segef segbufunion.bits.b4
#define segff segbufunion.bits.b5
#define seggf segbufunion.bits.b6
#define dspbuf segbufunion.byte
unsigned int DispDat;
unsigned char DispNum,KeyBuff;
bit Flag_Lock;
void SM(void)
{
switch(DispNum)
{
case 4:
{
if(segaf)
{PIN5T = 0;PIN5 = 0;}
if(segbf)
{PIN4T = 0;PIN4 = 0;}
if(segcf)
{PIN3T = 0;PIN3 = 0;}
PIN2T = 0;PIN2 = 0;
PIN6T = 0;PIN6 = 1;
break;
}
case 5:
{
if(seggf){PIN6T = 0;PIN6 = 0;}
if(segff){PIN4T = 0;PIN4 = 0;}
if(segef){PIN3T = 0;PIN3 = 0;}
if(segdf){PIN2T = 0;PIN2 = 0;}
PIN5T = 0;PIN5 = 1;
break;
}
case 2:
{
if(segaf){PIN6T = 0;PIN6 = 0;}
if(segbf){PIN5T = 0;PIN5 = 0;}
if(segcf){PIN3T = 0;PIN3 = 0;}
if(Flag_Lock){PIN2T = 0;PIN2 = 0;}
PIN4T = 0;PIN4 = 1;
break;
}
case 3:
{
if(seggf){PIN6T = 0;PIN6 = 0;}
if(segff){PIN5T = 0;PIN5 = 0;}
if(segef){PIN4T = 0;PIN4 = 0;}
if(segdf){PIN2T = 0;PIN2 = 0;}
PIN3T = 0;PIN3 = 1;
break;
}
case 0:
{
if(segaf)
{
PIN6T = 0;
PIN6 = 0;
}
if(segbf)
{
PIN5T = 0;
PIN5 = 0;
}
if(segcf)
{
PIN4T = 0;
PIN4 = 0;
}
PIN2T = 0;
PIN2 = 1;
break;
}
case 1:
{
if(seggf){PIN6T = 0;PIN6 = 0;}
if(segff){PIN5T = 0;PIN5 = 0;}
if(segef){PIN4T = 0;PIN4 = 0;}
if(segdf){PIN3T = 0;PIN3 = 0;}
PIN1T = 0;PIN1 = 1;
break;
}
}
}
unsigned char KeyDat,Key1OpenTime;
unsigned int KeyTime;
void DisPlay(void)
{
unsigned char KeyDatBuff;
DispNum++;
if(DispNum>5)DispNum = 0;
TRISA = 0Xff; //全输入
TRISB = 0Xff; //全输入
TRISC = 0XEB;//C4 C2输出
if(FLAG_XS)
{
switch(DispNum)
{
case 4:if(Sys_Open){dspbuf = SegMentCode[DispDat/100];SM();}break;
case 5:if(Sys_Open){dspbuf = SegMentCode[DispDat/100];SM();}else{dspbuf = SegMentCode[10];SM();}break;
case 2:if(Sys_Open){dspbuf = SegMentCode[DispDat/10%10];SM();}break;
case 3:if(Sys_Open){dspbuf = SegMentCode[DispDat/10%10];SM();}else{dspbuf = SegMentCode[10];SM();};break;
case 0:if(Sys_Open){dspbuf = SegMentCode[DispDat%10];SM();}break;
case 1:if(Sys_Open){dspbuf = SegMentCode[DispDat%10];SM();}else{dspbuf = SegMentCode[10];SM();}break;
}
}
if((!Key1) || (!Key2) || (!Key3))
{
if(!Key1)KeyDat = 1;
else KeyDat = 0;
KeyTime++;
if(KeyTime==42)
{
if(!Key2)
KeyBuff = 3;//减
if(!Key3)
KeyBuff = 4;//加
}
if(KeyTime==1000)
{
if(!Key1)
KeyBuff = 1;//NO/OFF
}
if(KeyTime>=1000)
{
KeyTime = 1000;
KeyDat = 0;
}
}
else
{
Key1OpenTime++;
if(Key1OpenTime>250)Key1OpenTime = 250;
if(KeyDat==1&&KeyTime)
{
if(Key1OpenTime<250)
{//0.5S
Key1OpenTime = 1000;
KeyBuff = 2;//Key1 双击
}
else Key1OpenTime = 0;
}
KeyTime = 0;
}
}
|