《用PIC16F877A单片机驱动按键控制8个数码管的显示的C程序(共5页).doc》由会员分享,可在线阅读,更多相关《用PIC16F877A单片机驱动按键控制8个数码管的显示的C程序(共5页).doc(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上用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 sam
2、ple_key; unsigned int sample_keyBuf2; unsigned int key_value; unsigned char key_row_sel; unsigned char key_number;/键号 unsigned int T100ms; unsigned char disbuf8; const unsigned char seven_seg16=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71; /- void disbuf_update(voi
3、d) if(key_number=16) disbuf0=0xff; disbuf1=0xff; disbuf2=0xff; disbuf3=0xff; disbuf4=0xff; disbuf5=0xff; disbuf6=0xff; disbuf7=0xff; return; disbuf0=seven_segkey_number; disbuf1=seven_segkey_number; disbuf2=seven_segkey_number; disbuf3=seven_segkey_number; disbuf4=seven_segkey_number; disbuf5=seven_
4、segkey_number; disbuf6=seven_segkey_number; disbuf7=seven_segkey_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对应着实参disbufi unsigned char i; for(i=0;i8;i+) if(0x80&(keyi) HC595_
5、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;i8;i+) if(0x80&(keyi) 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;i8;i+) s_pData(disbufi); HC595_RCK=0; HC
6、595_RCK=1; /- void key_process(void) unsigned char i; for(i=0;i16;i+) if( (key_value&(0x0001i) ) 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(T100ms10) continue; T100ms=0; disbuf
7、_update(); display(); /- void scan_key(void) sample_key =sample_key1; if(key_com1) sample_key |=0x0001; sample_key =sample_key1; if(key_com0) sample_key |=0x0001; key_row_sel+; if(key_row_sel8) Key_s_pData(1key_row_sel); return; key_row_sel=0; Key_s_pData(1key_row_sel); sample_keyBuf0=sample_key; if(sample_keyBuf1!=sample_keyBuf0) sample_keyBuf1=sample_keyBuf0; return; key_value=sample_keyBuf0; if(key_value!=0) key_finish=TURE; /- /2ms中断 interrupt void time_int(void) if(T0IF=1) T0IF=0; TMR0=0xf0; T100ms+; scan_key(); 专心-专注-专业