74HC164鍵掃+顯示實(shí)例
源代碼:
/*****************************************************
*main.c中如此這般:
*****************************************************/
kd_init();
//......
while(1)
{
if(should_update_kd)
{
kd_update();
}
//Othercode
//......
}
再看顯示、鍵掃源代碼:
/******************************************************
*key_disp-config.h
******************************************************/
#ifndef_KEY_DISP_CFG_H_
#define_KEY_DISP_CFG_H_
#defineDIGIT1B,0
#defineDIGIT2B,1
#defineDIGIT3B,2
#defineDIGIT4B,3
#defineKEY_FBD,6
#defineKD_CLRD,7
#defineKD_CLKB,5
#defineKD_DATB,4
#defineKEY_NONE(uint8_t)(0xFF)
#defineKEY_S1(uint8_t)(0x01<<0)
#defineKEY_S2(uint8_t)(0x01<<1)
#defineKEY_S3(uint8_t)(0x01<<2)
#defineKEY_S4(uint8_t)(0x01<<3)
#defineKEY_S5(uint8_t)(0x01<<4)
#defineKEY_S6(uint8_t)(0x01<<5)
#defineKEY_S7(uint8_t)(0x01<<6)
#defineKEY_S8(uint8_t)(0x01<<7)
#endif/*_KEY_DISP_CFG_H_*/
/******************************************************
*key_disp.h
******************************************************/
#ifndef_KEY_DISP_H_
#define_KEY_DISP_H_
#include
#include"key_disp-config.h"
#defineKD_CODE_NONE10
#defineKD_CODE_PAUSED11
#defineKD_CODE_CW12
#defineKD_CODE_CCW13
#defineKD_CODE_SET_RUN14
#defineKD_CODE_SET_SLEEP15
#defineKD_CODE_TIMER_RUN16
#defineKD_CODE_TIMER_SLEEP17
#defineKD_CODE_EXTERN_TRIG18
#defineKD_CODE_EXTERN_CTRL19
#defineKD_CODE_H20
#defineKD_CODE_M21
#defineKD_CODE_S22
//Initializekey&display
voidkd_init();
//Updatekey&display,MUSTbecalledperiodically,eg.,intimer
voidkd_update();
//Getkeycode
uint8_tkd_get_key();
//Setmodetodisplay
voidkd_display_code(uint8_tdigit_id,uint8_tcode_id);
//Setdisplaydigits,dp_pos=-1meansnodpdisplayed
voidkd_display(uint16_tvalue,uint8_tmax_digits,constint8_tdp_pos);
#endif/*_KEY_DISP_H_*/
/******************************************************
*key_disp.c
******************************************************/
#include"avr/io.h"
#include"key_disp.h"
#include"config.h"
#include"util.h"
#defineNOP()asmvolatile("nop")
staticconstuint8_tseg_code[]=
{
0x3F/*0*/,0x06/*1*/,0x5B/*2*/,0x4F/*3*/,0x66/*4*/,
0x6D/*5*/,0x7D/*6*/,0x07/*7*/,0x7F/*8*/,0x6F/*9*/,
0x00/*KD_CODE_NONE*/,
0x73/*KD_CODE_PAUSED*/,
0x21/*KD_CODE_CW*/,
0x03/*KD_CODE_CCW*/,
0x50/*KD_CODE_SET_RUN*/,
0x6D/*KD_CODE_SET_SLEEP*/,
0x09/*KD_CODE_TIMER_RUN*/,
0x36/*KD_CODE_TIMER_SLEEP*/,
0x79/*KD_CODE_EXTERN_TRIG*/,
0x39/*KD_CODE_EXTERN_CTRL*/,
0x76/*KD_CODE_H*/,
0x20/*KD_CODE_M*/,
0x22/*KD_CODE_S*/,
};
#defineSEG_DP0x80
staticvolatileuint8_t_key_code=0xFF;
staticvolatileuint8_t_digits[4];
voidkd_init()
{
PORT_DDR_SET(DIGIT1);
PORT_DDR_SET(DIGIT2);
PORT_DDR_SET(DIGIT3);
PORT_DDR_SET(DIGIT4);
PORT_DDR_CLR(KEY_FB);//Input
PORT_DDR_SET(KD_CLR);
PORT_PIN_CLR(DIGIT1);
PORT_PIN_CLR(DIGIT2);
PORT_PIN_CLR(DIGIT3);
PORT_PIN_CLR(DIGIT4);
PORT_PIN_SET(KEY_FB);//Internalpull-up
PORT_PIN_SET(KD_CLR);
_digits[0]=_digits[1]=_digits[2]=_digits[3]=0;
}
/*Takesabout50us@8MHz*/
voidkd_update()
{
staticuint8_tturn=0;
uint8_ti;
if(turn++&0x01)
return;
//Disablealldigitsfirst
PORT_PIN_CLR(DIGIT1);
PORT_PIN_CLR(DIGIT2);
PORT_PIN_CLR(DIGIT3);
PORT_PIN_CLR(DIGIT4);
if(turn++&0x02)
{
//
//trunforkeyscan
//
uint8_tshift_data;
staticuint8_tlast_scan_code=0;
staticuint8_tlast_code_count=0;
//
//Scankey
PORT_PIN_CLR(KD_CLK);
PORT_PIN_CLR(KD_CLR);
PORT_PIN_SET(KD_CLR);
//
//Alloutput1
shift_data=0xFF;
PORT_PIN_SET(KD_DAT);
while(shift_data)
{
//Pulseout
PORT_PIN_SET(KD_CLK);
PORT_PIN_CLR(KD_CLK);
shift_data>>=1;
}
shift_data=0x01;
while(shift_data)
{
if((~shift_data)&0x01)
PORT_PIN_SET(KD_DAT);
else
PORT_PIN_CLR(KD_DAT);
//Pulseout
PORT_PIN_SET(KD_CLK);
PORT_PIN_CLR(KD_CLK);
//Delay
for(i=0;i<16;i++)
NOP();
//Checkfeedback
if(PORT_PIN_VALUE(KEY_FB)==0)
{
if(last_scan_code==shift_data)
{
//Sameaslastscanresult,that'sthekey!
if(last_code_count>4)
_key_code=shift_data;
if(last_code_count<255)
last_code_count++;
}
else
{
last_scan_code=shift_data;
last_code_count=1;
_key_code=KEY_NONE;
}
break;
}
shift_data<<=1;
}
if(shift_data==0)
{
_key_code=KEY_NONE;
last_scan_code=KEY_NONE;
last_code_count=1;
}
}
else
{
//
//Turnfordisplay
//
staticuint8_tcurr_digit=0;
uint8_tcurr_code=0;
//
//Displaydigits
PORT_PIN_CLR(KD_CLK);
PORT_PIN_CLR(KD_CLR);
PORT_PIN_SET(KD_CLR);
curr_code=_digits[curr_digit];
for(i=0;i<8;i++)
{
//MSBfirst
if(curr_code&0x80)
PORT_PIN_SET(KD_DAT);
else
PORT_PIN_CLR(KD_DAT);
curr_code<<=1;
//Pulseout
PORT_PIN_SET(KD_CLK);
PORT_PIN_CLR(KD_CLK);
}
switch(curr_digit)//位控制pin可能不連續(xù),所以不能夠用移位之類的
{
case0:
PORT_PIN_SET(DIGIT4);
break;
case1:
PORT_PIN_SET(DIGIT3);
break;
case2:
PORT_PIN_SET(DIGIT2);
break;
case3:
PORT_PIN_SET(DIGIT1);
break;
}
//Fornexttrun
curr_digit++;
curr_digit%=4;
}
}
uint8_tkd_get_key()
{
return_key_code;
}
voidkd_display_code(uint8_tdigit_id,uint8_tcode_id)
{
_digits[digit_id]=seg_code[code_id];
}
voidkd_display(uint16_tvalue,uint8_tmax_digits,constint8_tdp_pos/*=-1*/)
{
//
//PreparesegcodeforLED
_digits[0]=seg_code[value%10];
value/=10;
_digits[1]=seg_code[value%10];
if(max_digits>2)
{
value/=10;
_digits[2]=seg_code[value%10];
if(max_digits>3)
{
value/=10;
_digits[3]=seg_code[value%10];
}
}
if(dp_pos>=0&&dp_pos<3)
_digits[dp_pos]|=SEG_DP;
}