?? ds1302.h
字號:
#include "iom16v.h"
//復位腳
#define RST_CLR PORTC &= ~(1 << PC6)
#define RST_SET PORTC |= (1 << PC6)
#define RST_IN DDRC &= ~(1 << PC6)
#define RST_OUT DDRC |= (1 << PC6)
//雙向數(shù)據(jù)
#define IO_CLR PORTC &= ~(1 << PC1)
#define IO_SET PORTC |= (1 << PC1)
#define IO_R PINC & (1 << PC1)
#define IO_IN DDRC &= ~(1 << PC1)
#define IO_OUT DDRC |= (1 << PC1)
//時鐘信號
#define SCK_CLR PORTC &= ~(1 << PC0)
#define SCK_SET PORTC |= (1 << PC0)
#define SCK_IN DDRC &= ~(1 << PC0)
#define SCK_OUT DDRC |= (1 << PC0)
#define ds1302_sec_add 0x80
#define ds1302_min_add 0x82
#define ds1302_hr_add 0x84
#define ds1302_date_add 0x86
#define ds1302_month_add 0x88
#define ds1302_day_add 0x8a
#define ds1302_year_add 0x8c
#define ds1302_control_add 0x8e
#define ds1302_charger_add 0x90
#define ds1302_clkburst_add 0xbe
//向DS1302寫入一字節(jié)數(shù)據(jù)
void ds1302_write_byte(unsigned char addr, unsigned char d) {
unsigned char i;
RST_SET; //啟動DS1302總線 RES為1時才可進行讀寫操作
//寫入目標地址:addr
IO_OUT;
addr = addr & 0xFE;//最低位置零 寫操作
for (i = 0; i < 8; i ++) {
if (addr & 0x01) {
IO_SET;
}
else {
IO_CLR;
}
SCK_SET; //上升沿將數(shù)據(jù)寫入
SCK_CLR;
addr = addr >> 1;
}
//寫入數(shù)據(jù)
IO_OUT;
for (i = 0; i < 8; i ++) {
if (d & 0x01) {
IO_SET;
}
else {
IO_CLR;
}
SCK_SET; //上升沿將數(shù)據(jù)寫入
SCK_CLR;
d = d >> 1;
}
RST_CLR; //禁止DS1302總線
}
//讀出一個字節(jié)
unsigned char ds1302_read_byte(unsigned char addr) {
unsigned char i;
unsigned char temp;
RST_SET; //啟動DS1302總線
//寫入目標地址:addr
IO_OUT;
addr = addr | 0x01; //最低位置高 讀操作
for (i = 0; i < 8; i ++) {
if (addr & 0x01) {
IO_SET;
}
else {
IO_CLR;
}
SCK_SET;
SCK_CLR;
addr = addr >> 1;
}
//輸出數(shù)據(jù)
IO_IN;
for (i = 0; i < 8; i ++) { //由低位向高位讀數(shù)據(jù)
temp = temp >> 1;
if (IO_R) { //判斷是否為1
temp |= 0x80;
}
else {
temp &= 0x7F;
}
SCK_SET;
SCK_CLR;
}
RST_CLR; //禁止DS1302總線
return temp;
}
//寫入時鐘數(shù)據(jù)
void ds1302_write_time(unsigned char *time_data)
{
ds1302_write_byte(ds1302_control_add,0x00); //關閉寫保護 此時可以寫入數(shù)據(jù)
ds1302_write_byte(ds1302_sec_add,0x80); //暫停 時鐘震蕩停止
time_data++; //指針指向年的后兩位
//送入數(shù)據(jù)
ds1302_write_byte(ds1302_year_add,*time_data++); //年份只寫入后面兩位
ds1302_write_byte(ds1302_month_add,*time_data++); //月
ds1302_write_byte(ds1302_date_add,*time_data++); //日
ds1302_write_byte(ds1302_hr_add,*time_data++); //時
ds1302_write_byte(ds1302_min_add,*time_data++); //分
ds1302_write_byte(ds1302_sec_add,*time_data++); //秒
ds1302_write_byte(ds1302_day_add,*time_data); //周
ds1302_write_byte(ds1302_control_add,0x80); //打開寫保護 拒絕寫入數(shù)據(jù)
}
//讀出時鐘數(shù)據(jù)
void ds1302_read_time(unsigned char *time_data) {
time_data++;
*time_data=ds1302_read_byte(ds1302_year_add); //只讀出后面兩位08
time_data++;
*time_data=ds1302_read_byte(ds1302_month_add); //月
time_data++;
*time_data=ds1302_read_byte(ds1302_date_add); //日
time_data++;
*time_data=ds1302_read_byte(ds1302_hr_add); //時
time_data++;
*time_data=ds1302_read_byte(ds1302_min_add); //分
time_data++;
*time_data=(ds1302_read_byte(ds1302_sec_add))&0x7F; //秒
time_data++;
*time_data=ds1302_read_byte(ds1302_day_add); //周
}
//初始化
void ds1302_init(void) {
RST_CLR; //RST為低電平
SCK_CLR; //時鐘信號為低電平
RST_OUT; //RST 為輸出
SCK_OUT; // SCK為輸出
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -