第八届省赛蓝桥杯单片机组:电子钟

第八届省赛蓝桥杯单片机组:电子钟

遇见的问题

1.三行代码trg使用,按键一次变量却自加多次
原因:trg 没有放在10ms一次keyflag检测中,导致这10ms main函数已经运行多次,trg一直没有变,所以变量多次自加。
而如果放在10ms检测中执行,函数的行为只用重复一次,第2次因为readdat&(cont^readdat) =0 ,所以不会被检测

2.闹钟开始闪烁的问题,以及后来闪烁超过一分钟
初始时shi,fen,maio=0 就与闹钟初试值一样,(在这里插入图片描述
虽然不太清楚为什么最初闹钟会响,此时我已经设置的初始23 59 50
已经和闹钟不一样了害,但是我将 shi,fen,miao分别定义时就赋值 23 59 50 就不会在初始时再led 闪了
有关后面无任何操作闹钟闪亮超过1Min的问题,正式因为前面已经开始闹钟,而我的num5s没有进行清零操作,也就是他要等65536ms才会凊0重新计时cheng我设置5000或者50…

3.有关数字溢出的问题
在这里插入图片描述
这样就可以0-23切换啦

代码

main.c

#include<stc15f2k60s2.h>
#include<ds1302.h>
#include<onewire.h>
#define  keypress P3
#define u8 unsigned char  
#define u16 unsigned int
  
u8 code d_display[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
u8 code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};  
typedef unsigned char BYTE;
typedef unsigned int WORD;

u8 smg_count, smg_i=0, key_count ,trg,cont; 
u8 time_index=0;  //时钟模式
u8 clock_index = 0;
u8 shi=23,fen=59,miao=50;
u8 shii = 0,fenn = 0, miaoo = 0;
u8 table1[8],table2[8],table3[8];
bit smg_flag, light_flag, key_flag ;
bit led_light, clock_start;
u16 num5s;
u8 led_count;
bit wendu_dis;
u8 temp;
/* define constants */
#define FOSC 11059200L

#define T1MS (65536-FOSC/12/1000)   //1ms timer calculation method in 12T mode

/* define SFR */
sbit TEST_LED = P1^0;               //work LED, flash once per second

/* define variables */
WORD count;                         //1000 times counter




void time0init();
void smg_display();
void keyscan();
void s6();
void s5();
void clock();
void s4();

void main()
{
   time0init();
	P2 = (P2&0x1f)|0xa0; P06 = 0; P2 =  (P2&0x1f);
   set_time(23,59,50);
    
    while (1)
		{
		 smg_display();
			 clock();   //闹钟响
			if(key_flag)
			{
				EA=0;
				temp =r_temp();
				EA=1;
				key_flag = 0;
        EA=0;
				shi = Read_Ds1302_Byte (0x85)/16*10 + (Read_Ds1302_Byte (0x85)%16);
				fen = Read_Ds1302_Byte (0x83)/16*10 + (Read_Ds1302_Byte (0x83)%16);;
				miao = Read_Ds1302_Byte (0x81)/16*10 + (Read_Ds1302_Byte (0x81)%16);;
				EA=1;
			   keyscan();
			if(cont & 0x08)  wendu_dis = 1;
    	else             wendu_dis = 0;
				       if(trg !=0 )
							{
								switch(trg)
								{
												case 0x01:  time_index++; if(time_index == 4) time_index = 0; break;
												case 0x02:  clock_index++; if( clock_index== 4) clock_index = 0; break;
												case 0x04:  s5(); break;
												case 0x08: s4(); break;
												default: break;
								}
					    }
				}
		}
}
void time0init()
{
	 TMOD = 0x01;                    //set timer0 as mode1 (16-bit)
    TL0 = T1MS;                     //initial timer0 low byte
    TH0 = T1MS >> 8;                //initial timer0 high byte
    TR0 = 1;                        //timer0 start running
    ET0 = 1;                        //enable timer0 interrupt
    EA = 1;                         //open global interrupt switch
    count = 0;                      //initial counter
}

/* Timer0 interrupt routine */
void tm0_isr() interrupt 1 using 1
{
    TL0 = T1MS;                     //reload timer0 low byte
    TH0 = T1MS >> 8;                //reload timer0 high byte
	 if(clock_start)
	 {
		 num5s++;
		 if(num5s == 5000 || trg != 0)
		 {
			 clock_start = 0;
			 num5s = 0;
		 }
	 }
    if (count-- == 0)               //1ms * 1000 -> 1s
    {
        count = 1000;   
        light_flag = ~light_flag ;			
    }
		 if (led_count-- == 0)               //1ms * 1000 -> 1s
    {
        led_count = 200;   
        led_light = ~led_light ;			
    }
		
		 if (smg_count-- == 0)               //1ms 
    {
        smg_count = 2;       
        smg_flag = ~smg_flag;			  
    }
		  if (key_count-- == 0)               //1ms * 1000 -> 1s
    {
        key_count = 10;     
        key_flag = 1;			
    }
}

void smg_display()
{
	
	if(smg_flag)
	{
		  s6();
			smg_flag = 0;
			P2 = (P2&0x1f)|0xc0; P0 = T_COM[smg_i]; P2 = (P2&0x1f);
			P2 = (P2&0x1f)|0xe0; P0 = ~table1[smg_i]; P2 = (P2&0x1f);
			smg_i++; 
			smg_i &=0x07;    //&111   
	}
}

void keyscan()
{
	u8 readdat;
	readdat = keypress^0xff;
	trg = readdat&(readdat^cont);
	cont = readdat;
}

void s6()
{
	if(time_index == 0 && clock_index == 0)
	{
		if(wendu_dis)
		{
			table1[0] = 0,table1[1] = 0;table1[2] = 0,table1[3] = 0;table1[4] = 0;
			table1[5] = d_display[temp/10%10];   table1[6] = d_display[temp%10],table1[7] = 0x39;
		}
		else
		{
             table1[0] = d_display[shi/10],table1[1] = d_display[shi%10],table1[2] = 0x40;
						 table1[3] = d_display[fen/10],table1[4] = d_display[fen%10],table1[5] = 0x40;
						 table1[6] = d_display[miao/10],table1[7] = d_display[miao%10];
		}
	}
	else if(time_index != 0) //时钟设置状态
		{
			   clock_index = 0; //避免干扰
				if(light_flag)      //1szhengcchang 1s light
				{
						 table1[0] = d_display[shi/10],table1[1] = d_display[shi%10],table1[2] = 0x40;
						 table1[3] = d_display[fen/10],table1[4] = d_display[fen%10],table1[5] = 0x40;
						 table1[6] = d_display[miao/10],table1[7] = d_display[miao%10];
						
				}
	
				else
				{
					switch(time_index)
					{
					case 1: table1[0] = 0,table1[1] = 0;break;
					case 2: table1[3] = 0,table1[4] = 0;break;
					case 3: table1[6] = 0; table1[7] = 0; break;
					}
				}
	   }
		else if(clock_index != 0)
		{
			       time_index = 0;
			       table1[0] = d_display[shii/10],table1[1] = d_display[shii%10],table1[2] = 0x40;
						 table1[3] = d_display[fenn/10],table1[4] = d_display[fenn%10],table1[5] = 0x40;
						 table1[6] = d_display[miaoo/10],table1[7] = d_display[miaoo%10];
		}
}

void s5()
{
	if(clock_index !=0)
	{
		switch(clock_index)
		{
			case 1:shii++; if(shii ==24) shii = 0;break;
			case 2:fenn++; if(fenn ==60) fenn = 0;break;
			case 3:miaoo++; if(miaoo ==60) miaoo = 0;break;
		}
	}
	else if(time_index !=0)
	{
		switch(time_index)
		{
			case 1:shi++; if(shi ==24) shi = 0; set_time(shi,fen,miao); break;
			case 2:fen++; if(fen ==60) fen = 0; set_time(shi,fen,miao); break;
			case 3:miao++; if(miao ==60) miao = 0;  set_time(shi,fen,miao); break;
		}
	}
	
}

void s4()
{
	if(clock_index !=0)
	{
		switch(clock_index)
		{
			case 1:shii--; if(shii >24) shii = 23 ;set_time(shi,fen,miao); break;
			case 2:fenn--; if(fenn >60) fenn = 59; set_time(shi,fen,miao); break;
			case 3:miaoo--; if(miaoo >60) miaoo = 59; set_time(shi,fen,miao); break;
		}
	}
	else if(time_index !=0)
	{
		switch(time_index)
		{
			case 1:shi--; if(shi >24) shi = 23; set_time(shi,fen,miao); break;
			case 2:fen--; if(fen >60) fen = 59; set_time(shi,fen,miao); break;
			case 3:miao--; if(miao >60) miao = 59;  set_time(shi,fen,miao); break;
		}
	}
	
}

void clock()
{
	if(shi == shii && fen == fenn && miao ==miaoo)
	{
		clock_start = 1;
	}
	if(clock_start)       //开始闹钟
	{
		if(led_light)
		{
	   P2 = (P2&0x1f)|0x80; P0 = ~0x01; P2 = (P2&0x1f);
		}
		else
		{
			P2 = (P2&0x1f)|0x80; P0 = 0xff; P2 = (P2&0x1f);
		}
	}
	else
	{
		P2 = (P2&0x1f)|0x80; P0 = 0xff; P2 = (P2&0x1f);
	}
}

ds1302.c

/*
  ????: DS1302????
  ????: Keil uVision 4.10 
  ????: CT107????????? 8051,12MHz
  ?    ?: 2011-8-9
*/

#include <reg52.h>
#include <intrins.h>
#define u8 unsigned char  
#define u16 unsigned int
sbit SCK=P1^7;		
sbit SDA=P2^3;		
sbit RST = P1^3;   // DS1302??												

void Write_Ds1302(unsigned  char temp) 
{
	unsigned char i;
	for (i=0;i<8;i++)     	
	{ 
		SCK=0;
		SDA=temp&0x01;
		temp>>=1; 
		SCK=1;
	}
}   

void Write_Ds1302_Byte( unsigned char address,unsigned char dat )     
{
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1; 	_nop_();  
 	Write_Ds1302(address);	
 	Write_Ds1302(dat);		
 	RST=0; 
}

unsigned char Read_Ds1302_Byte ( unsigned char address )
{
 	unsigned char i,temp=0x00;
	//EA = 0;
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1;	_nop_();
 	Write_Ds1302(address);
 	for (i=0;i<8;i++) 	
 	{		
		SCK=0;
		temp>>=1;	
 		if(SDA)
 		temp|=0x80;	
 		SCK=1;
	} 
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
	SCK=1;	_nop_();
	SDA=0;	_nop_();
	SDA=1;	_nop_();
	//EA = 1;
	return (temp);			
}

void set_time(u8 shi,u8 fen,u8 miao)
{
	Write_Ds1302_Byte(0x8e,0x00);
	Write_Ds1302_Byte(0x84,shi/10*16+(shi%10));
	Write_Ds1302_Byte(0x82,fen/10*16+(fen%10));
	Write_Ds1302_Byte(0x80,miao/10*16+(miao%10));	
  Write_Ds1302_Byte(0x8e,0x80);
}

ds1302.h

#ifndef __DS1302_H
#define __DS1302_H
#define u8 unsigned char  
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
void set_time(u8 shi,u8 fen,u8 miao);
#endif

onewire.c

/*
  ????: ???????
  ????: Keil uVision 4.10 
  ????: CT107?????????(????12MHz) STC89C52RC???
  ?    ?: 2011-8-9
*/
#include "reg52.h"
#define u8 unsigned char  
sbit DQ = P1^4;  //?????

//???????
void Delay_OneWire(unsigned int t)  //STC89C52RC
{
	u8 i;
	while(t--)
	{
		for(i=0;i<8;i++);
	}//括号
}

//??????DS18B20?????
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(5);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(5);
}

//?DS18B20??????
unsigned char Read_DS18B20(void)
{
	unsigned char i;
	unsigned char dat;
  
	for(i=0;i<8;i++)
	{
		DQ = 0;
		dat >>= 1;
		DQ = 1;
		if(DQ)
		{
			dat |= 0x80;
		}	    
		Delay_OneWire(5);
	}
	return dat;
}

//DS18B20?????
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(12);
  	DQ = 0;
  	Delay_OneWire(80);
  	DQ = 1;
  	Delay_OneWire(10); 
    initflag = DQ;     
  	Delay_OneWire(5);
  
  	return initflag;
}

u8 r_temp()
{
	u8 temp,low,high;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	Delay_OneWire(20);
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	
	low = Read_DS18B20();
	high = Read_DS18B20();
	temp = high<<4 | low>>4;
	return temp;
}




onewire.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H
#define u8 unsigned char  

unsigned char rd_temperature(void);  //; ;
u8 r_temp();
#endif

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值