the program have one sensor acs712 and moduleds1302
Dependencies: FATFileSystem mbed
DS1302.cpp
00001 #include "DS1302.h" 00002 00003 DS1302::DS1302(PinName SCLK, PinName IO, PinName CE) : _SCLK(SCLK), _IO(IO), _CE(CE) 00004 { 00005 _CE = 0; 00006 _SCLK = 0; 00007 _IO.input(); 00008 writeProtect = true; 00009 } 00010 00011 void DS1302::set_time(time_t t) 00012 { 00013 struct tm *_t = localtime(&t); 00014 writeReg(Seconds, (_t->tm_sec % 10) + ((_t->tm_sec / 10) << 4)); 00015 writeReg(Minutes, (_t->tm_min % 10) + ((_t->tm_min / 10) << 4)); 00016 writeReg(Hours, (_t->tm_hour % 10) + ((_t->tm_hour / 10) << 4)); 00017 writeReg(Dates, (_t->tm_mday % 10) + ((_t->tm_mday / 10) << 4)); 00018 writeReg(Months, ((_t->tm_mon + 1) % 10) + (((_t->tm_mon + 1) / 10) << 4)); 00019 writeReg(Days, _t->tm_wday + 1); 00020 writeReg(Years, ((_t->tm_year - 100) % 10) + (((_t->tm_year - 100) / 10) << 4)); 00021 } 00022 00023 time_t DS1302::time(time_t *t) 00024 { 00025 char regs[7]; 00026 _CE = 1; 00027 wait_us(4); 00028 writeByte(ClockBurst | 1); 00029 for (int i = 0; i<7; i++) 00030 regs[i] = readByte(); 00031 _CE = 0; 00032 00033 struct tm _t; 00034 _t.tm_sec = (regs[0] & 0xF) + (regs[0] >> 4) * 10; 00035 _t.tm_min = (regs[1] & 0xF) + (regs[1] >> 4) * 10; 00036 _t.tm_hour = (regs[2] & 0xF) + (regs[2] >> 4) * 10; 00037 _t.tm_mday = (regs[3] & 0xF) + (regs[3] >> 4) * 10; 00038 _t.tm_mon = (regs[4] & 0xF) + (regs[4] >> 4) * 10 - 1; 00039 _t.tm_year = (regs[6] & 0xF) + (regs[6] >> 4) * 10 + 100; 00040 00041 // convert to timestamp and display (1256729737) 00042 return mktime(&_t); 00043 } 00044 00045 void DS1302::storeByte(char address, char data) 00046 { 00047 if (address > 30) 00048 return; 00049 char command = RAMBase + (address << 1); 00050 writeReg(command, data); 00051 } 00052 00053 char DS1302::recallByte(char address) 00054 { 00055 if (address > 30) 00056 return 0; 00057 char command = RAMBase + (address << 1) + 1; 00058 return readReg(command); 00059 } 00060 00061 char DS1302::readReg(char reg) 00062 { 00063 char retval; 00064 00065 _CE = 1; 00066 wait_us(4); 00067 writeByte(reg); 00068 retval = readByte(); 00069 wait_us(4); 00070 _CE = 0; 00071 return retval; 00072 } 00073 00074 void DS1302::writeReg(char reg, char val) 00075 { 00076 if (writeProtect) { 00077 writeProtect = false; 00078 writeReg(WriteProtect, 0); 00079 } 00080 _CE = 1; 00081 wait_us(4); 00082 writeByte(reg); 00083 writeByte(val); 00084 wait_us(4); 00085 _CE = 0; 00086 } 00087 00088 00089 /*********************PRIVATE***********************/ 00090 void DS1302::writeByte(char data) 00091 { 00092 _IO.output(); 00093 for (int i = 0; i<8; i++) { 00094 _IO = data & 0x01; 00095 wait_us(1); 00096 _SCLK = 1; 00097 wait_us(1); 00098 _SCLK = 0; 00099 data >>= 1; 00100 } 00101 _IO.input(); 00102 } 00103 00104 char DS1302::readByte(void) 00105 { 00106 char retval = 0; 00107 00108 _IO.input(); 00109 for (int i = 0; i<8; i++) { 00110 retval |= _IO << i; 00111 wait_us(1); 00112 _SCLK = 1; 00113 wait_us(1); 00114 _SCLK = 0; 00115 } 00116 return retval; 00117 }
Generated on Wed Jul 13 2022 05:59:47 by 1.7.2