Skip to content

Instantly share code, notes, and snippets.

@electronut
Created June 29, 2013 11:04
Show Gist options
  • Save electronut/5890752 to your computer and use it in GitHub Desktop.
Save electronut/5890752 to your computer and use it in GitHub Desktop.
Raspberry Pi communicating with I2C PWM chip CAT9532 (16-bit Programmable LED Dimmer)
###############################################################################
# rpi-cat9532.py
#
# Author: electronut.in
#
# Description:
#
# Raspberry Pi communicating with I2C PWM chip CAT9532
# (16-bit Programmable LED Dimmer)
#
###############################################################################
import sys
import smbus
import time
# registers
INPUT0 = 0x00
INPUT1 = 0x01
PSC0 = 0x02
PWM0 = 0x03
PSC1 = 0x04
PWM1 = 0x05
LS0 = 0x06
LS1 = 0x07
LS2 = 0x08
LS3 = 0x09
# test based on CAT9532 data sheet example
# LED 0-3: ON
# LED 4-7: Dimming 30%, Blink 1: 152 Hz, 30% duty cycle
# LED 8-11: Blink at 2 Hz, 50% duty cycle (Blink 2)
# LED 12-15: OFF
def test1():
bus = smbus.SMbus(1)
addr = 0x60
# auto increment (AI) flag is set to 0
bus.write_byte_data(addr, 0x02, 0x00)
bus.write_byte_data(addr, 0x03, 0x4d)
bus.write_byte_data(addr, 0x04, 0x4b)
bus.write_byte_data(addr, 0x05, 0x80)
bus.write_byte_data(addr, 0x06, 0x55)
bus.write_byte_data(addr, 0x07, 0xaa)
bus.write_byte_data(addr, 0x08, 0xff)
bus.write_byte_data(addr, 0x09, 0x00)
return
# main() function
def main():
# use sys.argv if needed
print 'starting CAT9532 comms...'
print 'press Ctrl-C to exit.'
bus = smbus.SMBus(1)
addr = 0x60
# set Blink 0 frequency
# T_BLINK0 = (PSC0 + 1)/152
# 0 for always on
bus.write_byte_data(addr, PSC0, 0)
# set PWM0 to 50 %
bus.write_byte_data(addr, PWM0, 128)
# set LED 7 to Blink 0
bus.write_byte_data(addr, LS1, 0b10000000)
# set Blink 1 frequency
# T_BLINK1 = (PSC1 + 1)/152
# for f = 3 Hz, PSC0 = 152/3 - 1 = 50
bus.write_byte_data(addr, PSC1, 50)
# set PWM1 to 50 %
bus.write_byte_data(addr, PWM1, 128)
# set LED 8 to Blink 1
bus.write_byte_data(addr, LS2, 0b00000011)
while True:
try:
# generate duty cycle array for 0-255-0
# to "pulse" the LED intensity
vals = range(0, 256, 10)
vals = vals + vals[::-1]
for i in vals:
bus.write_byte_data(addr, PWM0, i)
time.sleep(0.025)
except IOError, err:
print err
except KeyboardInterrupt:
# turn off LEDs
bus.write_byte_data(addr, LS1, 0x0)
bus.write_byte_data(addr, LS2, 0x0)
print 'bye...'
exit()
# call main
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment