Merge pull request #582 from ssnover95/dev

Remove scaling of user input and add comments to SetPWMFreq function in PCA9685
This commit is contained in:
Ron Evans 2018-07-10 07:47:53 +02:00 committed by GitHub
commit 676da058d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 4 deletions

View File

@ -104,12 +104,14 @@ func (p *PCA9685Driver) SetPWM(channel int, on uint16, off uint16) (err error) {
// SetPWMFreq sets the PWM frequency in Hz // SetPWMFreq sets the PWM frequency in Hz
func (p *PCA9685Driver) SetPWMFreq(freq float32) error { func (p *PCA9685Driver) SetPWMFreq(freq float32) error {
freq *= 0.9 // IC oscillator frequency is 25 MHz
var prescalevel float32 = 25000000 var prescalevel float32 = 25000000
// Find frequency of PWM waveform
prescalevel /= 4096 prescalevel /= 4096
// Ratio between desired frequency and maximum
prescalevel /= freq prescalevel /= freq
prescalevel -= 1 prescalevel -= 1
// Round value to nearest whole
prescale := byte(prescalevel + 0.5) prescale := byte(prescalevel + 0.5)
if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1)}); err != nil { if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1)}); err != nil {
@ -121,20 +123,23 @@ func (p *PCA9685Driver) SetPWMFreq(freq float32) error {
return err return err
} }
// Put oscillator in sleep mode, clear bit 7 here to avoid overwriting
// previous setting
newmode := (oldmode & 0x7F) | 0x10 newmode := (oldmode & 0x7F) | 0x10
if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1), byte(newmode)}); err != nil { if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1), byte(newmode)}); err != nil {
return err return err
} }
// Write prescaler value
if _, err := p.connection.Write([]byte{byte(PCA9685_PRESCALE), prescale}); err != nil { if _, err := p.connection.Write([]byte{byte(PCA9685_PRESCALE), prescale}); err != nil {
return err return err
} }
// Put back to old settings
if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1), byte(oldmode)}); err != nil { if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1), byte(oldmode)}); err != nil {
return err return err
} }
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
// Enable response to All Call address, enable auto-increment, clear restart
if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1), byte(oldmode | 0xa1)}); err != nil { if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1), byte(oldmode | 0xa1)}); err != nil {
return err return err
} }