RGB LED Driver demo

Dependents:   NJU6063_HelloWorld

Committer:
og
Date:
Mon Jan 25 08:59:03 2016 +0000
Revision:
4:7a4b9d444b87
Parent:
3:f83202c2ae59
STM32F042K6 don't work

Who changed what in which revision?

UserRevisionLine numberNew contents of line
og 0:0283db13b7f1 1 #include "NJU6063.h"
og 4:7a4b9d444b87 2 //#ifndef TARGET_STM
og 0:0283db13b7f1 3 NJU6063::NJU6063( PinName sda, PinName scl, PinName rst)
og 0:0283db13b7f1 4 : _i2c(sda, scl), _rst(rst)
og 0:0283db13b7f1 5 {
og 2:137675ac48ab 6 //_i2c.frequency(400000);
og 0:0283db13b7f1 7 }
og 0:0283db13b7f1 8
og 0:0283db13b7f1 9 void NJU6063::reset(void)
og 0:0283db13b7f1 10 {
og 3:f83202c2ae59 11 NJU6063_WAIT_MS;
og 0:0283db13b7f1 12 _rst = 0;
og 3:f83202c2ae59 13 NJU6063_WAIT_MS;
og 0:0283db13b7f1 14 _rst = 1;
og 0:0283db13b7f1 15 }
og 0:0283db13b7f1 16
og 2:137675ac48ab 17 uint8_t NJU6063::set_multi_device(uint8_t n)
og 0:0283db13b7f1 18 {
og 3:f83202c2ae59 19 //_myI2c = (I2C_TypeDef*)I2C_1;
og 3:f83202c2ae59 20 //IS_I2C_NO_STRETCH(I2C_NOSTRETCH_ENABLE);
og 3:f83202c2ae59 21 uint8_t ret;
og 0:0283db13b7f1 22 char data[3];
og 3:f83202c2ae59 23 ret = n;
og 3:f83202c2ae59 24 data[0] = 0x00; // Initial chip addres
og 3:f83202c2ae59 25 data[1] = NJU6063_MADRES; // multi device address
og 4:7a4b9d444b87 26 #ifdef TARGET_NUCLEO_F042K6
og 4:7a4b9d444b87 27 for (uint8_t i=1; i<=n; i++) {
og 4:7a4b9d444b87 28 data[2] = i;
og 4:7a4b9d444b87 29 ack= _i2c.write(NJU6063_SLAVE, data, 2);
og 4:7a4b9d444b87 30 NJU6063_WAIT;
og 4:7a4b9d444b87 31 if (ack) {
og 4:7a4b9d444b87 32 ret = i-1;
og 4:7a4b9d444b87 33 break;
og 4:7a4b9d444b87 34 }
og 4:7a4b9d444b87 35 else {
og 4:7a4b9d444b87 36 ack= _i2c.write(NJU6063_SLAVE, data, 3);
og 4:7a4b9d444b87 37 }
og 4:7a4b9d444b87 38 }
og 4:7a4b9d444b87 39 #else
og 0:0283db13b7f1 40 for (uint8_t i=1; i<=n; i++) {
og 0:0283db13b7f1 41 data[2] = i;
og 2:137675ac48ab 42 ack= _i2c.write(NJU6063_SLAVE, data, 3);
og 3:f83202c2ae59 43 NJU6063_WAIT;
og 2:137675ac48ab 44 if (ack) {
og 3:f83202c2ae59 45 ret = i-1;
og 3:f83202c2ae59 46 break;
og 2:137675ac48ab 47 }
og 0:0283db13b7f1 48 }
og 4:7a4b9d444b87 49 #endif
og 3:f83202c2ae59 50 return(ret);
og 0:0283db13b7f1 51 }
og 0:0283db13b7f1 52 void NJU6063::init(uint8_t chip_addr, uint8_t d)
og 0:0283db13b7f1 53 {
og 0:0283db13b7f1 54 char data[3];
og 0:0283db13b7f1 55 data[0] = chip_addr;
og 3:f83202c2ae59 56 data[1] = NJU6063_INIT;
og 0:0283db13b7f1 57 data[2] = d;
og 2:137675ac48ab 58 ack = _i2c.write(NJU6063_SLAVE, data, 3);
og 3:f83202c2ae59 59 NJU6063_WAIT;
og 0:0283db13b7f1 60 }
og 0:0283db13b7f1 61
og 0:0283db13b7f1 62 void NJU6063::set_iled(uint8_t chip_addr, uint8_t d1, uint8_t d2, uint8_t d3)
og 0:0283db13b7f1 63 {
og 0:0283db13b7f1 64 char data[3];
og 0:0283db13b7f1 65 data[0] = chip_addr;
og 3:f83202c2ae59 66 data[1] = NJU6063_ILED;
og 0:0283db13b7f1 67 data[2] = (0x03&d1) | (0x03&d2)<<2 | (0x03&d3) <<4;
og 2:137675ac48ab 68 ack = _i2c.write(NJU6063_SLAVE, data, 3);
og 3:f83202c2ae59 69 NJU6063_WAIT;
og 0:0283db13b7f1 70 }
og 0:0283db13b7f1 71
og 0:0283db13b7f1 72 void NJU6063::set_pwm(uint8_t chip_addr, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t loop, uint8_t son)
og 0:0283db13b7f1 73 {
og 0:0283db13b7f1 74 char data[7];
og 0:0283db13b7f1 75 data[0] = chip_addr;
og 3:f83202c2ae59 76 data[1] = NJU6063_PWM1;
og 0:0283db13b7f1 77 data[2] = d1;
og 0:0283db13b7f1 78 data[3] = d2;
og 0:0283db13b7f1 79 data[4] = d3;
og 0:0283db13b7f1 80 data[5] = loop;
og 0:0283db13b7f1 81 data[6] = son;
og 2:137675ac48ab 82 ack = _i2c.write(NJU6063_SLAVE, data, 7);
og 3:f83202c2ae59 83 NJU6063_WAIT;
og 0:0283db13b7f1 84 }
og 0:0283db13b7f1 85 void NJU6063::dim_start(uint8_t chip_addr)
og 0:0283db13b7f1 86 {
og 0:0283db13b7f1 87 char data[3];
og 0:0283db13b7f1 88 data[0] = chip_addr;
og 3:f83202c2ae59 89 data[1] = NJU6063_START; // reg addres
og 0:0283db13b7f1 90 data[2] = 0x01; // start
og 2:137675ac48ab 91 ack = _i2c.write(NJU6063_SLAVE, data, 3);
og 3:f83202c2ae59 92 NJU6063_WAIT;
og 0:0283db13b7f1 93 }
og 0:0283db13b7f1 94
og 1:bbc915c814da 95 void NJU6063::dim_stop(uint8_t chip_addr)
og 1:bbc915c814da 96 {
og 1:bbc915c814da 97 char data[3];
og 1:bbc915c814da 98 data[0] = chip_addr;
og 3:f83202c2ae59 99 data[1] = NJU6063_START; // reg addres
og 1:bbc915c814da 100 data[2] = 0x02; // stop
og 2:137675ac48ab 101 ack = _i2c.write(NJU6063_SLAVE, data, 3);
og 3:f83202c2ae59 102 NJU6063_WAIT;
og 1:bbc915c814da 103 }
og 1:bbc915c814da 104
og 1:bbc915c814da 105 void NJU6063::check_dim(void)
og 0:0283db13b7f1 106 {
og 2:137675ac48ab 107 uint8_t timeout = 0x0f;
og 0:0283db13b7f1 108 char data[3];
og 0:0283db13b7f1 109 data[0] = 0xff;
og 3:f83202c2ae59 110 data[1] = NJU6063_DCHK;
og 0:0283db13b7f1 111 data[2] = 0x00;
og 0:0283db13b7f1 112 do {
og 4:7a4b9d444b87 113 ack = _i2c.write(NJU6063_SLAVE, data, 3, true);
og 3:f83202c2ae59 114 NJU6063_WAIT;
og 2:137675ac48ab 115 timeout--;
og 3:f83202c2ae59 116 if (timeout==0) break;
og 2:137675ac48ab 117 } while(!ack);
og 3:f83202c2ae59 118 NJU6063_WAIT_MS;
og 0:0283db13b7f1 119 }
og 4:7a4b9d444b87 120 //#endif
og 3:f83202c2ae59 121