Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Example Code For PCA9685 PWM 12bit 16 channel

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
hobby

Bascom Member



Joined: 12 Apr 2004
Posts: 109
Location: Brussels

belgium.gif
PostPosted: Fri Dec 30, 2011 2:53 am    Post subject: Example Code For PCA9685 PWM 12bit 16 channel Reply with quote

Hi,


Example of using PCA9685:
Small video: http://youtu.be/AkR0PxUkPNk

Code:

$regfile = "m324pdef.dat"
$crystal = 8000000
$hwstack = 32
$swstack = 20
$framesize = 40
$lib "i2c_twi.lbx"
' Config I2C
Config Scl = Portc.0
Config Sda = Portc.1
Config I2cdelay = 400000

Const Pca9685adw = &H82                                     ' Write Adress (A0 to VCC and A1-A5 to gnd )
Const Pca9685m1 = &H00                                      ' Register Adress Mode 1
Const Pca9685m2 = &H01                                      ' Register Adress Mode 2
Const Led0 = &H06                                           ' Register adress LED0 next +4
Const Speedfront = 250                                      ' Define waitms before next led front or left
Const Speedback = 100                                       ' Define waitms before next led back or right
Const Minpower = 0                                          ' Define minimum power to star led light
Const Maxpower = 4095                                       ' define max power
Const Boostspeed = 9                                        ' reduce the step 4096 is big

Dim A As Byte
Dim Temp As Byte
Dim Temp2 As Byte
Dim Z As Word
Dim Inv As Word
Dim Pwml As Byte
Dim Pwmh As Byte
Dim First As Bit

' Reset device
I2cinit
I2cstart
I2cwbyte Pca9685adw
I2cwbyte Pca9685m1
I2cwbyte &H01
I2cstop
Waitms 10
' Set MODE 1
I2cstart
I2cwbyte Pca9685adw
I2cwbyte Pca9685m1
I2cwbyte &H81                                               ' PCA Responds to Led All Call  and Restart enabled
I2cwbyte &HA1                                               ' enable auto increment
I2cstop
' Set MODE 2
I2cstart
I2cwbyte Pca9685adw
I2cwbyte Pca9685m2
I2cwbyte &H10                                               ' Set the output pin mode (direct to led)
I2cstop
' ALL LED OFF
For A = 0 To 15
   Temp = 4 * A
   Temp = Temp + Led0
   I2cstart
   I2cwbyte Pca9685adw
   I2cwbyte Temp
   I2cwbyte &H00
   I2cwbyte &H00
   I2cwbyte &H00
   I2cwbyte &H00
   I2cstop
Next
First = 1
Do
   For A = 0 To 15 Step 1
       Temp = 4 * A
       Temp = Temp + Led0
       Temp2 = Temp - 4
      For Z = Minpower To Maxpower Step Boostspeed
         If First = 1 Or A > 0 Then
            Pwml = Low(z)
            Pwmh = High(z)
            I2cstart
            I2cwbyte Pca9685adw
            I2cwbyte Temp
            I2cwbyte &H00
            I2cwbyte &H00
            I2cwbyte Pwml
            I2cwbyte Pwmh
            I2cstop
         End If
         If A > 0 Then
            Inv = Maxpower - Z
            Pwml = Low(inv)
            Pwmh = High(inv)
            I2cstart
            I2cwbyte Pca9685adw
            I2cwbyte Temp2
            I2cwbyte &H00
            I2cwbyte &H00
            I2cwbyte Pwml
            I2cwbyte Pwmh
            I2cstop
         End If
      Next

      Waitms Speedfront
   Next
   First = 0
   For A = 15 To 0 Step -1
       Temp = 4 * A
       Temp = Temp + Led0
       Temp2 = Temp - 4
      For Z = Minpower To Maxpower Step Boostspeed
         If A > 0 Then
            Inv = Maxpower - Z
            Pwml = Low(inv)
            Pwmh = High(inv)
            I2cstart
            I2cwbyte Pca9685adw
            I2cwbyte Temp
            I2cwbyte &H00
            I2cwbyte &H00
            I2cwbyte Pwml
            I2cwbyte Pwmh
            I2cstop
         End If
         Pwml = Low(z)
         Pwmh = High(z)
         I2cstart
         I2cwbyte Pca9685adw
         I2cwbyte Temp2
         I2cwbyte &H00
         I2cwbyte &H00
         I2cwbyte Pwml
         I2cwbyte Pwmh
         I2cstop
      Next
      Waitms Speedback
   Next
Loop
End
 


Thanks Olivier
Back to top
View user's profile
Luciano

Bascom Member



Joined: 29 Nov 2004
Posts: 3149
Location: Italy

blank.gif
PostPosted: Fri Dec 30, 2011 1:39 pm    Post subject: Reply with quote

Nice! Here is the audio for your video: LINK

Best regards,

Luciano
Back to top
View user's profile
PaulC

Bascom Member



Joined: 09 Jan 2008
Posts: 122
Location: Ireland

ireland.gif
PostPosted: Fri Dec 30, 2011 2:19 pm    Post subject: excellent share Reply with quote

excellent share
thank you

_________________
Bascom Avr 2.0.7.3
Works for me
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5119
Location: Holland

blank.gif
PostPosted: Fri Dec 30, 2011 4:09 pm    Post subject: Reply with quote

well done! Thanks for sharing and the video!
_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum