📄 dt8800.c
字号:
/*初始化DT8800*/
/*****SL*******/
#define ONE_KEYPAD 0
#define SEVEN_KEYPAD 1
/****t,del*****************/
#define ZERO_52SEC 00000100b
/*****r********************/
#define KEYNUM_8 00000010b
/*****sense********************/
/******************************
0-->MAX 0为最高max为最低
*******************************/
#define L0_SENSI 00000000b
#define L1_SENSI 00000100b
#define LMAX_SENSI 00000111b
/******stab*********************/
/*The stability defines the interaction between
the keypad and fingertip. After the touch of the
key is stable, the data corresponding to that key
will be sent out. Also,good stability means good
noise rejection. (STAB2, STAB1, STAB0)=(000) is least
stable, and (111) is the most stable. More stable
means more difficult to detect a key,but less error key data.*/
/******************************
0-->MAX 0为最高max为最低
*******************************/
#define L0_STAB 00000111b
#define L1_STAB 00000011b
#define LMAX_STAB 00000000b
/*******cb**************************/
/*(CB1, CB0)=(0,0) is the most sensitive level,
and (1,1) is the least sensitive level.*/
#define L0_CB 00000000b
#define L1_CB 00000010b
#define LMAX_CB 00000011b
/*******ca**************************/
/*(CA1, CA0)=(0,0) is the most sensitive level,
and (1,1) is the least sensitive level.*/
#define L0_CA 00000000b
#define L1_CA 00000010b
#define LMAX_CA 00000011b
/*******fra*****************************/
#define NO_DIVIDER 0
/*******level***************************/
#define CONTINUA 1
#define CHANG 0
/**
写DT8800初始化时序时用到的子函数
可以同时写几个位
**/
void write_DT8800(char time, char pluse)
{
char i;
for(i=0; i<time; i++)
{
DATA = pluse.0;//取最低位
pluse>>= 1;
CLOCK = 0;
DELAY_N_US;
CLOCK = 1;
DELAY_N_US;
}
}
/**
写一个位操作,用在DT8800初始化中
**/
void write_bit_DT8800(bit pluse)
{
DATA = pluse;
CLOCK = 0;
DELAY_N_US;
CLOCK = 1;
DELAY_N_US;
}
void init_DT8800(bit sl, char t, char del, char r, char sense,
char stab, char cb, char ca, bit fra, bit level)
{
char i;
trisb = 00010000b;//写命令设置为输出 01110000b;
DATA = 1;
CLOCK = 1;//因为是上升沿接收,所以进入状态可能会出现上升沿,应该在
//ABLEN置一之前先将CLOCK和DATA置一
ABLEN = 1;
DELAY_N_US;
/*********sl****************/
DATA = sl;
CLOCK = 0;
DELAY_N_US;
CLOCK = 1;
DELAY_N_US;
/*********t******************/
/*for(i=0; i<3; i++)
{
DATA = t.0;//取最低位
t >>= 1;
CLOCK = 0;
DELAY_N_US;
CLOCK = 1;
DELAY_N_US;
}*/
write_DT8800(3,t);
/*********del******************/
/*for(i=0; i<3; i++)
{
DATA = del.0;
del >>= 1;
CLOCK = 0;
DELAY_N_US;
CLOCK = 1;
DELAY_N_US;
}*/
write_DT8800(3,del);
/*********r******************/
/*for(i=0; i<3; i++)
{
DATA = r.0;
r >>= 1;
CLOCK = 0;
DELAY_N_US;
CLOCK = 1;
DELAY_N_US;
}*/
write_DT8800(3,r);
/*********sense******************/
/*for(i=0; i<3; i++)
{
DATA = sense.0;
sense >>= 1;
CLOCK = 0;
DELAY_N_US;
CLOCK = 1;
DELAY_N_US;
}*/
write_DT8800(3,sense);
/*********stab******************/
/*for(i=0; i<3; i++)
{
DATA = stab.0;
stab >>= 1;
CLOCK = 0;
DELAY_N_US;
CLOCK = 1;
DELAY_N_US;
}*/
write_DT8800(3,stab);
/*********cb******************/
/*for(i=0; i<2; i++)
{
DATA = cb.0;
cb >>= 1;
CLOCK = 0;
DELAY_N_US;
CLOCK = 1;
DELAY_N_US;
}*/
write_DT8800(2,cb);
/*********ca******************/
/*for(i=0; i<2; i++)
{
DATA = ca.0;
ca >>= 1;
CLOCK = 0;
DELAY_N_US;
CLOCK = 1;
DELAY_N_US;
}*/
write_DT8800(2,ca);
/*********fra******************/
/*DATA = fra;
CLOCK = 0;
DELAY_N_US;
CLOCK = 1;
DELAY_N_US;*/
write_bit_DT8800(fra);
/*********level******************/
/*DATA = level;
CLOCK = 0;
DELAY_N_US;
CLOCK = 1;
DELAY_N_US;*/
write_bit_DT8800(level);
/********************************/
ABLEN = 0;
CLOCK = 1;
DATA = 1;
// 将数据和时钟线设置为输入
trisb = 01110000b;
}
/***********************
function: 读取DT8800的按键检测结果信号
input: none
output: 按下键的键值
********************/
char read_DT8800(void)
{
char read_times = 0; // 控制读取下降沿的次数
char key = 0x00;// 储存返回的键值
read_times = 0;
for(read_times=0; read_times < 7; read_times++)
{
while(CLOCK);
key <<= 1;
key.0 = DATA;
while(!CLOCK);
}
key = key & 0x1f;
return key;
}
void init(void)
{
trisb = 01110000b;
adcon1 = 0xff; // 禁止AD功能
trisa = 00010010b;
//---------定时器初始化-------------------
//-----timer0-----------------------------
intcon.PEIE = 0;
intcon.T0IE = 1;//enable timer0 interrupt
intcon.INTE = 0;//disable RB0 interrupt
intcon.RBIE = 0;//disable RB interrupt
intcon.T0IF = 0;//clear the timer0 interrupt overflow
intcon.INTF = 0;
intcon.RBIF = 0;
option_reg.NOT_RBPU = 0;//enable the RB pull-ups
option_reg.INTEDG = 0;
option_reg.T0CS = 1; //Transition on RA4/T0CKI pin
option_reg.T0SE = 1; //Increment on high-to-low transition on RA4/T0CKI pin
option_reg.PSA = 1;//Prescaler is assigned to the WDT
option_reg.PS2 = 0;
option_reg.PS1 = 0;
option_reg.PS0 = 0; // 1:1 PAS
tmr0 = 0xff;
//打开中中断开关
intcon.GIE = 1;
}
void interrupt()
{
if(intcon.T0IF)
{
//此时有键按下
keyvalue = read_DT8800();
get_key = 1;
tmr0 = 0xff;
intcon.T0IF = 0;
intcon.T0IE = 0;
}
}
void main()
{
init();
while(1)
{
if(get_key)//有键按下
{
get_key = 0;
intcon.T0IF = 0;
intcon.T0IE = 1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -