基于stc15f2k60s2芯片单片机编程(可调时钟)

Ds1302可调时钟,按下S7调节时分秒,按下S6加1,按下S5减1

main.c

#include 
#include 
#include 

sbit s7=P3^0;
sbit s6=P3^1;
sbit s5=P3^2;


void Timer0Init(void);
void Delay5ms();
void xianshi();
void anjian();

unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char display[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};

unsigned wei;


void main()
{
 EA=1;ET0=1;Timer0Init();
   P2=0XA0;P0=0X00;P2=0X1F;
	DS_init();
	while(1)
		{
			anjian();
			xianshi();
		 if(TIME[2]>=0x24)
			TIME[2]=0x00;
		if(TIME[1]>=0x60)
			TIME[1]=0;
		if(TIME[0]>=0x60)
			TIME[0]=0;
	
		}	


}

void xianshi()
{
	if(wei==0)
	{
		
		DS_get();
		display[0]=tab[TIME[2]/16];
		display[1]=tab[TIME[2]%16];
		display[3]=tab[TIME[1]/16];
		display[4]=tab[TIME[1]%16];
		display[6]=tab[TIME[0]/16];
		display[7]=tab[TIME[0]%16];
	}

}


void Timer0Init(void)		//2毫秒@11.0592MHz
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x9A;		//设置定时初值
	TH0 = 0xA9;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
}

void shuma() interrupt 1
	{
		unsigned char i;
		unsigned int p;
		
		P2=0XE0;
		P0=0XFF;
		P2=0X1F;
		
		P2=0XC0;
		P0=1<<i;
		P2=0X1F;
		
		P2=0XE0;
		P0=display[i];
		P2=0X1F;
		i++;
		if(i>8)i=0;
	
	p++;
	if(p==250)
	{
		if(wei==1){display[0]=display[1]=0xff;}
		if(wei==2){display[3]=display[4]=0xff;}
		if(wei==3){display[6]=display[7]=0xff;}
		display[2]=display[5]=0XfF;
	}
	else if(p==500)
	{
		p=0;
		if(wei==1){display[0]=tab[TIME[2]/16];display[1]=tab[TIME[2]%16];}
		if(wei==2){display[3]=tab[TIME[1]/16];display[4]=tab[TIME[1]%16];}
		if(wei==3){display[6]=tab[TIME[0]/16];display[7]=tab[TIME[0]%16];}
		display[2]=display[5]=0XbF;
	}
}



void anjian()
{
	if(s7==0)
	{
		Delay5ms();
		if(s7==0)
		{
			wei++;
			if(wei>=4){DS_init();wei=0;}
		
		}
		while(!s7);
	}
	
		if(s6==0)
	{
		Delay5ms();
		if(s6==0)
		{
			if(wei==1){TIME[2]++;	}
			if(wei==2){TIME[1]++;	}
			if(wei==3){TIME[0]++;	}
			if(TIME[0]%16>9)TIME[0]=TIME[0]+6;
      if(TIME[1]%16>9)TIME[1]=TIME[1]+6;
      if(TIME[2]%16>9)TIME[2]=TIME[2]+6;	
		}
		while(!s6);
	}
	
		if(s5==0)
	{
		Delay5ms();
		if(s5==0)
		{
			if(wei==1){TIME[2]--;	}
			if(wei==2){TIME[1]--;}
			if(wei==3){TIME[0]--;	}
			if(TIME[0]%16>9)TIME[0]=TIME[0]-6;
      if(TIME[1]%16>9)TIME[1]=TIME[1]-6;
      if(TIME[2]%16>9)TIME[2]=TIME[2]-6;		
		}
		while(!s5);
	}

}



void Delay5ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 54;
	j = 199;
	do
	{
		while (--j);
	} while (--i);
}

ds1302.h

#include

sbit SCK=P1^7;		
sbit SDA=P2^3;		
sbit RST = P1^3;   // DS1302??												


unsigned char code READ_RTCADDR[7]={0X81,0X83,0X85,0X87,0X89,0X8B,0X8D};
unsigned char code WRITE_RTCADDR[7]={0x80,0X82,0X84,0X86,0X88,0X8C,0X8E};
 char TIME[7]={0x50,0x59,0x23};
 
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;
 	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_();
	return (temp);			
}
void DS_init()
{
	unsigned char n;
	Write_Ds1302_Byte(0x8e,0x00);//关闭写保护
  	for(n=0;n<3;n++)
	Write_Ds1302_Byte(WRITE_RTCADDR[n],TIME[n]);
  Write_Ds1302_Byte(0x8e,0x80);
}

void DS_get()
{
	unsigned char n;
	for(n=0;n<3;n++)
  TIME[n]=Read_Ds1302_Byte (READ_RTCADDR[n]);
}



ds1302.h

#ifndef __DS1302_H_
#define __DS1302_H_

#include
#include



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 DS_init();
void DS_get();
extern char TIME[7];


//---??????--//


#endif

你可能感兴趣的:(基于stc15f2k60s2芯片单片机编程(可调时钟))