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

📄 ds1302.c

📁 用于记述外寄生内 用于记述外寄生内
💻 C
字号:
//ICC-AVR application builder : 2004-9-7 15:48:55
// Target : M16
// Crystal: 1.0000Mhz

#include <iom16v.h>
#include <macros.h>

void port_init(void)
{
 PORTA = 0xFF;
 DDRA  = 0x00;
 PORTB = 0xFF;
 DDRB  = 0x00;
 PORTC = 0xff; //m103 output only
 DDRC  = 0xFF;
 PORTD = 0xFF;
 DDRD  = 0x00;
}

//call this routine to initialise all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialised
}

/*********************************************************************/
/* 实时时钟模块 时钟芯片型号:DS1302 */
/*********************************************************************/
#define	T_CLK 0
#define	T_IO  1
#define	T_RST 7
/******************************************************************** */

#define	SETBIT(x,y) (x|=(1<<y))      //set bit y in byte x
#define	CLRBIT(x,y) (x&=(~(1<<y)))   //clear bit y in byte x
#define	CHKBIT(x,y) (x&(1<<y))       //check bit y in byte x
/******************************************************************** */


void nop(void)
{
 char i;
 for (i=0;i<1;i++)
  ;
}

void initialize_1302(void)
{
 write_1302(0x8e,0x00);
 write_1302(0x90,0xA5);
 write_1302(0x80,0x00);
}
/********************************************************************
* 名称: ds1302_write_a_byte
* 说明:
* 功能: 往DS1302写入1Byte数据
* 调用:
* 输入: ucDa 写入的数据
* 返回值: 无
***********************************************************************/
void ds1302_write_a_byte(unsigned char ucDa)
{
    unsigned char i;
    for(i=8; i>0; i--)
    {
	    CLRBIT(PORTC,T_CLK);
		if (ucDa&1) SETBIT(PORTC,T_IO);
		    else CLRBIT(PORTC,T_IO);
		SETBIT(PORTC,T_CLK);
		ucDa>>=1;
    }
}

/********************************************************************
*
* 名称: unsigned char ds1302_read_a_byte
* 说明:
* 功能: 从DS1302读取1Byte数据
* 调用:
* 输入:
* 返回值: t
***********************************************************************/
unsigned char ds1302_read_a_byte(void)
{
    unsigned char i,t;
	CLRBIT(DDRC,T_IO);
	CLRBIT(PORTC,T_IO);
    for(i=8; i>0; i--)
    {
        t>>=1;
		SETBIT(PORTC,T_CLK);
		nop();
		CLRBIT(PORTC,T_CLK);
		nop();
		if(CHKBIT(PINC,T_IO))t|=0x80;
	}
    SETBIT(DDRC,T_IO);
	return(t);
}


/********************************************************************
* 名称: write_1302
* 说明: 先写地址,后写命令/数据
* 功能: 往DS1302写入数据
* 调用: ds1302_write_a_byte()
* 输入: ucAddr: DS1302地址, ucDa: 要写的数据
* 返回值: 无
***********************************************************************/
void write_1302(unsigned char ucAddr, unsigned char ucDa)
{
    //DDRC=0xff;
    CLRBIT(PORTC,T_RST);     //T_RST=0
	//;;nop();
	CLRBIT(PORTC,T_CLK);	 //T_CLK=0;
	//;;nop();
	SETBIT(PORTC,T_RST);	 //T_RST=1

    ds1302_write_a_byte(ucAddr); /* 地址,命令 */
	CLRBIT(PORTC,T_CLK);
    ds1302_write_a_byte(ucDa); /* 写1Byte数据*/
	CLRBIT(PORTC,T_CLK);      //T_CLK=1
	//;;nop();
	CLRBIT(PORTC,T_RST);	  //T_RST=0
}

/********************************************************************
* 名称: read_1302
* 说明: 先写地址,后读命令/数据
* 功能: 读取DS1302某地址的数据
* 调用: ds1302_write_a_byte() , ds1302_read_a_byte()
* 输入: ucAddr: DS1302地址
* 返回值: ucDa :读取的数据
***********************************************************************/
unsigned char read_1302(unsigned char ucAddr)
{
    unsigned char ucDa;
	CLRBIT(PORTC,T_RST);
	//;;nop();
	CLRBIT(PORTC,T_CLK);
	//;;nop();
	SETBIT(PORTC,T_RST);


    ds1302_write_a_byte(ucAddr); /* 地址,命令 */
    ucDa = ds1302_read_a_byte(); /* 读1Byte数据 */
	CLRBIT(PORTC,T_CLK);
	//;;nop();
	CLRBIT(PORTC,T_RST);
	//;;nop();
    return(ucDa);
}

/********************************************************************
* 名称: v_BurstW1302T
* 说明: 先写地址,后写数据(时钟多字节方式)
* 功能: 往DS1302写入时钟数据(多字节方式)
* 调用: ds1302_write_a_byte()
* 输入: pSecDa: 时钟数据地址 格式为: 秒 分 时 日 月 星期 年 控制
* 8Byte (BCD码) 1B 1B 1B 1B 1B 1B 1B 1B
* 返回值: 无
***********************************************************************/
void v_BurstW1302T(unsigned char *pSecDa)
{
    unsigned char i;
    write_1302(0x8e,0x00); /* 控制命令,WP=0,写操作?*/
	CLRBIT(PORTC,T_RST);
	CLRBIT(PORTC,T_CLK);
	SETBIT(PORTC,T_RST);

    ds1302_write_a_byte(0xbe); /* 0xbe:时钟多字节写命令 */
    for (i=8;i>0;i--) /*8Byte = 7Byte 时钟数据 + 1Byte 控制*/
    {
        ds1302_write_a_byte(*pSecDa);/* 写1Byte数据*/
        pSecDa++;
    }
	SETBIT(PORTC,T_CLK);
	CLRBIT(PORTC,T_RST);
}

