void RectifierParser::buildControlOnOffMessage(uint8_t address, bool isOn) {
  char infoPart[2];
  uint8_t pos = 0;
  infoPart[pos++] = decToBcd(isOn ? 0x0U : 0x01U);
  infoPart[pos++] = decToBcd(0x00U);
	buildSimpleIssuedMessage(RectifierOrder::ISSUED_CONTROL_ON_OFF, address, infoPart, pos);
}
bool Nanoshield_RTC::writeYear(int year)
{
	int mon;

  // Address register containing the months and century
  Wire.beginTransmission(i2cAddr);
  Wire.write(monthAddr);
  if (Wire.endTransmission()) return false;

	// Read month register
  if (Wire.requestFrom((int)i2cAddr, 1) != 1) return false;
  mon = bcdToDec(Wire.read() & 0x1F);

	// Rewrite month along with century bit
  Wire.beginTransmission(i2cAddr);
  Wire.write(monthAddr);              // Start address
	if (year / 100 == 19) {             // Set century bit to zero if 20th century
		Wire.write(decToBcd(mon) & 0x7F); // Month (1-12, century bit (bit 7) = 0)
	} else {
		Wire.write(decToBcd(mon) | 0x80); // Month (1-12, century bit (bit 7) = 1)
	}
  if (Wire.endTransmission() != 0) return false;

	// Write year
  Wire.beginTransmission(i2cAddr);
  Wire.write(yearAddr);              // Start address
  Wire.write(decToBcd(year % 100));  // Year (00-99)
  return Wire.endTransmission() == 0;
}
void getTwoByteBcdFromDouble(char* data, uint8_t& pos, double value, int multiply) {
  value *= multiply;
  int seconsPart = (int)value % 100;
  int firstPart = ((int)value - seconsPart) / 100;
  *(data + pos) = decToBcd(firstPart);
  pos++;
  *(data + pos) = decToBcd(seconsPart);
  pos++;
}
Exemple #4
0
void Rtc_Pcf8563::setTime(byte hour, byte minute, byte sec)
{
  Wire.beginTransmission(Rtcc_Addr);	// Issue I2C start signal
  Wire.send(RTCC_SEC_ADDR);   	// send addr low byte, req'd

  Wire.send(decToBcd(sec)); 		//set seconds
  Wire.send(decToBcd(minute));	//set minutes
  Wire.send(decToBcd(hour));		//set hour
	Wire.endTransmission();
   
}
void RectifierParser::buildSimpleIssuedMessage(char cid, uint8_t address, char* info, uint8_t infoLength) {
  sendMessageLength_ = 0;
  sendMessage_[sendMessageLength_++] = (char)START_FRAME_BYTE;
  sendMessage_[sendMessageLength_++] = decToBcd(address);
  sendMessage_[sendMessageLength_++] = decToBcd(infoLength + 1);
  sendMessage_[sendMessageLength_++] = cid;
  memcpy(sendMessage_ + sendMessageLength_, info, infoLength);
  sendMessageLength_ += infoLength;
	uint8_t chksum = getCheckSum(sendMessage_ + 1, sendMessageLength_ - 1);
  sendMessage_[sendMessageLength_++] = decToBcd(chksum);
  sendMessage_[sendMessageLength_++] = (char)END_FRAME_BYTE;
}
// Set the date/time, set to 24hr and enable the clock:
// (assumes you're passing in valid numbers)
void MCP7941x::setDateTime(
  byte second,        // 0-59
  byte minute,        // 0-59
  byte hour,          // 1-23
  byte dayOfWeek,     // 1-7
  byte dayOfMonth,    // 1-28/29/30/31
  byte month,         // 1-12
  byte year)          // 0-99
{
  Wire.beginTransmission(MCP7941x_RTC_I2C_ADDR);
  WireSend(RTC_LOCATION);
  
  WireSend(decToBcd(second) & 0x7f);              // set seconds and disable clock (01111111)
  WireSend(decToBcd(minute) & 0x7f);              // set minutes (01111111)
  WireSend(decToBcd(hour) & 0x3f);                // set hours and to 24hr clock (00111111)
  WireSend(0x08 | (decToBcd(dayOfWeek) & 0x07));  // set the day and enable battery backup (00000111)|(00001000)
  WireSend(decToBcd(dayOfMonth) & 0x3f);          // set the date in month (00111111)
  WireSend(decToBcd(month) & 0x1f);               // set the month (00011111)
  WireSend(decToBcd(year));                       // set the year (11111111)
  
  Wire.endTransmission();

  // Start Clock:
  Wire.beginTransmission(MCP7941x_RTC_I2C_ADDR);
  WireSend(RTC_LOCATION);
  WireSend(decToBcd(second) | 0x80);     // set seconds and enable clock (10000000)
  Wire.endTransmission();
}
Exemple #7
0
void DS1307::set_time(time_t seconds) {
	LOG << "set_time - 2 - Entered";
	tm *ltm = localtime(&seconds);
	// convert now to tm struct for UTC
	tm *gmtm = gmtime(&seconds);
	LOG << "GMTIME set device to this. " << asctime(gmtm);

	LOG << "seconds " << gmtm->tm_sec;   // seconds of minutes from 0 to 61
	LOG << "Minutes " << gmtm->tm_min;   // minutes of hour from 0 to 59
	LOG << "Hours " << gmtm->tm_hour;  // hours of day from 0 to 24
	LOG << "DayOfWeek " << gmtm->tm_wday;  // days since sunday
	LOG << "DayOfMonth " << gmtm->tm_mday;  // day of month from 1 to 31
	LOG << "Month " << gmtm->tm_mon;   // month of year from 0 to 11
	LOG << "Year " << gmtm->tm_year;  // year since 1900

	char buffer[10];
	buffer[0] = 0x00; // Command
	buffer[1] = decToBcd(gmtm->tm_sec & 0x7f); // Seconds
	buffer[2] = decToBcd(gmtm->tm_min); // Minutes
	buffer[3] = decToBcd(gmtm->tm_hour); // Hours
	buffer[4] = decToBcd(gmtm->tm_wday); // DayOfWeek
	buffer[5] = decToBcd(gmtm->tm_mday); // DayOfMonth
	buffer[6] = decToBcd(gmtm->tm_mon); // Month
	LOG << "Y1 = " << gmtm->tm_year - 100;
	LOG << "Y2 = " << decToBcd(gmtm->tm_year - 100);
	buffer[7] = decToBcd(gmtm->tm_year - 100); // Year
	i2c_bus->write_bytes(buffer, 8);


}
Exemple #8
0
void Time_Adj_1224hrs(BOOL h12)
{
	BYTE hours, bcd10, bcd;

	Time_Pause(TRUE);
	Rtc_ReadFromRtc();

	if(datetime._02h.bits_24hrs._1224hr != h12)
	{
		if(h12)
		{
			// Convert from 24h to 12h
			bcdToDec(datetime._02h.bits_24hrs.hours10, datetime._02h.bits_24hrs.hours, &hours);
			if(hours >= 12)
			{
				datetime._02h.bits_ampm.ampm = 1;
				if(hours > 12) hours -= 12;
			}
			else
			{
				datetime._02h.bits_ampm.ampm = 0;
				if(hours == 0) hours = 12;
			}
			decToBcd(&bcd10 , &bcd, hours);
			datetime._02h.bits_ampm.hours10 = bcd10;
			datetime._02h.bits_ampm.hours = bcd;
		}
		else
		{
			// Convert from 12h to 24h
			bcdToDec(datetime._02h.bits_ampm.hours10, datetime._02h.bits_ampm.hours, &hours);
			if(datetime._02h.bits_ampm.ampm == 1 && hours != 12)
			{
				hours += 12;
			}
			else if(datetime._02h.bits_ampm.ampm == 0 && hours == 12)
			{
				hours = 0;
			}
			decToBcd(&bcd10 , &bcd, hours);
			datetime._02h.bits_24hrs.hours10 = bcd10;
			datetime._02h.bits_24hrs.hours = bcd;
		}
	}

	datetime._02h.bits_24hrs._1224hr = h12;

	Rtc_WriteToRtc();
	Time_Pause(FALSE);
}
Exemple #9
0
bool RTCx::setClock(const struct tm *tm, timeFunc_t func) const
{
  // Find which register to read from
  uint8_t sz = 0;
  uint8_t reg = getRegister(func, sz);
  
  if (sz == 0)
    return false; // not supported

  if (func == TIME)
    stopClock();

  uint8_t osconEtc = 0;
  if (device == MCP7941x)
    // Preserve OSCON, VBAT, VBATEN on MCP7941x
    osconEtc = readData((uint8_t)0x03) & 0x38;
  
  // Write everything *except* the second
  Wire.beginTransmission(address);
  Wire.write(reg + 1);
  Wire.write(decToBcd(tm->tm_min));
  Wire.write(decToBcd(tm->tm_hour)); // Forces 24h mode
  Wire.write(decToBcd(tm->tm_wday + 1) | osconEtc);
  Wire.write(decToBcd(tm->tm_mday));
  Wire.write(decToBcd(tm->tm_mon + 1)); // leap year read-only on MCP7941x
  Wire.write(decToBcd(tm->tm_year % 100));
  Wire.endTransmission();

  if (func == TIME)
    startClock(decToBcd(tm->tm_sec));
  else
    writeData(reg, decToBcd(tm->tm_sec));
  return true;
}
Exemple #10
0
void Time_Adj_S(BOOL direction)
{
	BYTE seconds, bcd10, bcd;
	
	Time_Pause(TRUE);
	Rtc_ReadFromRtc();

	bcdToDec(datetime._00h.bits.seconds10, datetime._00h.bits.seconds, &seconds);

	if(direction == TIME_UP)
	{
		if(seconds < 59) seconds++;
		else seconds = 0;
	}
	else
	{
		if(seconds > 0) seconds--;
		else seconds = 59;
	}

	decToBcd(&bcd10, &bcd, seconds);

	datetime._00h.bits.seconds = bcd;
	datetime._00h.bits.seconds10 = bcd10;

	Rtc_WriteToRtc();
	Time_Pause(FALSE);
}
void RealTimeClockDS1307::setYear(int y)
{
  if (y >= 0 && y <100)
  {
    _reg6_year = decToBcd(y);
  }
}
Exemple #12
0
void Time_Adj_Mi(BOOL direction)
{
	BYTE minutes, bcd10, bcd;

	Time_Pause(TRUE);
	Rtc_ReadFromRtc();

	bcdToDec(datetime._01h.bits.minutes10, datetime._01h.bits.minutes, &minutes);

	if(direction == TIME_UP)
	{
		if(minutes < 59) minutes++;
		else minutes = 0;
	}
	else
	{
		if(minutes > 0) minutes--;
		else minutes = 59;
	}

	decToBcd(&bcd10, &bcd, minutes);

	datetime._01h.bits.minutes = bcd;
	datetime._01h.bits.minutes10 = bcd10;

	Rtc_WriteToRtc();
	Time_Pause(FALSE);
}
bool Nanoshield_RTC::writeDay(int day)
{
  Wire.beginTransmission(i2cAddr);
  Wire.write(dayAddr);         // Start address
  Wire.write(decToBcd(day));   // Day (1-31)
  return Wire.endTransmission() == 0;
}
void RealTimeClockDS1307::setDayOfWeek(int d)
{
  if (d > 0 && d < 8)
  {
    _reg3_day = decToBcd(d);
  }
}
void RealTimeClockDS1307::setMinutes(int m)
{
  if (m < 60 && m >=0)
  {
    _reg1_min = decToBcd(m);
  }
}
bool Nanoshield_RTC::writeHours(int hour)
{
  Wire.beginTransmission(i2cAddr);
  Wire.write(hoursAddr);       // Start address
  Wire.write(decToBcd(hour));  // Hour (0-23)
  return Wire.endTransmission() == 0;
}
bool Nanoshield_RTC::writeSeconds(int sec)
{
  Wire.beginTransmission(i2cAddr);
  Wire.write(secondsAddr);     // Start address
  Wire.write(decToBcd(sec));   // Second (0-59)
  return Wire.endTransmission() == 0;
}
bool Nanoshield_RTC::writeMinutes(int min)
{
  Wire.beginTransmission(i2cAddr);
  Wire.write(minutesAddr);     // Start address
  Wire.write(decToBcd(min));   // Minute (0-59)
  return Wire.endTransmission() == 0;
}
void RealTimeClockDS1307::setMonth(int m)
{
  if (m > 0 && m < 13)
  {
    _reg5_month = decToBcd(m);
  }
}
bool Nanoshield_RTC::writeWeekday(int wday)
{
  Wire.beginTransmission(i2cAddr);
  Wire.write(weekdayAddr);     // Start address
  Wire.write(decToBcd(wday));  // Weekday (0-6 = Sunday-Saturday)
  return Wire.endTransmission() == 0;
}
void RealTimeClockDS1307::setDate(int d)
{
  if (d > 0 && d < 32)
  {
    _reg4_date = decToBcd(d);
  }
}
void RealTimeClockDS1307::setHours(int h)
{
  if (is12hour())
  {
    if (h >= 1 && h <=12)
    {
      //preserve 12/24 and AM/PM bits
      _reg2_hour = decToBcd(h) | (_reg2_hour & 0x60);
    }
  } else {
    if (h >= 0 && h <=24)
   {
      //preserve 12/24 bit
      _reg2_hour = decToBcd(h) | (_reg2_hour & 0x40);
   }
  }//else
}//setHours
void RealTimeClockDS1307::setSeconds(int s)
{
  if (s < 60 && s >=0)
  {
    //need to preserve oscillator bit
    _reg0_sec = decToBcd(s) | (_reg0_sec & 0x80);
  }
}
Exemple #24
0
/* set the alarm values
 * whenever the clock matches these values an int will
 * be sent out pin 3 of the Pcf8563 chip
 */
void Rtc_Pcf8563::setAlarm(byte min, byte hour, byte day, byte weekday)
{
    if (min <99) {
        min = constrain(min, 0, 59);
        min = decToBcd(min);
        min &= ~RTCC_ALARM;
    } else {
        min = 0x0; min |= RTCC_ALARM;
    }

    if (hour <99) {
        hour = constrain(hour, 0, 23);
        hour = decToBcd(hour);
        hour &= ~RTCC_ALARM;
    } else {
        hour = 0x0; hour |= RTCC_ALARM;
    }

    if (day <99) {
        day = constrain(day, 1, 31);
        day = decToBcd(day); day &= ~RTCC_ALARM;
    } else {
        day = 0x0; day |= RTCC_ALARM;
    }

    if (weekday <99) {
        weekday = constrain(weekday, 0, 6);
        weekday = decToBcd(weekday);
        weekday &= ~RTCC_ALARM;
    } else {
        weekday = 0x0; weekday |= RTCC_ALARM;
    }

    Rtc_Pcf8563::enableAlarm();

    //TODO bounds check  the inputs first
    Wire.beginTransmission(Rtcc_Addr);    // Issue I2C start signal
    Wire.send(RTCC_ALRM_MIN_ADDR);
    Wire.send(min);                //minute alarm value reset to 00
    Wire.send(hour);                //hour alarm value reset to 00
    Wire.send(day);                //day alarm value reset to 00
    Wire.send(weekday);            //weekday alarm value reset to 00
    Wire.endTransmission();
}
void rtc_ds1307_set_time(struct __time t)
{
    uint8 dta[8] = {0x00, t.second, t.minute, t.hour, t.week, t.day, t.month, t.year};
    
    for(int i=1; i<8; i++)
    {
        dta[i] = decToBcd(dta[i]);
    }
    
    suli_i2c_write(__I2C_Device_RTC, DS1307_I2C_ADDRESS, dta, 8);
}
Exemple #26
0
	void I2CFlexel::setTimeAndDate(const DateTime &timeAndDate)
	{
		setTimeAndDate(
			decToBcd(timeAndDate.seconds), decToBcd(timeAndDate.minutes), decToBcd(timeAndDate.hours), decToBcd(timeAndDate.weekDay),
			decToBcd(timeAndDate.day), decToBcd(timeAndDate.month), decToBcd(timeAndDate.year)
		);
	}
