单片机 0~99加减

xiaoxiao2021-02-28  133

#include <reg51.h> #define GPIO_PLACE P1 //段选 #define DataPort P0 //位选 sbit KEY_UP=P2^7; //加 sbit KEY_DOWN=P2^6; //减 unsigned char code Bit[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; unsigned char code Seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; unsigned char TempData[2]; void DelayUs2x(unsigned char t) { while(--t); } void DelayMs(unsigned char t) { while(t--) { DelayUs2x(245); DelayUs2x(245); } } void display(unsigned char FirstBit, unsigned char Num) { unsigned char i,j; for(i=0;i<Num;i++) { DataPort = 0x00; GPIO_PLACE = Bit[i+FirstBit]; j=TempData[i]; DataPort = Seg[j]; DelayMs(10); } } void Time0_init() { TMOD |= 0x01; EA = 1; ET0 = 1; TR0 = 1; } void Time0_isr() interrupt 1 { TH0 = (65536 - 2000) / 256; TL0 = (65536 - 2000) % 256; display(0,2); } void main() { unsigned char key_press_num=0,num=0; KEY_UP=1; KEY_DOWN=1; while(1) { if(!KEY_UP) { DelayMs(10); display(0,2); if(!KEY_UP) { while(!KEY_UP) { key_press_num++; DelayMs(10); display(0,2); if(key_press_num==100) { key_press_num=0; while(!KEY_UP) { num++; if(num==100) { num=0; } TempData[1]=num; TempData[0]=num/10; DelayMs(100); display(0,2); } } } key_press_num=0; num++; if(num==100) { num=0; } } } if(!KEY_DOWN) { DelayMs(10); display(0,2); if(!KEY_DOWN) { while(!KEY_DOWN) { key_press_num++; DelayMs(10); display(0,2); if(key_press_num==100) { key_press_num=0; while(!KEY_UP) { num--; if(num==0xff) { num=99; } TempData[1]=num; TempData[0]=num/10; DelayMs(100); display(0,2); } } } key_press_num=0; num--; if(num==0xff) { num=99; } } } TempData[1]=num; TempData[0]=num/10; display(0,2); } }

 

 

 

 

 

转载请注明原文地址: https://www.6miu.com/read-26455.html

最新回复(0)