Now that I have it working I will post a simple example of utilizing the PWM features on the PCA9532. Thanks. Also added an updated (and correct schematic) for anyone that wants to test out this 16 channel PWM LED driver.
Code:
'****************************************************************
'* Name : i2c comms test with led driver *
'* Author : Cody Finden *
'* Version : 1.0 *
'* Notes : SOFTWARE FOR A 16F887 TO CONTROL A PCA9532 *
'* : OVER THE I2C PROTOCOL *
'****************************************************************
#config
__CONFIG _CONFIG1, _HS_OSC & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _CPD_OFF & _BOR_ON & _IESO_ON & _FCMEN_ON & _LVP_OFF
__CONFIG _CONFIG2, _BOR40V & _WRT_OFF
#endconfig
'PCA9532 Control Registers
psc0 con 000010 'frequency prescaler 0 (PERIOD = (PSC0 + 1) / 152)
pwm0 con 000011 'PWM register 0 (0-255 PWM)
psc1 con 000100 'frequency prescaler 1 (PERIOD = (PSC1 + 1) / 152)
pwm1 con 000101 'PWM register 1 (0-255 PWM)
ls0 con 000110 'LED0 to LED3 selector
ls1 con 000111 'LED4 to LED7 selector
ls2 con 001000 'LED8 to LED11 selector
ls3 con 001001 'LED12 to LED15 selector
DEFINE OSC 20 '20MHz crystal
'DEFINE I2C_SLOW 1
'DEFINE I2C_SCLOUT 1
ANSEL = 000000 'no analog, all digital!
ANSELH = 000000
TRISA = 000000 'all ports output
TRISB = 000000
TRISC = 000000
TRISD = 000000
SDA VAR PORTD.3 'i2c data line
SCL VAR PORTD.2 'i2c clock line
led var portc.5 'led for testing
RST VAR portc.6 'pca9532 reset pin (active low)
addr var byte 'pca9532 address storage var
duty0 var byte
duty1 var byte
INIT:
duty0 = 0
duty1 = 255
PAUSE 400 'let hardware settle
RST = 0 'reset pca9532
pause 10
SDA = 1 'i2c pins high on startup
SCL = 1
RST = 1 'pca9532 on
'PCA9532 i2c address(7 bits + 1 bit for R/W)
'Slave address is 1100(A2)(A1)(A0) + (R/(/W))
'In my setup A2 is pulled low, A1 pulled low, A0 pulled low
addr = 000000
MAIN:
I2CWRITE SDA,SCL,ADDR,psc0,[$00] 'set both pwm prescalers to max pwm freq
I2CWRITE SDA,SCL,ADDR,psc1,[$00]
duty0 = duty0 + 1 'increment duty0 for pwm0
duty1 = duty1 - 1 'decrement duty1 for pwm1
I2CWRITE SDA,SCL,ADDR,pwm0,[duty0] 'set duty cycle on pwm0
I2CWRITE SDA,SCL,ADDR,pwm1,[duty1] 'set duty cycle on pwm1
I2CWRITE SDA,SCL,ADDR,ls0,[101010] 'enable led0-3 with pwm0
I2CWRITE SDA,SCL,ADDR,ls1,[111111] 'enable led4-7 with pwm1
if duty0=255 then 'restart count on overflow
duty0=0
endif
if duty1=0 then 'restart count on overflow
duty1=255
endif
pause 5 'delay for viewing pleasure
' led = 1 'blink led for feedback
' PAUSE 50
' led = 0
' pause 50
goto main
Bookmarks