单片机之利用按键来使数码管上两位示数增加
2019-08-15 来源:eefocus
#include #include './delay/delay.h' sbit seg_select = P2^1; sbit bit_select = P2^0; sbit key1 = P1^0; #define SEGPORT P0 unsigned char segdata[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; unsigned char bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; unsigned char TempData[2]; void display(void) { /*消除重影*/ static unsigned char i = 0; SEGPORT = 0x0; seg_select = 1; seg_select = 0; SEGPORT = 0xff; bit_select = 1; bit_select = 0; SEGPORT = TempData[i]; seg_select = 1; seg_select = 0; SEGPORT = bitdata[i]; bit_select = 1; bit_select = 0; i++; if(2 == i) { i = 0; } } void timer0_init(void) { EA = 1; //打开总中断 TMOD |= 0x01; //选择中断方式 TH0 = (65536 - 2000) / 256; TL0 = (65536 - 2000) % 256; ET0 = 1; TR0 = 1; } void timer0_isr(void) interrupt 1 { TH0 = (65536 - 2000) / 256; TL0 = (65536 - 2000) % 256; display(); } void main() { unsigned char disdata = 0; timer0_init(); while(1) { /*消抖*/ if(0 == key1) //有按键按下 { delay_ms(10); if(0 == key1) //确实有按键按下 { while(!key1); //有按键抬起 delay_ms(10); while(!key1); //确实有按键抬起 } disdata++; if(100 == disdata) { disdata = 0; } } TempData[0] = segdata[disdata / 10]; TempData[1] = segdata[disdata % 10]; } } /************************************/ /*delay.h*/ #define _DELAY_H_ extern void delay_us(unsigned char t); extern void delay_ms(unsigned char t); extern void delay_s(unsigned char t); #endif /***************************************/ /*delay.c*/ { while(--t); } void delay_ms(unsigned char t) { while(t--) { delay_us(245); delay_us(245); } } void delay_s(unsigned char t) { while(t--) { delay_ms(200); delay_ms(200); delay_ms(200); delay_ms(200); delay_ms(200); } } #ifndef _DELAY_H_
void delay_us(unsigned char t)
上一篇:单片机之矩阵键盘的驱动程序