• 原森林之家(foresthouse.cn)改为走私范(zousifan.com),只是改个名内容不变。
  • 网站图片自建立开始到19年全部丢失,血的教训时刻备份,多点备份!

51单片机加TM1637模块制作0-9999计次方案

51单片机 10个月前 (01-21) 363次浏览 0个评论 扫描二维码


程序源代码

#include<reg52.h>
#include <intrins.h>

#define u8 unsigned char
#define u16 unsigned int

unsigned char  flagkey =0;	 //按键标志
unsigned char  flagkey2 =0;	 //按键标志2
unsigned char  pauskey =0;	 //暂停标志
unsigned char  backkey =0;	 //倒计时标志

unsigned char Timer0Init_count = 0;//定时器溢出计数 

sbit DIO = P1^1; //TM1637
sbit CLK = P1^2; //TM1637
sbit P10 = P1^0; //led
sbit p00 = P0^0; //电机启动按键
sbit p13 = P1^3; //电机启动
sbit p01 = P0^1; //按键
sbit p02 = P0^2; //清零按键
sbit p03 = P0^3; //暂停按键
sbit p14 = P1^4; //暂停led
sbit p04 = P0^4; //倒计时按键左
sbit p05 = P0^5; //倒计时按键右
sbit p06 = P0^6; //倒计时开始
sbit p15 = P1^5; //倒计时结束蜂鸣器
sbit p16 = P1^6; //倒计时开始LED

u8 num[10]={0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFf,0xEF}; //0-9
u16 second,min,hour;	//TM1637六段


/*------------TM1637----------------*/
void Delay2us()                //@12.01MHz
{
        unsigned char i;       
        i = 6;
        while (--i);
}

void Delay3us()                //@12MHz
{
        unsigned char i;

        i = 10;
        while (--i);
}

void Delay5us()                //@12MHz
{
        unsigned char i;        
        i = 18;
        while (--i);
}

void I2CStart()
{
        CLK = 1;
        DIO = 1;
        Delay2us();
        DIO = 0;
}

void I2CAsk()
{
        CLK = 0;
        Delay5us();
        while(DIO);
        CLK = 1;
        Delay2us();
        CLK = 0;
}

void I2CStop()
{
        CLK = 0;
        Delay2us();
        DIO = 0;
        Delay2us();
        CLK = 1;
        Delay2us();
        DIO = 1;
}

void I2CWriteByte(u8 dat)
{
        u8 i;
        for(i=0;i<8;i++)
        {
                CLK = 0;
                if(dat&0x01)
                {
                        DIO = 1;
                }
                else
                {
                        DIO = 0;
                }
                Delay3us();
                dat>>=1;
                CLK = 1;
                Delay3us();
        }
}

void Display(u8 m,u8 s)
{
        I2CStart();	//1637 开始
        I2CWriteByte(0x40);
        I2CAsk();
        I2CStop();
        I2CStart();
        I2CWriteByte(0xC0);	//设置显示首地址即第一个LED
        I2CAsk();

        I2CWriteByte(num[m/10]);
        I2CAsk();
        I2CWriteByte(num[m%10]);
        I2CAsk();
        I2CWriteByte(num[s/10]);
        I2CAsk();

        I2CWriteByte(num[s%10]);
        I2CAsk();
        I2CWriteByte(0xFF);
        I2CAsk();
        I2CWriteByte(0xFF);
        I2CAsk();
        I2CStop();
        I2CStart();
        I2CWriteByte(0x8F);
        I2CAsk();
        I2CStop();
}




/*------------定时器----------------*/
void Timer0Init(void)		//12M晶振,延时10Ms
{
	TMOD = 0x01; // 设置定时器0为模式1
    TH0 = 0x0D8; // 设置定时器0的初始值,用于产生10ms的定时中断
    TL0 = 0x0F0;
    EA = 1; // 允许中断
    ET0 = 1; // 允许定时器0中断
    //TR0 = 1; // 启动定时器0
}


