当前位置:首页 > 单片机 > 单片机
[导读]源代码: /***************************************************** *main.c中如此这般: *****************************************************/ kd_init(); //...... while(1) { if(should_upda

源代码:

/*****************************************************
*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可能不连续,所以不能够用移位之类的
{
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;
}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

5月25日,中关村论坛在北京盛大开幕,来自全球知名的行业专家、领军企业齐聚一堂,以“开放合作·共享未来”为主题发布创新科技成果,展开深入交流。适逢创立三十周年的BOE(京东方)携柔性显示、高刷显示等领域的最新技术成果,绿...

关键字: 显示 物联网 显示屏幕

作为目前全国唯一经国务院批准的消费电子领域专业化、国际化展会,中国国际消费电子博览会(简称“电博会”)5月17日在青岛开幕。5万平方米的展区,展示了300余家消费电子、工业互联网领域企业的新产品、新成果。与展同步进行的高...

关键字: 显示 LCD显示 元宇宙

1 月 31 日消息,三星电子现公布 2022 年第四季度财报。三星电子第四季度净利润 23.50 万亿韩元,预估 4.71 万亿韩元。

关键字: 三星电子 电子 LCD 显示

在“2019国际新型显示技术展”上,TechSugar记者采访了四家显示领域的企业,有老兵、有新玩家,他们对产业有着个人独特的见解,公司的发展也处于特殊时期。在这产能过剩问题一直存在的行业,他们到底怎么活下去?

关键字: 显示 国产化 行业
关闭