用PIC16F877A单片机驱动按键控制8个数码管的显示的C程序
扫描二维码
随时随地手机看文章
经典按键控制数码管C程序
#include
#define HC595_SER RB7
#define HC595_SCK RB6
#define HC595_RCK RB5
#define key_164_data RB3
#define key_164_clk RB2
#define key_com1 RB1
#define key_com0 RB0
#define TURE 1
bit key_finish; //定义的位变量,这是PICmate仿真软件环境的特有的地方
unsigned int sample_key;
unsigned int sample_keyBuf[2];
unsigned int key_value;
unsigned char key_row_sel;
unsigned char key_number;//键号
unsigned int T100ms;
unsigned char disbuf[8];
const unsigned char seven_seg[16]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//----------------------------------------------
void disbuf_update(void)
{
if(key_number>=16)
{
disbuf[0]=0xff;
disbuf[1]=0xff;
disbuf[2]=0xff;
disbuf[3]=0xff;
disbuf[4]=0xff;
disbuf[5]=0xff;
disbuf[6]=0xff;
disbuf[7]=0xff;
return;
}
disbuf[0]=seven_seg[key_number];
disbuf[1]=seven_seg[key_number];
disbuf[2]=seven_seg[key_number];
disbuf[3]=seven_seg[key_number];
disbuf[4]=seven_seg[key_number];
disbuf[5]=seven_seg[key_number];
disbuf[6]=seven_seg[key_number];
disbuf[7]=seven_seg[key_number];
}
//----------------------------------------------
void inite_time0(void) //对timer0中断进行初始化
{
PSA=0;
T0CS=0;
T0IF=0;
GIE=1;
T0IE=1;
TMR0=0xf0;
PS0=1;
PS1=1;
PS2=1;
}
//----------------------------------------------
//发送一个字节
void s_pData(unsigned char key) //形参key对应着实参disbuf[i]
{
unsigned char i;
for(i=0;i<8;i++)
{
if(0x80&(key< HC595_SER=1;
else HC595_SER=0;
HC595_SCK=0;
HC595_SCK=1;
}
}
//----------------------------------------------
//发送一个字节
void Key_s_pData(unsigned char key)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(0x80&(key< key_164_data=1;
else
key_164_data=0;
key_164_clk=0;
key_164_clk=1;
}
}
//----------------------------------------------
void display (void)
{
unsigned char i;
for(i=0;i<8;i++)
s_pData(disbuf[i]);
HC595_RCK=0;
HC595_RCK=1;
}
//----------------------------------------------
void key_process(void)
{
unsigned char i;
for(i=0;i<16;i++)
{
if( (key_value&(0x0001< {
key_number=i;
return;
}
}
}
//----------------------------------------------
//主程序
int main(void)
{
PORTB=0;
TRISB=0x03;
inite_time0();
key_number=0xff;
while(1)
{
if(key_finish==TURE)
{//按键扫描成功
key_process();
key_finish=0;
}
if(T100ms<10)
continue;