Example #1
0
        public void SetDesiredFrequency(double frequency)
        {
            frequency *= 0.9f;  // Correct for overshoot in the frequency setting.
            double prescaleval = 25000000;

            prescaleval /= 4096;
            prescaleval /= frequency;
            prescaleval -= 1;

            byte prescale = (byte)Math.Floor(prescaleval + 0.5f);

            int oldMode = i2cDevice.ReadFromAddress((byte)PwmRegister.MODE1);
            int newMode = (oldMode & 0x7f) | 0x10;

            this.i2cDevice.WriteToAddress((byte)PwmRegister.MODE1, (byte)newMode);
            this.i2cDevice.WriteToAddress((byte)PwmRegister.PRESCALE, (byte)prescale);
            this.i2cDevice.WriteToAddress((byte)PwmRegister.MODE1, (byte)oldMode);

            this.i2cDevice.WriteToAddress((byte)PwmRegister.MODE1, (byte)(oldMode | 0xa1));
        }