/*------------检察按键----------------*/  	
void anjian(void)
{
	  if(p01 == 0){  // 检测按键是否按下 传感器	  
	  	flagkey2=0;TR0 = 1;//开始计时,溢出后中断
	  }
	  else if(p00 == 0){//电机启停键
	  flagkey=2;flagkey2=1;TR0 = 1;
	  }else if(p02 == 0){//Tm1637清零
	  flagkey=3;flagkey2=1;TR0 = 1;
	  }else if(p03 == 0){//暂停
	  flagkey=4;flagkey2=1;TR0 = 1;
	  }else if(p04 == 0 || p05 == 0){//倒计时设置
	  flagkey=5;flagkey2=1;TR0 = 1;
	  }else if(p06 == 0){//倒计时开始
	  flagkey=6;flagkey2=1;TR0 = 1;
	  }else{
	   		 flagkey=1;
	  	}
}

/*------------主程序----------------*/
void main()
{ 
  Timer0Init(); //定时器初始化
  P10 = 1;//led关  
  p13 = 1;//电机关       
        while(1) 
        {
            Display(min,second); //显示
			anjian();			 //检察按键
        }
}




/*------------定时器中断子程序----------------*/
void Timer0Interrupt(void) interrupt 1{
if(flagkey==1 && flagkey2==0 && pauskey==0 && backkey==0 && p13==0){
flagkey=0;
Timer0Init_count ++;
if(Timer0Init_count>5){
TR0 = 0;//关定时器
TH0 = 0x0D8;//赋初值
TL0 = 0x0F0;
Timer0Init_count=0;
P10 = ~P10;
second++;
if(second==100){
	second=0;
	min++;
	if(min==100)
	    {
	        //min=0;
			min=99;
			second=99;
	        //hour++;
	        //if(hour==100){hour=0;}
	    }				
			
}}}

else if(flagkey==1 && flagkey2==0 && pauskey==0 && backkey==1 && p13==0){
flagkey=0;	
Timer0Init_count ++;
if(Timer0Init_count>5){
TR0 = 0;//关定时器
TH0 = 0x0D8;//赋初值
TL0 = 0x0F0;
Timer0Init_count=0;
P10 = ~P10;

if(second>0){second--;}else
if(min>0){min--;second=99;}else{p15=~p15;p13=1;p03=~p03;}
}}

else if(flagkey==2){// 电机启停
flagkey=0;
Timer0Init_count ++;
if(Timer0Init_count>2){
TR0 = 0;//关定时器
TH0 = 0x0D8;//赋初值
TL0 = 0x0F0;
Timer0Init_count=0;
p13 = ~p13;
}}

else if(flagkey==3){//清零
flagkey=88;	
Timer0Init_count ++;
if(Timer0Init_count>2){
TR0 = 0;//关定时器
TH0 = 0x0D8;//赋初值
TL0 = 0x0F0;	

Timer0Init_count=0;

second=0;min=0;

}}

else if(flagkey==4){//暂停 
flagkey=0;
Timer0Init_count ++;
if(Timer0Init_count>2){

TR0 = 0;//关定时器
TH0 = 0x0D8;//赋初值
TL0 = 0x0F0;
Timer0Init_count=0;
if(pauskey==0){pauskey=1;p14=0;}else if(pauskey==1){pauskey=0;p14=1;}
}
	
}

else if(flagkey==5){//倒计时设置
flagkey=88;	
Timer0Init_count ++;
if(Timer0Init_count>1){
TR0 = 0;//关定时器
TH0 = 0x0D8;//赋初值
TL0 = 0x0F0;
Timer0Init_count=0;
if(pauskey==0 && p04==0){pauskey=1;p13=1;p14=0;min++;if(min==100){min=0;}}else
if(pauskey==0 && p05==0){pauskey=1;p13=1;p14=0;second++;if(second==100){second=0;}}
else if(pauskey==1){pauskey=0;p14=1;}
}}

else if(flagkey==6){//倒计时开始
flagkey=88;	
Timer0Init_count ++;
if(Timer0Init_count>2){
TR0 = 0;//关定时器
TH0 = 0x0D8;//赋初值
TL0 = 0x0F0;
Timer0Init_count=0;
if(backkey==0){backkey=1;p16=0;}else if(backkey==1){backkey=0;p16=1;}
}}


}
上一集 下一集
正在播放

版权所有丨如未注明 , 均为原创丨本网站采用 BY-NC-SA 协议进行授权 , 转载请注明 出处!
喜欢 (1)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
    仅用来给您发送回复提醒。不会公开!
  • 网址