robot.sl/CarControl/PwmController.cs
/// <summary>
/// Set the frequency (defaults to 60Hz if not set). 1 Hz equals 1 full pwm cycle per second.
/// </summary>
/// <param name="frequency">Frequency in Hz</param>
Set the frequency (defaults to 60Hz if not set). 1 Hz equals 1 full pwm cycle per second.
Set the frequency ( defaults to 60Hz if not set ) . 1 Hz equals 1 full pwm cycle per second .
public double SetDesiredFrequency(double frequency)
        {
            if (frequency > MaxFrequency || frequency < MinFrequency)
            {
                throw new ArgumentOutOfRangeException(nameof(frequency), "Frequency must be between 40 and 1000hz");
            }
            frequency *= 0.9f; 
            double prescaleval = 25000000f;
            prescaleval /= 4096;
            prescaleval /= frequency;
            prescaleval -= 1;
            byte prescale = (byte)Math.Floor(prescaleval + 0.5f);
            I2CSynchronous.Call(() =>
            {
                var readBuffer = new byte[1];
                _pwmDevice.WriteRead(new byte[] { (byte)Registers.MODE1 }, readBuffer);
                byte oldmode = readBuffer[0];
                byte newmode = (byte)((oldmode & 0x7F) | 0x10); 
                _pwmDevice.Write(new byte[] { (byte)Registers.MODE1, newmode });
                _pwmDevice.Write(new byte[] { (byte)Registers.PRESCALE, prescale });
                _pwmDevice.Write(new byte[] { (byte)Registers.MODE1, oldmode });
                Task.Delay(5).Wait();
                _pwmDevice.Write(new byte[] { (byte)Registers.MODE1, (byte)(oldmode | 0xa1) });
            });
            ActualFrequency = frequency;
            return ActualFrequency;
        }
public double SetDesiredFrequency ( double frequency ) { if ( frequency > MaxFrequency || frequency < MinFrequency ) { throw new ArgumentOutOfRangeException ( nameof ( frequency ) , " " ) ; } frequency *= 0.9f ; double prescaleval = 25000000f ; prescaleval /= 4096 ; prescaleval /= frequency ; prescaleval -= 1 ; byte prescale = ( byte ) Math . Floor ( prescaleval + 0.5f ) ; I2CSynchronous . Call ( ( ) => { var readBuffer = new byte [ 1 ] ; _pwmDevice . WriteRead ( new byte [ ] { ( byte ) Registers . MODE1 } , readBuffer ) ; byte oldmode = readBuffer [ 0 ] ; byte newmode = ( byte ) ( ( oldmode & 0x7F ) | 0x10 ) ; _pwmDevice . Write ( new byte [ ] { ( byte ) Registers . MODE1 , newmode } ) ; _pwmDevice . Write ( new byte [ ] { ( byte ) Registers . PRESCALE , prescale } ) ; _pwmDevice . Write ( new byte [ ] { ( byte ) Registers . MODE1 , oldmode } ) ; Task . Delay ( 5 ) . Wait ( ) ; _pwmDevice . Write ( new byte [ ] { ( byte ) Registers . MODE1 , ( byte ) ( oldmode | 0xa1 ) } ) ; } ) ; ActualFrequency = frequency ; return ActualFrequency ; }
Set the frequency (defaults to 60Hz if not set).
Set the frequency ( defaults to 60Hz if not set ) .
//Correct for overshoot in the frequency setting (see issue #11). //sleep
frequency
double
Frequency in Hz
Frequency in Hz