/********************************************************************
* 名称: v_BurstR1302T
* 说明: 先写地址,后读命令/数据(时钟多字节方式)
* 功能: 读取DS1302时钟数据
* 调用: ds1302_write_a_byte() , ds1302_read_a_byte()
* 输入: pSecDa: 时钟数据地址 格式为: 秒 分 时 日 月 星期 年
* 7Byte (BCD码) 1B 1B 1B 1B 1B 1B 1B
* 返回值: ucDa :读取的数据
***********************************************************************/
void v_BurstR1302T(unsigned char *pSecDa)
{
    unsigned char i;
	//DDRC=0xff;
	CLRBIT(PORTC,T_RST);
	CLRBIT(PORTC,T_CLK);
	SETBIT(PORTC,T_RST);
    ds1302_write_a_byte(0xbf); /* 0xbf:时钟多字节读命令 */
    for (i=8; i>0; i--)
    {
        *pSecDa = ds1302_read_a_byte(); /* 读1Byte数据 */
        pSecDa++;
    }
	//DDRC=0xff;
	SETBIT(PORTC,T_CLK);
	CLRBIT(PORTC,T_RST);
}

/********************************************************************
* 名称: v_BurstW1302R
* 说明: 先写地址,后写数据(寄存器多字节方式)
* 功能: 往DS1302寄存器数写入数据(多字节方式)
* 调用: ds1302_write_a_byte()
* 输入: pReDa: 寄存器数据地址
* 返回值: 无
***********************************************************************/
void v_BurstW1302R(unsigned char *pReDa)
{
    unsigned char i;
    write_1302(0x8e,0x00); /* 控制命令,WP=0,写操作?*/
	CLRBIT(PORTC,T_RST);
	CLRBIT(PORTC,T_CLK);
	SETBIT(PORTC,T_RST);
    ds1302_write_a_byte(0xfe); /* 0xfe:寄存器多字节写命令 */
    for (i=31;i>0;i--) /*31Byte 寄存器数据 */
    {
        ds1302_write_a_byte(*pReDa); /* 写1Byte数据*/
        pReDa++;
    }
	SETBIT(PORTC,T_CLK);
	CLRBIT(PORTC,T_RST);
}


/********************************************************************
* 名称: v_BurstR1302R
* 说明: 先写地址,后读命令/数据(寄存器多字节方式)
* 功能: 读取DS1302寄存器数据
* 调用: ds1302_write_a_byte() , ds1302_read_a_byte()
* 输入: pReDa: 寄存器数据地址
* 返回值: 无
***********************************************************************/
void v_BurstR1302R(unsigned char *pReDa)
{
    unsigned char i;
	//DDRC=0xff;
	CLRBIT(PORTC,T_RST);
	CLRBIT(PORTC,T_CLK);
	SETBIT(PORTC,T_RST);
    ds1302_write_a_byte(0xff); /* 0xff:寄存器多字节读命令 */
    for (i=31; i>0; i--) /*31Byte 寄存器数据 */
    {
        *pReDa = ds1302_read_a_byte(); /* 读1Byte数据 */
        pReDa++;
    }
	//DDRC=0xff;
	SETBIT(PORTC,T_CLK);
	CLRBIT(PORTC,T_RST);
}

/********************************************************************
* 名称: v_Set1302
* 说明:
* 功能: 设置初始时间
* 调用: write_1302()
* 输入: pSecDa: 初始时间地址。初始时间格式为: 秒 分 时 日 月 星期 年
* 7Byte (BCD码) 1B 1B 1B 1B 1B 1B 1B
* 返回值: 无
***********************************************************************/
void v_Set1302(unsigned char *pSecDa)
{
    unsigned char i;
    unsigned char ucAddr = 0x80;
    write_1302(0x8e,0x00); /* 控制命令,WP=0,写操作?*/
    for(i =7;i>0;i--)
    {
        write_1302(ucAddr,*pSecDa); /* 秒 分 时 日 月 星期 年 */

        pSecDa++;
        ucAddr +=2;
    }
    write_1302(0x8e,0x80); /* 控制命令,WP=1,写保护?*/
}

/********************************************************************
* 名称: v_Get1302
* 说明:
* 功能: 读取DS1302当前时间
* 调用: read_1302()
* 输入: ucCurtime: 保存当前时间地址。当前时间格式为: 秒 分 时 日 月 星期 年
* 7Byte (BCD码) 1B 1B 1B 1B 1B 1B 1B
* 返回值: 无
***********************************************************************/
void v_Get1302(unsigned char ucCurtime[])
{
    unsigned char i;
    unsigned char ucAddr = 0x81;
    for (i=0;i<7;i++)
    {
        ucCurtime[i] = read_1302(ucAddr);/*格式为: 秒 分 时 日 月
        星期 年 */
        ucAddr += 2;
    }
	CLRBIT(PORTC,T_CLK);
}

/*  enable power charge of 1302  */




void main()
{
 unsigned char buffer2[7]={0x45,0x59,0x15,0x13,0x3,0x2,0x01}; //初始数秒,分,时,日,
//月,星期,年
 unsigned char buffer3[7]={0x00,0x00,0x00,0x00,0x00,0x00,0x00};


 init_devices();
 initialize_1302();
 v_Set1302(buffer2);
 while(1)
 {
  v_Get1302(buffer3);
 }
}

⌨️ 快捷键说明

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