Example of PCA9532 being used on the Embedded Artists Baseboard, this time fading up LEDs using PWM

Dependencies:   mbed

Committer:
chris
Date:
Fri May 07 12:41:06 2010 +0000
Revision:
0:0df46cf615c3

        

Who changed what in which revision?

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