Exemple #27
0
void Rtc_Pcf8563::setDate(byte day, byte weekday, byte mon, byte century, byte year)
{
    /* year val is 00 to 99, xx
        with the highest bit of month = century
        0=20xx
        1=19xx
        */
    month = decToBcd(mon);
    if (century == 1){
        month |= RTCC_CENTURY_MASK;
    }
    else {
        month &= ~RTCC_CENTURY_MASK;
    }
    Wire.beginTransmission(Rtcc_Addr);    // Issue I2C start signal
    Wire.send(RTCC_DAY_ADDR);
    Wire.send(decToBcd(day));            //set day
    Wire.send(decToBcd(weekday));    //set weekday
    Wire.send(month);                         //set month, century to 1
    Wire.send(decToBcd(year));        //set year to 99
    Wire.endTransmission();
}
bool Nanoshield_RTC::writeMonth(int mon)
{
	int century;

  // Address register containing the months and century
  Wire.beginTransmission(i2cAddr);
  Wire.write(monthAddr);
  if (Wire.endTransmission()) return false;

	// Read month register
  if (Wire.requestFrom((int)i2cAddr, 1) != 1) return false;
  century = Wire.read() & 0x80;

  Wire.beginTransmission(i2cAddr);
  Wire.write(monthAddr);              // Start address
	if (century) {
		Wire.write(decToBcd(mon) | 0x80); // Month (1-12, century bit (bit 7) = 1)
	} else {
		Wire.write(decToBcd(mon) & 0x7F); // Month (1-12, century bit (bit 7) = 0)
	}
  return Wire.endTransmission() == 0;
}
Exemple #29
0
void Time_Adj_Y(BOOL direction)
{
	BYTE date, month, year, bcd10, bcd;

	Time_Pause(TRUE);
	Rtc_ReadFromRtc();

	bcdToDec(datetime._04h.bits.date10, datetime._04h.bits.date, &date);
	bcdToDec(datetime._05h.bits.month10, datetime._05h.bits.month, &month);
	bcdToDec(datetime._06h.bits.year10, datetime._06h.bits.year, &year);

	bcd10 = datetime._06h.bits.year10;
	bcd = datetime._06h.bits.year;

	if(direction == TIME_UP)
	{
		year++;
		if(isValidDate(date, month, year))
		{
			decToBcd(&bcd10, &bcd, year);
		}
	}
	else
	{
		year--;
		if(isValidDate(date, month, year))
		{
			decToBcd(&bcd10, &bcd, year);
		}
	}

	datetime._06h.bits.year10 = bcd10;
	datetime._06h.bits.year = bcd;

	Rtc_WriteToRtc();
	Time_Pause(FALSE);
}
Exemple #30
0
/*Frunction: Write the time that includes the date to the RTC chip */
void DS1307::setTime()
{
	Wire1.beginTransmission(DS1307_I2C_ADDRESS);
	Wire1.write((uint8_t)0x00);
	Wire1.write(decToBcd(second));// 0 to bit 7 starts the clock
	Wire1.write(decToBcd(minute));
	Wire1.write(decToBcd(hour));  // If you want 12 hour am/pm you need to set bit 6 
	Wire1.write(decToBcd(dayOfWeek));
	Wire1.write(decToBcd(dayOfMonth));
	Wire1.write(decToBcd(month));
	Wire1.write(decToBcd(year));
	Wire1.endTransmission();
}