⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds1302[1].c.txt

📁 430的DS1302的驱动,知识源于网络,
💻 TXT
字号:
ds1302程序(430版)根据51改写

都是过眼烟云,贡献出来,大家分享。
除了DS1302BurstRread(),即burst read clock reg 不能出来,其余的都试过了。
还请高人们解决一下。

/********************************************************/
// 实时时钟模块 时钟芯片型号:DS1302 */
//
/********************************************************/
//  DSSCLK = BIT0;
//  DSIO = BIT1;
//  DSRST = BIT2;
//SCLK = P3.0; /*实时时钟时钟线引脚 */
//IO = P3.1;   /*实时时钟数据线引脚 */
//RST = P3.2;  /*实时时钟复位线引脚 */

/*******************************************************
* Function 1
* 名称:1302InputByte
* 说明:
* 功能: 往DS1302写入1Byte数据
* 调用:
* 输入: uchDa 写入的数据
* 返回值: 无
********************************************************/
#include <MSP430x14x.h>
void DS1302InputByte(unsigned char uchDa)
{
   unsigned char i;
   unsigned char uchWData;
   uchWData = uchDa;
   P3DIR |= (DSIO+DSSCLK);
   for(i=8; i>0; i--)
   {
      if ((uchWData&BIT0)==BIT0)
          P3OUT |= DSIO;
      else
          P3OUT &= ~DSIO;

      P3OUT &= ~DSSCLK;       //data input as sclk rising
      _NOP();
      P3OUT |= DSSCLK;
      uchWData = uchWData >> 1;
    }
}
/*********************************************************
*  Function 2
* 名称:1302OutputByte
* 说明:
* 功能: 从DS1302读取1Byte数据
* 调用:
* 输入:
* 返回值:ReadData
***********************************************************************/
unsigned char DS1302OutputByte(void)
{
   unsigned char i,ReadData;
   P3DIR |= DSSCLK;

   P3DIR |= DSIO;
   P3OUT |= DSIO;
   P3DIR &= ~DSIO;

   for(i=0; i<8; i++)
   {
       P3OUT |= DSSCLK;     //read from 1302 as sclk falling down
       _NOP();
       P3OUT &= ~DSSCLK;
       ReadData >>= 1;
       if ((P3IN&DSIO)==DSIO)
            ReadData |= BIT7;
       //else
       //     ReadData &= ~BIT7;


    }

    return(ReadData);
}
/********************************************************************
* Function 3
* 名称:Write1302
* 说明: 先写地址,后写命令/数据
* 功能: 往DS1302写入数据
* 调用:1302InputByte()
* 输入: uchAddr: DS1302地址, uchDa: 要写的数据
* 返回值: 无
***********************************************************************/
void Write1302(unsigned char uchAddr, unsigned char uchDa)
{
     uchAddr &= 0xfe;   //write address: bit0 is 0
     P3DIR |= (DSRST+DSSCLK);
     P3OUT &= ~DSRST;    //first rst set 0
     P3OUT &= ~DSSCLK;   //sclk set 0
     _NOP();
     _NOP();
     P3OUT |=DSRST;         //then rst set 1, enable operate 1302
     _NOP();
     _NOP();
     _NOP();
     DS1302InputByte(uchAddr);  //firt write address
     DS1302InputByte(uchDa);    //then write data
     P3OUT |= DSSCLK;       //sclk set 1
     P3OUT &= ~DSRST;
}
/********************************************************************
* Function 4
* 名称: Read1302
* 说明: 先写地址,后读命令/数据
* 功能: 读取DS1302某地址的数据
* 调用: 1302InputByte() , 1302OutputByte()
* 输入: uchAddr: DS1302地址
* 返回值: uchDa :读取的数据
***********************************************************************/
unsigned char Read1302(unsigned char uchAddr)
{
      unsigned char uchDa;
      uchAddr |= BIT0;  //read address: bit0 is 1
      P3DIR |= DSRST;
      P3DIR |= DSSCLK;
      P3OUT &= ~DSRST;
      P3OUT &= ~DSSCLK;
      _NOP();
      _NOP();
      P3OUT |= DSRST;
      _NOP();
      _NOP();
      DS1302InputByte(uchAddr);  //write address
      uchDa = DS1302OutputByte();   //read 1 Byte
      P3OUT |= DSSCLK;
      P3OUT &= ~DSRST;

      return(uchDa);
}
/********************************************************************
* Function 5
* 名称: 1302BurstWrite
* 说明: 先写地址,后写数据(时钟多字节方式)
* 功能: 往DS1302写入时钟数据(多字节方式)
* 调用: 1302InputByte() ,Write1302(unsigned char uchAddr, unsigned char uchDa)
* 输入: ptrSecDa: 时钟数据地址 格式为: 秒 分 时 日 月 星期 年 控制
* 8Byte (BCD码) 1B 1B 1B 1B 1B 1B 1B 1B
* 返回值: 无
***********************************************************************/
void DS1302BurstWrite(unsigned char *ptrSecDa)
{
      unsigned char i;
      P3DIR |= (DSRST+DSSCLK);
      Write1302(0x8e,0x00);   //control register =0,enable write
      P3OUT &= ~DSRST;
      P3OUT &= ~DSSCLK;
      _NOP();
      _NOP();
      P3OUT |= DSRST;
      _NOP();
      _NOP();
      DS1302InputByte(0xbe);
      for(i=8;i>0;i--)
      {
           DS1302InputByte(*ptrSecDa);
           ptrSecDa++;
      }
      P3OUT |= DSSCLK;
      P3OUT &= ~DSRST;
}
/********************************************************************
* Function 6
* 名称: 1302BurstRread
* 说明: 先写地址,后读命令/数据(时钟多字节方式)
* 功能: 读取DS1302时钟数据
* 调用: 1302InputByte() , 1302OutputByte()
* 输入: ptrSecDa: 时钟数据地址 格式为: 秒 分 时 日 月 星期 年
* 7Byte (BCD码) 1B 1B 1B 1B 1B 1B 1B
* 返回值: uchDa :读取的数据
***********************************************************************/
void DS1302BurstRread(unsigned char *ptrSecDa)
{
     unsigned char i;
     P3DIR |= (DSRST+DSSCLK);
     P3OUT &= ~DSRST;
     P3OUT &= ~DSSCLK;
     _NOP();
     _NOP();
     _NOP();
     P3OUT |= DSRST;
     _NOP();
     _NOP();
     DS1302InputByte(0xbf);   //clock burst address
     for(i=8;i>0;i--)
     {
           *ptrSecDa = DS1302OutputByte();
           ptrSecDa++;
      }
      P3OUT |= DSSCLK;
      P3OUT &= ~DSRST;
}
/********************************************************************
* Function 7
* 名称: BurstW1302R
* 说明: 先写地址,后写数据(寄存器多字节方式)
* 功能: 往DS1302寄存器数写入数据(多字节方式)
* 调用: 1302InputByte()
* 输入: ptrReDa: 寄存器数据地址
* 返回值: 无
***********************************************************************/
void BurstW1302R(unsigned char *ptrReDa)
{
     unsigned char i;
     P3DIR |= (DSRST+DSSCLK);
     Write1302(0x8e,0x00);    //WP=0,enable write
     P3OUT &= ~DSRST;
     P3OUT &= ~DSSCLK;
     _NOP();
     _NOP();
     P3OUT |= DSRST;
     _NOP();
     _NOP();
     DS1302InputByte(0xfe);   //0xfe ram burst address
     for(i=31;i>0;i--)
     {
          DS1302InputByte(*ptrReDa);
          ptrReDa++;
      }
      P3OUT |= DSSCLK;
      P3OUT &= ~DSRST;
}
/********************************************************************
* Function 8
* 名称: BurstR1302R
* 说明: 先写地址,后读命令/数据(寄存器多字节方式)
* 功能: 读取DS1302寄存器数据
* 调用: 1302InputByte() , 1302OutputByte()
* 输入: ptrReDa: 寄存器数据地址
* 返回值: 无
***********************************************************************/
void BurstR1302R(unsigned char *ptrReDa)
{
      unsigned char i;
      P3DIR |= (DSRST+DSSCLK);
      P3OUT &= ~DSRST;
      P3OUT &= ~DSSCLK;
      _NOP();
      _NOP();
      P3OUT |= DSRST;
      _NOP();
      _NOP();
      DS1302InputByte(0xff);   //clock busrt
      for(i=31;i>0;i--)
      {
           *ptrReDa = DS1302OutputByte();  //read 1 Byte
           ptrReDa++;

       }
       P3OUT |= DSSCLK;
       P3OUT &= ~DSRST;
}
/********************************************************************
*  Function 9
* 名称: Set1302
* 说明:
* 功能: 设置初始时间
* 调用: Write1302()
* 输入: ptrSecDa: 初始时间地址。初始时间格式为: 秒 分 时 日 月 星期 年
* 7Byte (BCD码) 1B 1B 1B 1B 1B 1B 1B
* 返回值: 无
***********************************************************************/
void Set1302(unsigned char *ptrSecDa)
{
    unsigned char i;
    unsigned char uchAddr = 0x80;
    Write1302(0x8e,0x00); /* 控制命令,WP=0,eanble write*/
    for(i =7;i>0;i--)
    {
         Write1302(uchAddr,*ptrSecDa);
         ptrSecDa++;
         uchAddr +=2;
    }
    Write1302(0x8e,0x80);   //WP=1,write protec
}
/********************************************************************
* Function 10
* 名称: Get1302
* 说明:
* 功能: 读取DS1302当前时间
* 调用: Read1302()
* 输入: uchCurTime: 保存当前时间地址。当前时间格式为: 秒 分 时 日 月 星期 年
* 7Byte (BCD码) 1B 1B 1B 1B 1B 1B 1B
* 返回值: 无
***********************************************************************/
void Get1302(unsigned char uchCurTime[])
{
     unsigned char i;
     unsigned char uchAddr = 0x81;
     for (i=0;i<7;i++)
    {
         uchCurTime = Read1302(uchAddr);/*格式为: 秒 分 时 日 月 星期 年 */
         uchAddr += 2;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -