- /*
- 【Arduino】37种传感器模块系列实验(29)
- 实验二十九:DS1302实时时钟模块(带电池CR2032 掉电走时)之一
- */
- #define RST 5
- #define SCLK 7
- #define IO 6
- #define uchar unsigned char
- void setup() {
- pinMode(RST,OUTPUT);
- pinMode(SCLK,OUTPUT);
- pinMode(IO,OUTPUT);
- Serial.begin(9600);
- attachInterrupt(0, settm, FALLING );
- }
- void writeData(unsigned char addr,unsigned char dat){
- pinMode(IO,OUTPUT);
- digitalWrite(RST,LOW);
- digitalWrite(SCLK,LOW) ;
- delayMicroseconds(1);
- digitalWrite(RST,HIGH);
- int i;
- for(i =0;i<8;i++){
- digitalWrite(SCLK,LOW);
- digitalWrite(IO, addr&1);
- addr =addr>>1;
- digitalWrite(SCLK,HIGH);
- delayMicroseconds(1);
- }
- for(i =0;i<8;i++){
- digitalWrite(SCLK,LOW);
- digitalWrite(IO, dat&1);
- dat =dat>>1;
- digitalWrite(SCLK,HIGH);
- delayMicroseconds(1);
- }
- digitalWrite(RST,LOW);
- }
- uchar readData(uchar addr){
- addr = addr | 1;
- pinMode(IO,OUTPUT);
- digitalWrite(RST,LOW);
- digitalWrite(SCLK,LOW) ;
- delayMicroseconds(1);
- digitalWrite(RST,HIGH);
- int i;
- for(i =0;i<8;i++){
- digitalWrite(SCLK,LOW);
- digitalWrite(IO, addr&1);
- addr =addr>>1;
- digitalWrite(SCLK,HIGH);
- delayMicroseconds(1);
- }
- pinMode(IO,INPUT);
- int dat = 0;
- for(i =0;i<8;i++){
- digitalWrite(SCLK,LOW);
- delayMicroseconds(10);
- uchar b = digitalRead(IO);
- dat = dat >>1;
- dat = dat | (b<<7);
- digitalWrite(SCLK,HIGH);
- delayMicroseconds(1);
- }
- digitalWrite(RST,LOW);
- return dat ;
- }
- #define SECOND 0x80
- #define MIN 0x82
- #define HOUR 0x84
- #define DAY 0x86
- #define MONTH 0x88
- #define YEAR 0x8C
- #define WEEK 0x8A
- void writeTimeUnit(uchar TYPE,uchar data){
- uchar high = data / 10;
- uchar low = data % 10;
- uchar d = ((data /10) << 4) | (data % 10);
- writeData(TYPE,data);
- }
- uchar readTimeUnit(uchar TYPE){
- uchar r = readData(TYPE);
- r = 10*(r >> 4)+ (r & 0xf);
- return r;
- }
- void writeAllTm(uchar tm[]){
- uchar s = SECOND;
- for(uchar i=0;i<5;i++){
- writeTimeUnit(s,tm[5-i]);
- s+=2;
- }
- writeTimeUnit(YEAR,tm[0]);
- }
- void readAllTm(uchar tm[]){
- uchar s = SECOND;
- for(uchar i=0;i<5;i++){
- tm[5-i] = readTimeUnit(s);
- s+=2;
- }
- tm[0] = readTimeUnit(YEAR);
- tm[6] = readTimeUnit(WEEK);
- }
- void printTm(uchar tm[]){
- Serial.print(tm[0]);
- Serial.print("-");
- Serial.print(tm[1]);
- Serial.print("-");
- Serial.print(tm[2]);
- Serial.print(" ");
- Serial.print(tm[3]);
- Serial.print(":");
- Serial.print(tm[4]);
- Serial.print(":");
- Serial.print(tm[5]);
- Serial.print(" week ");
- Serial.print(tm[6]);
- Serial.println();
- }
- uchar st = 0;
- void loop() {
- uchar tm[7] = {0};
- readAllTm(tm);
- printTm(tm);
- delay(1000);
- }
- void settm()
- {
- uchar tm[] = {11,12,13,4,5,6 ,0};
- Serial.println("set tm ...");
- writeAllTm(tm);
- }
复制代码
|