LQB新2024:NE 555,将T0设置成计数模式,将TH和TL等于最大值,参考STCISP设置位下降沿中断

在这里插入图片描述

#include "stc15.h"
#include <intrins.h>
#include <stdio.h>
#include <absacc.h>  

typedef     unsigned char   u8;
typedef     unsigned int    u16;
typedef     unsigned long   u32;


	u8 shi=5,fen=12,miao=25;
	
	
float dsread();

void    WriteNbyte(u8 addr, u8 *p, u8 number);
void    ReadNbyte( u8 addr, u8 *p, u8 number);

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    ReadRTC(void);
void    WriteRTC(void);
void init1302();


//延时函数,位操作,选择支路573,LED操作刷新,ULN2003刷新,数码管的刷新
void delay1ms()		//@11.0592MHz
{
	unsigned char i, j;
	_nop_();
	
	//**********传统的51,仿真用
//	i = 2;
//	j = 199;
	
	//************开发板的51,
	i = 11;
	j = 190;
	
	do
	{
		while (--j);
	} while (--i);
	
	
}

void delayms(int n)		//@11.0592MHz
{
	unsigned char i, j;
  int k;
	for(k=0;k<n;k++)
	{
	  delay1ms();	
	}
}




//***************位操作,置1,清0,取反,判断
u8 bitSet(u8 temp,u8 pos)
{
	temp =temp |(1<<pos);
	return temp;
}
u8 bitReset(u8 temp,u8 pos)
{
	temp &=~(1<<pos);
	return temp;
}

u8 bitTog(u8 temp,u8 pos)
{
  temp =temp ^(1<<pos);
	return temp;
}
u8 bitRead(u8 temp,u8 pos)
{
	u8 res;
  res =(temp>>pos) &0x01;
	return res;
}
//***************位操作,置1,清0,取反,判断

//***************选择573
#define LEDY 1  
#define SMGABCY 2
#define SMGCOMY 3
#define ULNY 4
#define NONE 5

void xz573(u8 x)
{
	switch(x)
	{
		case LEDY:
			P2=(P2&0x1F)|0x80;  //Y4  1000 0000
		break;
		
		case SMGABCY:
			P2=(P2&0x1F)|0xe0;  //Y7  1110 0000
			break;
		case SMGCOMY:
			P2=(P2&0x1F)|0xc0;  //Y6  1100
			break;
		case ULNY:
			P2=(P2&0x1F)|0xa0;  //Y5  1010
			break;	
		
		case NONE:
			P2=P2&0x1F;
			break;
	}
}

LED___________________---
//#define L1 0
//#define L2 1
//#define L3 2

u8 u8LED=0xff;

//void LEDonx(u8 i)  //0
//{
//	u8LED = bitReset(u8LED,i);
//}
//void LEDoffx(u8 i)  //0
//{
//	u8LED = bitSet(u8LED,i);
//}

//void LEDtog(u8 i)
//{
//	u8LED =bitTog(u8LED,i);
//}

//u8 LEDRead(u8 i)
//{
//	u8 sta;
//	sta =bitRead(u8LED,i);
//	return sta;
//}

void LEDdis(u8 temp)
{
	//关闭所有的通道
	xz573(NONE);
	//刷新P0数据
	u8LED=temp;
	P0=u8LED;
	
  xz573(LEDY);
	xz573(NONE);
}

*************蜂鸣器继电器
u8 u8ULN=0x00;

//void JDQonx()  //1
//{
//	u8ULN = bitSet(u8ULN,4);
//}
//void JDQoffx()  //0
//{
//	u8ULN = bitReset(u8ULN,4);
//}

//void FMQonx()  //0
//{
//	u8ULN = bitSet(u8ULN,6);
//}
//void FMQoffx()  //0
//{
//	u8ULN = bitReset(u8ULN,6);
//}

void ULNdis(u8 temp)
{
	//关闭所有的通道
	xz573(NONE);
	//刷新P0数据
	u8ULN=temp;
	
	P0=u8ULN;
  xz573(ULNY);
	xz573(NONE);
}
//**********************************数码管,定时器动态扫描
//位码,段码,显示扫描,定时扫描,字符串翻译到LED8函数;
/*************  本地常量声明    **************/
u8 code t_display[]={                       //标准字库,共阴极,比赛共阳,取反
//   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black  -     H    J    K    L    N    o   P    U     t    G    Q    r   M    y
    0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
    0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};    //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1

