Example code and library for the PCF9532 I2C LED driver on the Embedded Artists baseboard

Dependencies:   mbed

Committer:
chris
Date:
Tue Apr 27 13:03:05 2010 +0000
Revision:
0:a64165168954

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris 0:a64165168954 1 /* mbed PCF9532 LED Driver Library
chris 0:a64165168954 2 *
chris 0:a64165168954 3 * Copyright (c) 2010, cstyles (http://mbed.org)
chris 0:a64165168954 4 *
chris 0:a64165168954 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
chris 0:a64165168954 6 * of this software and associated documentation files (the "Software"), to deal
chris 0:a64165168954 7 * in the Software without restriction, including without limitation the rights
chris 0:a64165168954 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
chris 0:a64165168954 9 * copies of the Software, and to permit persons to whom the Software is
chris 0:a64165168954 10 * furnished to do so, subject to the following conditions:
chris 0:a64165168954 11 *
chris 0:a64165168954 12 * The above copyright notice and this permission notice shall be included in
chris 0:a64165168954 13 * all copies or substantial portions of the Software.
chris 0:a64165168954 14 *
chris 0:a64165168954 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
chris 0:a64165168954 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
chris 0:a64165168954 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
chris 0:a64165168954 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
chris 0:a64165168954 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
chris 0:a64165168954 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
chris 0:a64165168954 21 * THE SOFTWARE.
chris 0:a64165168954 22 */
chris 0:a64165168954 23
chris 0:a64165168954 24
chris 0:a64165168954 25 #include "PCA9532.h"
chris 0:a64165168954 26 #include "mbed.h"
chris 0:a64165168954 27
chris 0:a64165168954 28 /*
chris 0:a64165168954 29 Constructor, pin names for I2C and the I2C addrss of the device
chris 0:a64165168954 30 */
chris 0:a64165168954 31 PCA9532::PCA9532(PinName scl, PinName sda, int addr)
chris 0:a64165168954 32 : _i2c(scl, sda) {
chris 0:a64165168954 33
chris 0:a64165168954 34 _i2c.frequency(1000000);
chris 0:a64165168954 35
chris 0:a64165168954 36 _addr = addr;
chris 0:a64165168954 37
chris 0:a64165168954 38 }
chris 0:a64165168954 39
chris 0:a64165168954 40
chris 0:a64165168954 41
chris 0:a64165168954 42
chris 0:a64165168954 43
chris 0:a64165168954 44 /*
chris 0:a64165168954 45 Set the period
chris 0:a64165168954 46 */
chris 0:a64165168954 47 int PCA9532::Period (int channel, float period) {
chris 0:a64165168954 48
chris 0:a64165168954 49 char reg = 0;
chris 0:a64165168954 50
chris 0:a64165168954 51 if (channel == 0) {
chris 0:a64165168954 52 reg = PCA9532_REG_PSC0;
chris 0:a64165168954 53 } else if (channel == 1) {
chris 0:a64165168954 54 reg = PCA9532_REG_PSC1;
chris 0:a64165168954 55 } else {
chris 0:a64165168954 56 return (1);
chris 0:a64165168954 57 }
chris 0:a64165168954 58
chris 0:a64165168954 59 if (period > 1.0) {
chris 0:a64165168954 60 period = 255;
chris 0:a64165168954 61 } else if ( period < 0.0 ) {
chris 0:a64165168954 62 period = 0;
chris 0:a64165168954 63 } else {
chris 0:a64165168954 64 period = 256 * period;
chris 0:a64165168954 65 }
chris 0:a64165168954 66
chris 0:a64165168954 67 _write(reg, period);
chris 0:a64165168954 68 return(0);
chris 0:a64165168954 69
chris 0:a64165168954 70 }
chris 0:a64165168954 71
chris 0:a64165168954 72
chris 0:a64165168954 73 /*
chris 0:a64165168954 74 Set the duty cycle
chris 0:a64165168954 75 */
chris 0:a64165168954 76 int PCA9532::Duty (int channel, float d) {
chris 0:a64165168954 77
chris 0:a64165168954 78 char duty = 0;
chris 0:a64165168954 79 char reg = 0;
chris 0:a64165168954 80
chris 0:a64165168954 81 if (channel == 0) {
chris 0:a64165168954 82 reg = PCA9532_REG_PWM0;
chris 0:a64165168954 83 } else if (channel == 1) {
chris 0:a64165168954 84 reg = PCA9532_REG_PWM1;
chris 0:a64165168954 85 } else {
chris 0:a64165168954 86 return (1);
chris 0:a64165168954 87 }
chris 0:a64165168954 88
chris 0:a64165168954 89 if (d > 1.0) {
chris 0:a64165168954 90 duty = 255;
chris 0:a64165168954 91 } else if ( d < 0.0 ) {
chris 0:a64165168954 92 duty = 0;
chris 0:a64165168954 93 } else {
chris 0:a64165168954 94 duty = 256 * d;
chris 0:a64165168954 95 }
chris 0:a64165168954 96
chris 0:a64165168954 97 _write(reg, duty);
chris 0:a64165168954 98 return(0);
chris 0:a64165168954 99
chris 0:a64165168954 100 }
chris 0:a64165168954 101
chris 0:a64165168954 102 /*
chris 0:a64165168954 103 Set each of the LEDs in this mask to the give mode
chris 0:a64165168954 104 Loop through the mask calling SetLed on each match
chris 0:a64165168954 105 alt_mode specifies the mode of the non-Matches of SetMode
chris 0:a64165168954 106 */
chris 0:a64165168954 107 int PCA9532::SetMode (int mask, int mode) {
chris 0:a64165168954 108 if ( (mode < 0) || (mode > 3) ) {
chris 0:a64165168954 109 return(1);
chris 0:a64165168954 110 } else {
chris 0:a64165168954 111 for (int i=0 ; i < 16 ; i++ ) {
chris 0:a64165168954 112
chris 0:a64165168954 113 // if this matches, set the LED to the mode
chris 0:a64165168954 114 if (mask & (0x1 << i)) {
chris 0:a64165168954 115 SetLed(i,mode);
chris 0:a64165168954 116 }
chris 0:a64165168954 117 }
chris 0:a64165168954 118 }
chris 0:a64165168954 119 return(0);
chris 0:a64165168954 120 }
chris 0:a64165168954 121
chris 0:a64165168954 122
chris 0:a64165168954 123
chris 0:a64165168954 124 /*
chris 0:a64165168954 125 led is in the range 0-15
chris 0:a64165168954 126 mode is inthe range 0-3
chris 0:a64165168954 127 */
chris 0:a64165168954 128 int PCA9532::SetLed(int led, int mode) {
chris 0:a64165168954 129
chris 0:a64165168954 130 int reg = 0;
chris 0:a64165168954 131 int offset = (led % 4);
chris 0:a64165168954 132
chris 0:a64165168954 133 printf("\nSetLed(%d,%d)\n", led, mode);
chris 0:a64165168954 134
chris 0:a64165168954 135 // makesure mode is within bounds
chris 0:a64165168954 136 if ( (mode < 0) || (mode > 3) ) {
chris 0:a64165168954 137 printf("Error : Invalid mode supplied\n");
chris 0:a64165168954 138 return(1);
chris 0:a64165168954 139 }
chris 0:a64165168954 140
chris 0:a64165168954 141 // determine which register this is,
chris 0:a64165168954 142 if (led < 4) {
chris 0:a64165168954 143 reg = PCA9532_REG_LS0;
chris 0:a64165168954 144 } else if ( (led > 3) && (led < 8) ) {
chris 0:a64165168954 145 reg = PCA9532_REG_LS1;
chris 0:a64165168954 146 } else if ( (led > 7) && (led < 12) ) {
chris 0:a64165168954 147 reg = PCA9532_REG_LS2;
chris 0:a64165168954 148 } else if ( (led > 11) && (led < 16) ) {
chris 0:a64165168954 149 reg = PCA9532_REG_LS3;
chris 0:a64165168954 150 } else {
chris 0:a64165168954 151 return(1);
chris 0:a64165168954 152 }
chris 0:a64165168954 153
chris 0:a64165168954 154 // read the current status of the register
chris 0:a64165168954 155 char regval = _read(reg);
chris 0:a64165168954 156
chris 0:a64165168954 157 // clear the two bit slice at the calculated offset
chris 0:a64165168954 158 regval &= ~(0x3 << (2 * offset));
chris 0:a64165168954 159
chris 0:a64165168954 160 // now OR in the mode, shifted by 2*offset
chris 0:a64165168954 161 regval |= (mode << (2 * offset));
chris 0:a64165168954 162
chris 0:a64165168954 163 // write the new value back
chris 0:a64165168954 164 _write(reg, regval);
chris 0:a64165168954 165
chris 0:a64165168954 166 return(0);
chris 0:a64165168954 167 }
chris 0:a64165168954 168
chris 0:a64165168954 169
chris 0:a64165168954 170
chris 0:a64165168954 171 // private functions for low level IO
chris 0:a64165168954 172
chris 0:a64165168954 173 void PCA9532::_write(int reg, int data) {
chris 0:a64165168954 174 char args[2];
chris 0:a64165168954 175 args[0] = reg;
chris 0:a64165168954 176 args[1] = data;
chris 0:a64165168954 177 _i2c.write(_addr, args,2);
chris 0:a64165168954 178 }
chris 0:a64165168954 179
chris 0:a64165168954 180 int PCA9532::_read(int reg) {
chris 0:a64165168954 181 char args[2];
chris 0:a64165168954 182 args[0] = reg;
chris 0:a64165168954 183 _i2c.write(_addr, args, 1);
chris 0:a64165168954 184 _i2c.read(_addr, args, 1);
chris 0:a64165168954 185 return(args[0]);
chris 0:a64165168954 186 }
chris 0:a64165168954 187
chris 0:a64165168954 188
chris 0:a64165168954 189
chris 0:a64165168954 190
chris 0:a64165168954 191