单片机源程序如下:
#include
#include
#include "stdio.h"
#include "delay.h"
#define uchar unsigned char
#define uint unsigned int
char smg_zx[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d, 0x7d,0x07,0x7f,0x6f,0x76,0X38};//
char smg_wx[]={0B00000001,0B00000010,0B00000100,0b00001000,0b00010000,0b00100000,0b01000000,0b10000000};
char buff[]={1,2,3,4,5,6,7,8};
uint cnt0=0,lc=0,dj=0,KS=11,ZJ=0;
uchar mode=0;
void delay_ms(unsigned int k) //8M MS级函数
{
unsigned int i,j;
for(i=0;i { for(j=0;j<570;j++); } } ////多位数拆分函数///////*/ void xs_buff() { buff[0]=lc/10; buff[1]=lc%10; buff[2]=dj/10; buff[3]=dj%10; buff[4]=ZJ/100; buff[5]=ZJ/10%10; buff[6]=ZJ%10; buff[7]=KS; } void xs_smg() { static unsigned char posit=0; PORTA |= 0XFF; // turn off PORTA = ~ smg_wx[posit];//selecting led 7-segment PORTB = smg_zx[buff[posit]]; // delay_ms(2); if(posit++>=7)posit=0; } void keyscan() { if((PIND&0xf0)!=0xf0) { delaynms(20); if((PIND&0xf0)!=0xf0) { if((PIND&0x10)==0) //第一个按键 { if(dj++>=10)dj=0; KS=11; //L //++cnt_1; //00-FF } if((PIND&0x20)==0) //第二个按键 { mode=0; //--cnt_1;//00-FF SEI(); KS=10; //H } if((PIND&0x40)==0) //第三个按键 { //--cnt_1;//00-FF CLI(); KS=11; //L } if((PIND&0x80)==0) //第四个按键 { if(mode++>=2)mode=0;//第一次按下时结束计价,第二次时清零, KS=11; //L //--cnt_1;//00-FF CLI(); if(mode==2) {ZJ=0; lc=0;} } } while((PIND&0xf0)!=0xf0); } } void init_devices(void)//设备初始化函数 { PORTA = 0XFF;//内部上拉 DDRA = 0xFF;//输出 PORTB = 0XFF;//内部上拉 DDRB = 0xFF;//输出 PORTD = BIT(4)|BIT(5)|BIT(6)|BIT(7);//内部上拉 PORTD &= (~BIT(3)); DDRD = ~(BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7));//输入 /****外部事件计数*******/ MCUCR = 0x02; GICR = 0x80; SREG = 0x80; CLI(); //关中断 //SEI(); //开中断 } void main(void) { init_devices();//设备初始化 delaynms(300); //延时待系统稳定; while(1) { keyscan(); xs_buff(); xs_smg(); ZJ=lc*dj; } } /****外部事件计数*******/ #pragma interrupt_handler int1_isr:3 void int1_isr() { if(++cnt0>500){cnt0=0;if(lc++>=99)lc=0;} }
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』