示例#1
0
        /// <summary>
        /// Sets the Date and Time on the DS1307.
        /// </summary>
        /// <param name="value">The date and time as a System.DateTime object
        /// that will be saved on the DS1307.</param>
        public async Task SetDateAsync(DateTime value)
        {
            byte[] writeBuffer = new byte[]
            {
                //RTC_ADDRESS,
                Bcd.FromInt(value.Second),
                Bcd.FromInt(value.Minute),
                Bcd.FromInt(value.Hour),
                Bcd.FromInt((int)value.DayOfWeek),
                Bcd.FromInt(value.Day),
                Bcd.FromInt(value.Month),
                Bcd.FromInt(value.Year >= 2000 ? value.Year - 2000: 0),
            };

            await this.i2c.WriteAsync(RTC_ADDRESS, writeBuffer);
        }
示例#2
0
        /*
         * /// <summary>
         * /// Get date and time from RTC
         * /// </summary>
         * /// <returns>Date and time read</returns>
         * public DateTime GetDateTime()
         * {
         *  DateTime dateTime;
         *
         *  // read seconds converting from BCD
         *  this.ramAddress[0] = SECONDS_ADDR;
         *  this.ReadRam(this.ramAddress, this.ramData);
         *  // avoid clock halt bit
         *  this.ramData[0] = (byte)(this.ramData[0] & ~CLOCK_HALT);
         *  int second = (this.ramData[0] >> 4) * 10 + (this.ramData[0] & 0x0F);
         *
         *  // read minutes converting from BCD
         *  this.ramAddress[0] = MINUTES_ADDR;
         *  this.ReadRam(this.ramAddress, this.ramData);
         *  int minute = (this.ramData[0] >> 4) * 10 + (this.ramData[0] & 0x0F);
         *
         *  // read hours converting from BCD
         *  this.ramAddress[0] = HOURS_ADDR;
         *  this.ReadRam(this.ramAddress, this.ramData);
         *  // avoid 12/24 hour bit (forced 24 hour)
         *  this.ramData[0] = (byte)(this.ramData[0] & ~HOUR_12_24);
         *  int hour = (this.ramData[0] >> 4) * 10 + (this.ramData[0] & 0x0F);
         *
         *  // read day converting from BCD
         *  this.ramAddress[0] = DAY_ADDR;
         *  this.ReadRam(this.ramAddress, this.ramData);
         *  DayOfWeek day = (DayOfWeek)this.ramData[0];
         *
         *  // read date converting from BCD
         *  this.ramAddress[0] = DATE_ADDR;
         *  this.ReadRam(this.ramAddress, this.ramData);
         *  int date = (this.ramData[0] >> 4) * 10 + (this.ramData[0] & 0x0F);
         *
         *  // read month converting from BCD
         *  this.ramAddress[0] = MONTH_ADDR;
         *  this.ReadRam(this.ramAddress, this.ramData);
         *  int month = (this.ramData[0] >> 4) * 10 + (this.ramData[0] & 0x0F);
         *
         *  // read year converting from BCD
         *  this.ramAddress[0] = YEAR_ADDR;
         *  this.ReadRam(this.ramAddress, this.ramData);
         *  int year = (this.ramData[0] >> 4) * 10 + (this.ramData[0] & 0x0F);
         *  year += 2000;
         *
         *  dateTime = new DateTime(year, month, date, hour, minute, second);
         *
         *  return dateTime;
         * }*/
        /// <summary>
        /// Get the current Date and Time from the DS1307.
        /// </summary>
        /// <returns>The current date and time as a System.DateTime object.</returns>
        public Task <DateTime> GetDateAsync()
        {
            DateTime returnValue = DateTime.MinValue;

            byte[] readBuffer  = new byte[7];
            byte[] writeBuffer = new byte[] { RTC_ADDRESS };

            this.i2c.ReadBytes(RTC_ADDRESS, (byte)readBuffer.Length, readBuffer);

            returnValue = new DateTime
                          (
                Bcd.ToInt(readBuffer[6]) + 2000,            // Year
                Bcd.ToInt(readBuffer[5]),                   // Month
                Bcd.ToInt(readBuffer[4]),                   // Day
                Bcd.ToInt((byte)(readBuffer[2] & 0x3f)),    // Hours over 24 hours (bit 6 is 24/12 hour format; 1 = 12, 0 = 24)
                Bcd.ToInt(readBuffer[1]),                   // Minutes
                Bcd.ToInt((byte)(readBuffer[0] & 0x7f))     // Seconds (bit 7 is the clock halt bit; 0 = enabled, 1 = halted)
                          );

            return(Task <DateTime> .FromResult(returnValue));
        }
示例#3
0
 public Bcd(int value)
 {
     _value = Bcd.FromInt(value);
 }