51单片机时钟显示底层,24小时制

#include "ds1302.h"

//void Delay2ms()        
//{
//    unsigned char i, j;

//    _nop_();
//    _nop_();
//    i = 22;
//    j = 128;
//    do
//    {
//        while (--j);
//    } while (--i);
//}


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

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


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

void Set_RTC(unsigned char *pucRtc)
{
    unsigned char temp;
    
    Write_Ds1302(0x8e, 0X00);
    
    temp = ((pucRtc[0]/10)<<4)+pucRtc[0]%10;
  Write_Ds1302(0x84, temp);//hour
    temp = ((pucRtc[1]/10)<<4)+pucRtc[1]%10;
  Write_Ds1302(0x82, temp);//min
    temp = ((pucRtc[2]/10)<<4)+pucRtc[2]%10;
  Write_Ds1302(0x80, temp);//sec

    Write_Ds1302(0x8e, 0x80);
}

void Read_RTC(unsigned char *pucRtc)
{
    unsigned char temp;
    
  temp = Read_Ds1302(0x85);//hour
    pucRtc[0] = (temp>>4)*10 + (temp & 0xf );
  temp = Read_Ds1302(0x83);//min
    pucRtc[1] = (temp>>4)*10 + (temp & 0xf );    
  temp = Read_Ds1302(0x81);//sec
    pucRtc[2] = (temp>>4)*10 + (temp & 0xf );    
}

H文件

#ifndef __DS1302_H
#define __DS1302_H

#include "STC15F2K60S2.h"
#include "intrins.h"

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


void Set_RTC(unsigned char *pucRtc);
void Read_RTC(unsigned char *pucRtc);

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值