u8 code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //注意数码管的数据LED8[0]是不是对应TCOM[0]还是TCOM[8]
//u8 code T_COM[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; //位码
/*************  本地变量声明    **************/
u8  LED8[8];        //显示缓冲
u8  display_index=0;  //显示位索引
//bit B_1ms;          //1ms标志
u8  SMGbuf[16];
/********************** 显示扫描函数 ************************/
//void DisplayScan(void)
//{   
//    Send_595(~T_COM[display_index]);                //输出位码
//    Send_595(t_display[LED8[display_index]]);   //输出段码

//    P_HC595_RCLK = 1;
//    P_HC595_RCLK = 0;                           //锁存输出数据
//    if(++display_index >= 8)    display_index = 0;  //8位结束回0
//}
/********************** 显示扫描函数 ************************/
/********************** 显示扫描函数 ************************/
void DisplayScan(void)
{   
	
		//*******************************完善一下消隐,关闭位选,关闭段码,两个都要。实验发现仅仅关闭位选,不行,仅仅关闭段码可以消隐。
	//位取0,消隐
		 // XBYTE [0xC000] = 0x00;
//		  xz573(NONE);
//			P0=0x00; 
//	    xz573(SMGCOMY);
//	    xz573(NONE);
	
		//段码
//	XBYTE [0xE000] = LED8[display_index];
	
			xz573(NONE);
			P0=0xFF; //记得取反
	    xz573(SMGABCY);
	    xz573(NONE);
	//*******************************************************显示数据,位选,段选
		//位选择
	 // XBYTE [0xC000] = T_COM[display_index];
			xz573(NONE);
			P0=T_COM[display_index]; 
	    xz573(SMGCOMY);
	    xz573(NONE);
	//段码
//	XBYTE [0xE000] = LED8[display_index];
			xz573(NONE);
			P0=LED8[display_index]; //记得取反
	    xz573(SMGABCY);
	    xz573(NONE);
	 //锁存输出数据,这里是8个数码管
    if(++display_index >= 8)    display_index = 0;  //8位结束回0
}
//Pmmmm3.250000
void SMGconv()
{
	u8 i=0,j=0,temp=0;
	while(SMGbuf[i]!=0)
	{
		switch(SMGbuf[i])
		{
			case '0':temp=~t_display[0];break;
			case '1':temp=~t_display[1];break;
			case '2':temp=~t_display[2];break;
			case '3':temp=~t_display[3];break;
			case '4':temp=~t_display[4];break;
			case '5':temp=~t_display[5];break;
			case '6':temp=~t_display[6];break;
			case '7':temp=~t_display[7];break;
			case '8':temp=~t_display[8];break;
			case '9':temp=~t_display[9];break;
			case 'A':temp=~0x77;break;
			case 'B':temp=~0x7C;break;
		  case 'P':temp=~0x73;break;
			case '-':temp=~0x40;break;
			default:temp=0xFF;break;
		}
		if(SMGbuf[i+1]=='.')
		{
			temp &=0x7f;//0111 1111
			i++;
		}
		i++;
		
		LED8[j]=temp;
		j++;
	
	}
}


//***********************按键操作
//int aaa=12;

int longtime=0;
u8 longkeyflag=0;

u8 key10ms=0;
u8 key10msflag=0;

u8 keyRead1(void)
{
	u8 keyx=0;
	if(P30==0)keyx=7;
	else if(P31==0)keyx=6;
	else if(P32==0)keyx=5;
	else if(P33==0)keyx=4;
	return keyx;
}

u8 keyRead4(void)   //开发板
{
	u8 keyx=0;
	
	P44=0;P42=1;P35=1;P34=1;
	if(P30==0)keyx=7;
	else if(P31==0)keyx=6;
	else if(P32==0)keyx=5;
	else if(P33==0)keyx=4;
	
	P44=1;P42=0;P35=1;P34=1;
	if(P30==0)keyx=11;
	else if(P31==0)keyx=10;
	else if(P32==0)keyx=9;
	else if(P33==0)keyx=8;
	
	P44=1;P42=1;P35=0;P34=1;
	if(P30==0)keyx=15;
	else if(P31==0)keyx=14;
	else if(P32==0)keyx=13;
	else if(P33==0)keyx=12;
	
	P44=1;P42=1;P35=1;P34=0;
	if(P30==0)keyx=19;
	else if(P31==0)keyx=18;
	else if(P32==0)keyx=17;
	else if(P33==0)keyx=16;
	
	return keyx;
}

u8 keyRead40(void)   //仿真的,没有P4
{
	u8 keyx=0;
	
	P24=0;P22=1;P35=1;P34=1;
	if(P30==0)keyx=7;
	else if(P31==0)keyx=6;
	else if(P32==0)keyx=5;
	else if(P33==0)keyx=4;
	
	P24=1;P22=0;P35=1;P34=1;
	if(P30==0)keyx=11;
	else if(P31==0)keyx=10;
	else if(P32==0)keyx=9;
	else if(P33==0)keyx=8;
	
	P24=1;P22=1;P35=0;P34=1;
	if(P30==0)keyx=15;
	else if(P31==0)keyx=14;
	else if(P32==0)keyx=13;
	else if(P33==0)keyx=12;
	
	P24=1;P22=1;P35=1;P34=0;
	if(P30==0)keyx=19;
	else if(P31==0)keyx=18;
	else if(P32==0)keyx=17;
	else if(P33==0)keyx=16;
	
	return keyx;
}


   u8  tmp[8]={0,0,0,0,0,0,0,0};
  u8  second;
  u8  minute;
  u8 hour;


void KeyProc()
{

	
	

	u8 keydown;
	u8 keyup;
	u8 keyx;
	static u8 keyold;
	
	if(key10msflag==1)
	{
		key10msflag=0;
	}
	else return;
	
	
	//keyx=keyRead1();//仿真
		//keyx=keyRead40();//仿真
	keyx=keyRead4();//开发板
	
	keydown = keyx&(keyold^keyx);
	keyup =~keyx&(keyold^keyx);
	keyold=keyx;
	
	if(keydown)
	{
		longtime=0;
//		
//		if(keydown==7)aaa=aaa+10;
//		if(keydown==6)aaa=aaa-10;

//		sprintf(SMGbuf,"PPPPPP%2d",(unsigned int)aaa);
//		SMGconv();
	}
	
	if(keyup)
	{
		if(longkeyflag==0)
		{
			if(keyup==7)
			{
				//	aaa=aaa+10;
//				  tmp[0] += 2;
//					tmp[1] +=3;
//					tmp[2] +=4;
//				

//			
//				sprintf(SMGbuf,"%2d-%2d-%2d",(unsigned int)tmp[0],(unsigned int)tmp[1],(unsigned int)tmp[2]);			
//			  SMGconv();
		
//				  sprintf(SMGbuf,"PPPPPP%2d",(unsigned int)aaa);
//			    SMGconv();
				
//注意,写单个字节的时候,需要延时15毫秒以上,实验发现10ms以上合适,比赛发现不能正确读取的话,就改下延时
//					WriteNbyte(0x00, &tmp[0], 1);delayms(5);
//					WriteNbyte(0x01, &tmp[1], 1);delayms(5);
//					WriteNbyte(0x02, &tmp[2], 1);delayms(5);
				
				
				
				
			}
			if(keyup==6)
			{
				//aaa=aaa-10;
					
			//	ReadNbyte(0x00, tmp, 3);delayms(15);
			
				//注意,读取单个字节的时候,需要延时15毫秒以上,实验发现10ms以上合适,比赛发现不能正确读取的话,就改下延时
//				ReadNbyte(0x00, &tmp[0], 1);delayms(5);
//				ReadNbyte(0x01, &tmp[1], 1);delayms(5);
//				ReadNbyte(0x02, &tmp[2], 1);delayms(5);

//				second = tmp[0]+5;
//				minute = tmp[1]+5;
//				hour   = tmp[2]+5;
//				sprintf(SMGbuf,"%2d-%2d-%2d",(unsigned int)second,(unsigned int)minute,(unsigned int)hour);
//			  SMGconv();
				
				miao=miao+1;
				if(miao==60)miao=0;
				fen=fen+1;
				if(fen==60)fen=0;
				shi=shi+1;
				if(shi==24)shi=0;
			sprintf(SMGbuf,"%2d-%2d-%2d",(unsigned int)shi,(unsigned int)fen,(unsigned int)miao);
			  SMGconv();	
				WriteRTC();
				
				
			}
		}
		else longkeyflag=0;
//		sprintf(SMGbuf,"AAAAAA%2d",(unsigned int)keyup);
//		SMGconv();
	}
	
	if(keyold &&(longtime>1000))
	{
		longtime=0;
		longkeyflag=1;
		
		if(keyold==7)
		{
		//aaa=aaa+1;
		}
			
		if(keyold==6)
		{
		//aaa=aaa-1;
		}
			
		//sprintf(SMGbuf,"BBBBBB%2d",(unsigned int)aaa);
		//SMGconv();	
	}
	
}

//float wendu=12.56;
//int sec=12;
int cnt1ms=0;
/* define constants */
#define FOSC 11059200L
#define T1MS (65536-FOSC/12/1000)   //1ms timer calculation method in 12T mode
/********************** Timer0 1ms中断函数 ************************/
void timer0 (void) interrupt 1
{
	TL0 = T1MS;		//仿真软件的传统51单片机需要重新设置定时初值
	TH0 = T1MS>>8;		//设置定时初值
}

u32 Gfre=50;
u32 cnt555=0;
/* Timer0 interrupt routine */
void tm1_isr() interrupt 3
{
		//float wenduVal=12.5;
	
    TL1 = T1MS;                     //reload timer1 low byte
    TH1 = T1MS >> 8;                //reload timer1 high byte
   	cnt1ms++;
		key10ms++;
	  longtime++;
		if(key10ms==10)
		{
			key10msflag=1;
			key10ms=0;
		}
//			
		if(cnt1ms==5000) //边界处理
		{
			cnt1ms=0;
			
			
//	 		sec++;
//			if(sec==59)sec=0;
		}
		if(cnt1ms%200==0) //200ms读取一次脉冲个数处理
		{
			
			
			//Gfre=(float)cnt555;
				Gfre=cnt555*5; 
				cnt555=0;
				sprintf(SMGbuf,"PPP%05d",(unsigned int)Gfre);//36000 65536
				SMGconv();
			
//	 		sec++;
//			if(sec==59)sec=0;
		}
		
	 if(cnt1ms%600==0) //1ms的倍数
		{
//				wendu+=0.01;
//				if(wendu>25.5)
//					wendu=5.2;
			
		
			
			
		}
		
		
		if(cnt1ms%20==0) //20ms的倍数
		{
//			sprintf(SMGbuf,"PPPPP%03d",(unsigned int)sec);
//			SMGconv();
//			
		
			
				KeyProc();//20ms读取一次按键
		}
		if(cnt1ms%2==0) //1ms的倍数
		{
				DisplayScan();  //1ms扫描显示一位	
		}
}


//T0 interrupt service routine
void t0int() interrupt 1           //(location at 000BH)
{
    //P0++;
	
	cnt555++;
	
}

//********************************************************main
void main()
{
	float wenduVal;
	int i;
	
	
	LEDdis(0xff);//开机关闭LED灯
  ULNdis(0x00);//开机关闭继电器和蜂鸣器

	sprintf(SMGbuf,"        ");//熄灭数码管
	SMGconv();
	//开机读取次温度,并且延时1秒,跳过一开始的85
  dsread();
	 delayms(500);
	 delayms(500);
//	AUXR &= 0x7F;		//定时器时钟12T模式
//	//TMOD &= 0xF0;		//开发板设置定时器模式
//	TMOD |= 0x01;		//仿真Proteus设置定时器模式
//  TL0 = T1MS;		//设置定时初值
//	TH0 = T1MS>>8;		//设置定时初值
//	TF0 = 0;		//清除TF0标志
//	TR0 = 1;		//定时器0开始计时

//	ET0=1;//定时器0中断
	
	 TMOD |= 0x10;                    //set timer1 as mode1 (16-bit)
    TL1 = T1MS;                     //initial timer1 low byte
    TH1 = T1MS >> 8;                //initial timer1 high byte
    TR1 = 1;                        //timer1 start running
   
	 ET1 = 1;                        //enable timer1 interrupt
   

		
    TMOD |= 0x04;                    //设置定时器0为16位自动重装载外部记数模式
    TH0 = TL0 = 0xff;               //设置定时器0初始值
    TR0 = 1;                        //定时器0开始工作
    ET0 = 1;                        //开定时器0中断

	EA=1;//单片机总中断
	//init1302();
	WriteRTC();
	ReadRTC();
	
	
	while(1)
	{
//		if(cnt1ms%300==0) //1ms的倍数
//		{
			wenduVal=dsread();
			sprintf(SMGbuf,"P-AB%5.2f",wenduVal);
			SMGconv();
//			
//			 ReadRTC();
//				sprintf(SMGbuf,"%02d-%02d-%02d",(unsigned int)shi,(unsigned int)fen,(unsigned int)miao);
//			  SMGconv();	
//			
//		}	
		//delayms(1000);//将按键处理放在定时中断里面,延时不会影响到按键的识别效果,
//		

	}
	
}

#define SLAW    0xA0   //1010 0000
#define SLAR    0xA1  //1010 0001

sbit    SDA = P2^1; //定义SDA  PIN5
sbit    SCL = P2^0; //定义SCL  PIN6

///****************************开发板/
//void    I2C_Delay(void) //for normal MCS51, delay (2 * dly + 4) T, for STC12Cxxxx delay (4 * dly + 10) T
//{
//    u8  dly;
//   // dly = 22059200 / 2000000UL;        //按2us计算
//	  dly=5;
//    while(--dly)    ;
//}

//******仿真
void    I2C_Delay(void) //for normal MCS51, delay (2 * dly + 4) T, for STC12Cxxxx delay (4 * dly + 10) T
{
    int  dly;
   // dly = 22059200 / 2000000UL;        //按2us计算
	  dly=5;
    while(--dly)    ;
}

/****************************/
void I2C_Start(void)               //start the I2C, SDA High-to-low when SCL is high
{
    SDA = 1;
    I2C_Delay();
    SCL = 1;
    I2C_Delay();
    SDA = 0;
    I2C_Delay();
    SCL = 0;
    I2C_Delay();
}       


void I2C_Stop(void)                 //STOP the I2C, SDA Low-to-high when SCL is high
{
    SDA = 0;
    I2C_Delay();
    SCL = 1;
    I2C_Delay();
    SDA = 1;
    I2C_Delay();
}

void S_ACK(void)              //Send ACK (LOW)
{
    SDA = 0;
    I2C_Delay();
    SCL = 1;
    I2C_Delay();
    SCL = 0;
    I2C_Delay();
}

void S_NoACK(void)           //Send No ACK (High)
{
    SDA = 1;
    I2C_Delay();
    SCL = 1;
    I2C_Delay();
    SCL = 0;
    I2C_Delay();
}
        
void I2C_Check_ACK(void)         //Check ACK, If F0=0, then right, if F0=1, then error
{
    SDA = 1;
    I2C_Delay();
    SCL = 1;
    I2C_Delay();
    F0  = SDA;
    SCL = 0;
    I2C_Delay();
}

/****************************/
void I2C_WriteAbyte(u8 dat)     //write a byte to I2C
{
    u8 i;
    i = 8;
    do
    {
        if(dat & 0x80)  SDA = 1;
        else            SDA = 0;
        dat <<= 1;
        I2C_Delay();
        SCL = 1;
        I2C_Delay();
        SCL = 0;
        I2C_Delay();
    }
    while(--i);
}

/****************************/
u8 I2C_ReadAbyte(void)          //read A byte from I2C
{
    u8 i,dat;
    i = 8;
    SDA = 1;
    do
    {
        SCL = 1;
        I2C_Delay();
        dat <<= 1;
        if(SDA)     dat++;
        SCL  = 0;
        I2C_Delay();
    }
    while(--i);
    return(dat);
}

/****************************/
void WriteNbyte(u8 addr, u8 *p, u8 number)          /*  WordAddress,First Data Address,Byte lenth   */
                                                            //F0=0,right, F0=1,error
{
    I2C_Start();
    I2C_WriteAbyte(SLAW);
    I2C_Check_ACK();
    if(!F0)
    {
        I2C_WriteAbyte(addr);
        I2C_Check_ACK();
        if(!F0)
        {
            do
            {
                I2C_WriteAbyte(*p);     p++;
                I2C_Check_ACK();
                if(F0)  break;
            }
            while(--number);
        }
    }
    I2C_Stop();
}


/****************************/
void ReadNbyte(u8 addr, u8 *p, u8 number)       /*  WordAddress,First Data Address,Byte lenth   */
{
    I2C_Start();
    I2C_WriteAbyte(SLAW);
    I2C_Check_ACK();
    if(!F0)
    {
        I2C_WriteAbyte(addr);
        I2C_Check_ACK();
        if(!F0)
        {
            I2C_Start();
            I2C_WriteAbyte(SLAR);
            I2C_Check_ACK();
            if(!F0)
            {
                do
                {
                    *p = I2C_ReadAbyte();   p++;
                    if(number != 1)     S_ACK();    //send ACK
                }
                while(--number);
                S_NoACK();          //send no ACK
            }
        }
    }
    I2C_Stop();
}


/****************************/
sbit DQ=P1^4;
//
void Delay_OneWire(unsigned int t)  
{
	unsigned char i;
	while(t--){
		for(i=0;i<12;i++);//开发板的,12
	}
}

//
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);
}

//
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;
}

//
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;
}

float dsread()
{
	float wenduH,wenduL=0;
	float wendu;
	int temp;
	char MSB,LSB;
	
	init_ds18b20();
	Write_DS18B20(0xCC);
	//Write_DS18B20(0x4E);
  Write_DS18B20(0x44);
	//delayms(750);
	init_ds18b20();
  Write_DS18B20(0xCC);
	Write_DS18B20(0xBE);
	LSB=Read_DS18B20();
	MSB=Read_DS18B20();
	wenduH=bitRead(MSB,2)*64+bitRead(MSB,1)*32+bitRead(MSB,0)*16;
	wenduL=bitRead(LSB,7)*8+bitRead(LSB,6)*4+bitRead(LSB,5)*2+bitRead(LSB,4)*1+bitRead(LSB,3)*0.5+bitRead(LSB,2)*0.25;
	wendu=wenduH+wenduL;
	return wendu;
}

//****************************
/*	# 	DS1302代码片段说明
	1. 	本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
	2. 	参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
		中对单片机时钟频率的要求,进行代码调试和修改。
*/								
sbit SCK2=P1^7;
sbit SDA2=P2^3;
sbit RST2=P1^3;
//
void Write_Ds1302(unsigned  char temp) 
{
	unsigned char i;
	for (i=0;i<8;i++)     	
	{ 
		SCK2 = 0;
		SDA2 = temp&0x01;
		temp>>=1; 
		SCK2=1;
	}
}   

//
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )     
{
 	RST2=0;	_nop_();
 	SCK2=0;	_nop_();
 	RST2=1; 	_nop_();  
 	Write_Ds1302(address);	
 	Write_Ds1302(dat);		
 	RST2=0; 
}

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

/********************** 读RTC函数 ************************/
void    ReadRTC(void)
{
	 u8 shix,fenx,miaox;
		shix =Read_Ds1302_Byte(0x85);
		fenx =Read_Ds1302_Byte(0x83);
		miaox =Read_Ds1302_Byte(0x81);
	
	  miao = ((miaox >> 4) & 0x07) * 10 + (miaox & 0x0f);
    fen = ((fenx >> 4) & 0x07) * 10 + (fenx& 0x0f);
    shi   = ((shix >> 4) & 0x03) * 10 + (shix & 0x0f);   
}

/********************** 写RTC函数 ************************/
void    WriteRTC(void)
{
    u8  tmp[3];
    tmp[0] = ((miao / 10) << 4) + (miao % 10);
    tmp[1] = ((fen / 10) << 4) + (fen % 10);
    tmp[2] = ((shi / 10) << 4) + (shi % 10); 
		Write_Ds1302_Byte(0x8E,0x00);//读取时钟,需要关闭写保护,允许写数据
		Write_Ds1302_Byte(0x80, tmp[0] ) ;
		Write_Ds1302_Byte(0x82, tmp[1] ) ;
		Write_Ds1302_Byte(0x84, tmp[2] ) ;
		Write_Ds1302_Byte(0x8E,0x80);//写完,打开写保护,不允许改变数据
}
//void init1302()
//{
//	   Write_Ds1302_Byte(0x8E,0x00);
//	
//	   Write_Ds1302_Byte(0x80,0x56);
//     Write_Ds1302_Byte(0x82,0x50);
//	   Write_Ds1302_Byte(0x84,0x23);
//	
//	   Write_Ds1302_Byte(0x86,0x25);
//	   Write_Ds1302_Byte(0x88,0x12);
//	   Write_Ds1302_Byte(0x8A,0x07);
//		 Write_Ds1302_Byte(0x8C,0x22);
//	
//	   Write_Ds1302_Byte(0x8E,0x80);
//}


//*********************************************************

    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值