📄 3530complete.c
字号:
#include "C8051F040.h"
#include <intrins.h>
#include<math.h>
#include<absacc.h>
#define RESET 0x60 //1 ack
#define STATUS 0x62 //2 ack
#define DATEHOUR 0x64 //8 ack year month day week hour minute second
#define HOUR 0x66 //4 ack hour minute second
#define INTR1 0x68 //3 ack
#define INTR2 0x6A
#define uchar unsigned char
#define uint unsigned int
extern void delayms(uint ms) ;
sbit INT1=P1^4;
sbit INT2=P1^5;
sbit SDA=P1^6;
sbit SCL=P1^7;
uchar year,month,day,week,minute,hour,second;
/*==========================================================================================
* 函数名:start
*
* 功能描述:3530的开始状态
*
============================================================================================*/
void start(void)
{SDA = 1;
SCL = 1;
_nop_();
_nop_();
SDA = 0;
_nop_();
_nop_();
_nop_();
_nop_();
SCL = 0;
}
/*============================================================================================
* 函数名:stop
*
* 功能描述:3530的停止状态
*
============================================================================================*/
void stop(void)
{SDA = 0;
_nop_();
_nop_();
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
SDA = 1;
}
/*===========================================================================================
* 函数名:shiftin
*
* 返回值:返回取出的值
*
* 功能描述:从3530取出数据
*
============================================================================================*/
uchar shiftin()
{
uchar i,read_data;
for(i = 0; i < 8; i++)
{SCL = 1;
_cror_(read_data,1);
read_data |= (uchar)SDA;
SCL = 0;
}
return(read_data);
}
/*=========================================================================================
* 函数名:shiftout,shiftout1.分别是从低为和高为写入数据,SHIFTOUT2为循环16位,用于INT
*
* 参数:write_data----需要存储的数值
*
* 功能描述:存储数据到3530
==========================================================================================*/
shiftout(uchar write_data)
{uchar i,change;
bit ack_bit;
change=0x01;
for(i = 0; i < 8; i++) // 循环移入8个位
{SDA = (bit)(write_data & change); //80
_nop_();
SCL = 1;
_nop_();
_nop_();
SCL = 0;
change <<= 1;
}
SDA = 1; // 读取应答
_nop_();
_nop_();
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ack_bit = SDA;
SCL = 0;
}
shiftout1(uchar write_data)
{uchar i;
bit ack_bit;
for(i = 0; i < 8; i++) // 循环移入8个位
{SDA = (bit)(write_data & 0x80); //80
_nop_();
SCL = 1;
_nop_();
_nop_();
SCL = 0;
write_data <<= 1;
}
SDA = 1; // 读取应答
_nop_();
_nop_();
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ack_bit = SDA;
SCL = 0;
}
shiftout2(uchar write_data)
{uchar i,change;
bit ack_bit;
change=0x0001;
for(i = 0; i < 16; i++) // 循环移入16个位
{SDA = (bit)(write_data & change); //80
_nop_();
SCL = 1;
_nop_();
_nop_();
SCL = 0;
write_data <<= 1;
}
SDA = 1; // 读取应答
_nop_();
_nop_();
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ack_bit = SDA;
SCL = 0;
}
/*=========================================================================================
* 函数名:delayms
*
* 参数:ms--延迟的毫秒数
*
* 功能描述:起延时的作用
===========================================================================================*/
void delayms(uint ms)
{uint i;
while(ms--)
for(i = 0; i < 120; i++);
}
/*==========================================================================================
主程序
===========================================================================================*/
void main(void)
{
uchar year,month,day,week,minute,hour,second;
//关看门狗
WDTCN = 0xde;
WDTCN = 0xad;
//复位命令==================================================================================
start();
shiftout1(RESET);
delayms(10);
stop();
delayms(10);
//状态寄存器初始化==========================================================================
start();
shiftout1(STATUS);
delayms(10);
shiftout(0xD2);
delayms(10);
stop();
delayms(10);
//INTR1初始化===============================================================================
start();
shiftout1(INTR1);
delayms(10);
shiftout2(0x1000);
delayms(10);
stop();
delayms(10);
//INTR2初始化===============================================================================
start();
shiftout1(INTR2);
delayms(10);
shiftout2(0x2080);
delayms(10);
stop();
delayms(10);
//写入数据==================================================================================
start();
shiftout1(DATEHOUR);
delayms(10);
//year
shiftout(0x07);
delayms(10);
//month
shiftout(0x07);
delayms(10);
//day
shiftout(0x20);
delayms(10);
//week
shiftout(0x05);
delayms(10);
//hour
shiftout(0x12);
delayms(10);
//minute
shiftout(0x59);
delayms(10);
//second
shiftout(0x59);
stop();
delayms(10);
//读出数据===================================================================================
start();
shiftout1(DATEHOUR | 0x01);
delayms(10);
//year
year=shiftin();
delayms(10);
//month
month=shiftin();
delayms(10);
//day
day=shiftin();
delayms(10);
//week
week=shiftin();
delayms(10);
//hour
hour=shiftin();
delayms(10);
//minute
minute=shiftin();
delayms(10);
//second
second=shiftin();
stop();
delayms(10);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -