?? ds1302.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數(shù)據(jù)
* 調用:
* 輸入: ucDa 寫入的數(shù)據(jù)
* 返回值: 無
***********************************************************************/
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數(shù)據(jù)
* 調用:
* 輸入:
* 返回值: 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
* 說明: 先寫地址,后寫命令/數(shù)據(jù)
* 功能: 往DS1302寫入數(shù)據(jù)
* 調用: ds1302_write_a_byte()
* 輸入: ucAddr: DS1302地址, ucDa: 要寫的數(shù)據(jù)
* 返回值: 無
***********************************************************************/
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數(shù)據(jù)*/
CLRBIT(PORTC,T_CLK); //T_CLK=1
//;;nop();
CLRBIT(PORTC,T_RST); //T_RST=0
}
/********************************************************************
* 名稱: read_1302
* 說明: 先寫地址,后讀命令/數(shù)據(jù)
* 功能: 讀取DS1302某地址的數(shù)據(jù)
* 調用: ds1302_write_a_byte() , ds1302_read_a_byte()
* 輸入: ucAddr: DS1302地址
* 返回值: ucDa :讀取的數(shù)據(jù)
***********************************************************************/
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數(shù)據(jù) */
CLRBIT(PORTC,T_CLK);
//;;nop();
CLRBIT(PORTC,T_RST);
//;;nop();
return(ucDa);
}
/********************************************************************
* 名稱: v_BurstW1302T
* 說明: 先寫地址,后寫數(shù)據(jù)(時鐘多字節(jié)方式)
* 功能: 往DS1302寫入時鐘數(shù)據(jù)(多字節(jié)方式)
* 調用: ds1302_write_a_byte()
* 輸入: pSecDa: 時鐘數(shù)據(jù)地址 格式為: 秒 分 時 日 月 星期 年 控制
* 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:時鐘多字節(jié)寫命令 */
for (i=8;i>0;i--) /*8Byte = 7Byte 時鐘數(shù)據(jù) + 1Byte 控制*/
{
ds1302_write_a_byte(*pSecDa);/* 寫1Byte數(shù)據(jù)*/
pSecDa++;
}
SETBIT(PORTC,T_CLK);
CLRBIT(PORTC,T_RST);
}
/********************************************************************
* 名稱: v_BurstR1302T
* 說明: 先寫地址,后讀命令/數(shù)據(jù)(時鐘多字節(jié)方式)
* 功能: 讀取DS1302時鐘數(shù)據(jù)
* 調用: ds1302_write_a_byte() , ds1302_read_a_byte()
* 輸入: pSecDa: 時鐘數(shù)據(jù)地址 格式為: 秒 分 時 日 月 星期 年
* 7Byte (BCD碼) 1B 1B 1B 1B 1B 1B 1B
* 返回值: ucDa :讀取的數(shù)據(jù)
***********************************************************************/
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:時鐘多字節(jié)讀命令 */
for (i=8; i>0; i--)
{
*pSecDa = ds1302_read_a_byte(); /* 讀1Byte數(shù)據(jù) */
pSecDa++;
}
//DDRC=0xff;
SETBIT(PORTC,T_CLK);
CLRBIT(PORTC,T_RST);
}
/********************************************************************
* 名稱: v_BurstW1302R
* 說明: 先寫地址,后寫數(shù)據(jù)(寄存器多字節(jié)方式)
* 功能: 往DS1302寄存器數(shù)寫入數(shù)據(jù)(多字節(jié)方式)
* 調用: ds1302_write_a_byte()
* 輸入: pReDa: 寄存器數(shù)據(jù)地址
* 返回值: 無
***********************************************************************/
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:寄存器多字節(jié)寫命令 */
for (i=31;i>0;i--) /*31Byte 寄存器數(shù)據(jù) */
{
ds1302_write_a_byte(*pReDa); /* 寫1Byte數(shù)據(jù)*/
pReDa++;
}
SETBIT(PORTC,T_CLK);
CLRBIT(PORTC,T_RST);
}
/********************************************************************
* 名稱: v_BurstR1302R
* 說明: 先寫地址,后讀命令/數(shù)據(jù)(寄存器多字節(jié)方式)
* 功能: 讀取DS1302寄存器數(shù)據(jù)
* 調用: ds1302_write_a_byte() , ds1302_read_a_byte()
* 輸入: pReDa: 寄存器數(shù)據(jù)地址
* 返回值: 無
***********************************************************************/
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:寄存器多字節(jié)讀命令 */
for (i=31; i>0; i--) /*31Byte 寄存器數(shù)據(jù) */
{
*pReDa = ds1302_read_a_byte(); /* 讀1Byte數(shù)據(jù) */
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}; //初始數(shù)秒,分,時,日,
//月,星期,年
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 + -