initial commit

Dependencies:   mbed

Fork of empc_pdu_v3 by gami

Files at this revision

API Documentation at this revision

Comitter:
rcflyair
Date:
Fri Apr 07 16:12:08 2017 +0000
Parent:
0:d58e47b3cc7e
Commit message:
initial commit;

Changed in this revision

SWITCH.cpp Show annotated file Show diff for this revision Revisions of this file
SWITCH.h Show annotated file Show diff for this revision Revisions of this file
TLC59116.cpp Show annotated file Show diff for this revision Revisions of this file
TLC59116.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r d58e47b3cc7e -r 1686fedb8e0a SWITCH.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SWITCH.cpp	Fri Apr 07 16:12:08 2017 +0000
@@ -0,0 +1,20 @@
+#include "SWITCH.h"
+
+SWITCH::SWITCH(PinName sw0, PinName sw1, PinName sw2, PinName sw3) : _sw0(sw0), _sw1(sw1), _sw2(sw2), _sw3(sw3)
+{
+    
+}    
+
+int SWITCH::GetMode(void)
+{
+    if (!_sw0.read()) return 1;
+    if (!_sw1.read()) return 2;
+    return 0;
+}
+
+int SWITCH::GetMove(void)
+{
+    if (!_sw2.read()) return 1;
+    if (!_sw3.read()) return 2;
+    return 0;
+}
\ No newline at end of file
diff -r d58e47b3cc7e -r 1686fedb8e0a SWITCH.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SWITCH.h	Fri Apr 07 16:12:08 2017 +0000
@@ -0,0 +1,21 @@
+#ifndef SWITCH_H
+#define SWITCH_H
+
+#include <mbed.h>
+
+class SWITCH
+{
+    private:
+        DigitalIn _sw0;
+        DigitalIn _sw1;
+        DigitalIn _sw2;
+        DigitalIn _sw3;
+        
+    public:
+        SWITCH(PinName sw0, PinName sw1, PinName sw2, PinName sw3);
+        int GetMode(void);
+        int GetMove(void);
+};
+
+
+#endif // SWITCH
\ No newline at end of file
diff -r d58e47b3cc7e -r 1686fedb8e0a TLC59116.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TLC59116.cpp	Fri Apr 07 16:12:08 2017 +0000
@@ -0,0 +1,164 @@
+#include "TLC59116.h"
+
+TLC59116::TLC59116(PinName sda, PinName scl, PinName cs) : _i2c(sda, scl), _cs(cs)
+{
+    for (int i = 0; i < sizeof(b); i++) b[i] = 0;               // clear internal buffer
+    _i2c.frequency(400000);                                     // set i2c buffer frequency
+}    
+
+void TLC59116::Enable(void)
+{
+    _cs = 1;
+    wait_ms(1);
+}
+
+void TLC59116::Disable(void)
+{
+    _cs = 0;
+    wait_ms(1);
+}
+
+int TLC59116::SWRST()
+{
+    // TLC59116 SWRST
+    b[0] = 0xA5;
+    b[1] = 0x5A;
+    return(_i2c.write(0xD6, b, 2));
+}
+
+int TLC59116::init(void)
+{
+    // TLC59116 FOO
+    b[ 0] = AUTO_INCREMENT_ALL;             // COMMAND
+    b[ 1] = 0x00;                           // MODE1
+    b[ 2] = 0x00;                           // MODE2
+    b[ 3] = 0xFF;                           // PWM0
+    b[ 4] = 0xFF;                           // PWM1
+    b[ 5] = 0xFF;                           // PWM2
+    b[ 6] = 0xFF;                           // PWM3
+    b[ 7] = 0xFF;                           // PWM4
+    b[ 8] = 0xFF;                           // PWM5
+    b[ 9] = 0xFF;                           // PWM6
+    b[10] = 0xFF;                           // PWM7
+    b[11] = 0xFF;                           // PWM8
+    b[12] = 0xFF;                           // PWM9
+    b[13] = 0xFF;                           // PWM10
+    b[14] = 0xFF;                           // PWM11
+    b[15] = 0xFF;                           // PWM12
+    b[16] = 0xFF;                           // PWM13
+    b[17] = 0xFF;                           // PWM14
+    b[18] = 0xFF;                           // PWM15
+    b[19] = 0xFF;                           // GRPPWM
+    b[20] = 0x00;                           // GRPFREQ
+    b[21] = 0x00;                           // LEDOUT0
+    b[22] = 0x00;                           // LEDOUT1
+    b[23] = 0x00;                           // LEDOUT2
+    b[24] = 0x00;                           // LEDOUT3
+    return(_i2c.write(TLC59116_ADDRESS, b, 25));
+}
+
+int TLC59116::SetBrightness(int x)
+{
+    b[0] = AUTO_INCREMENT_NONE;                    // set mask for no auto increment
+    b[0] = b[0] | GRPPWM;                          // mask in group pwm control register
+    b[1] = x;                                      // Brightness - PWM value between 0 and 255
+    return(_i2c.write(TLC59116_ADDRESS, b, 2));
+}
+
+void TLC59116::SetAuto(bool v)
+{
+    b[0] = AUTO_INCREMENT_NONE;                     // set mask for no auto increment
+    b[0] = b[0] | LEDOUT3;                          // mask in LEDOUT3 register
+    _i2c.write(TLC59116_ADDRESS, b, 1);             // write register address
+    _i2c.read(TLC59116_ADDRESS, b, 1);              // read register
+    if (v)
+        b[1] = b[0] | 0b00000011;
+    else
+        b[1] = b[0] & 0b11111100;
+    b[0] = AUTO_INCREMENT_NONE;
+    b[0] = b[0] | LEDOUT3;
+    _i2c.write(TLC59116_ADDRESS, b, 2);
+}
+
+void TLC59116::SetManual(bool v)
+{
+    b[0] = AUTO_INCREMENT_NONE;                     // set mask for no auto increment
+    b[0] = b[0] | LEDOUT3;                          // mask in LEDOUT3 register
+    _i2c.write(TLC59116_ADDRESS, b, 1);             // write register address
+    _i2c.read(TLC59116_ADDRESS, b, 1);              // read register
+    if (v)
+        b[1] = b[0] | 0b00001100;
+    else
+        b[1] = b[0] & 0b11110011;
+    b[0] = AUTO_INCREMENT_NONE;
+    b[0] = b[0] | LEDOUT3;
+    _i2c.write(TLC59116_ADDRESS, b, 2);
+}
+
+void TLC59116::SetPower(bool v)
+{
+    b[0] = AUTO_INCREMENT_NONE;                     // set mask for no auto increment
+    b[0] = b[0] | LEDOUT2;                          // mask in LEDOUT3 register
+    _i2c.write(TLC59116_ADDRESS, b, 1);             // write register address
+    _i2c.read(TLC59116_ADDRESS, b, 1);              // read register
+    if (v)
+        b[1] = b[0] | 0b00110000;
+    else
+        b[1] = b[0] & 0b11001111;
+    b[0] = AUTO_INCREMENT_NONE;
+    b[0] = b[0] | LEDOUT2;
+    _i2c.write(TLC59116_ADDRESS, b, 2);
+}
+
+void TLC59116::SetFault(bool v)
+{
+    b[0] = AUTO_INCREMENT_NONE;                     // set mask for no auto increment
+    b[0] = b[0] | LEDOUT2;                          // mask in LEDOUT3 register
+    _i2c.write(TLC59116_ADDRESS, b, 1);             // write register address
+    _i2c.read(TLC59116_ADDRESS, b, 1);              // read register
+    if (v)
+        b[1] = b[0] | 0b11000000;
+    else
+        b[1] = b[0] & 0b00111111;
+    b[0] = AUTO_INCREMENT_NONE;
+    b[0] = b[0] | LEDOUT2;
+    _i2c.write(TLC59116_ADDRESS, b, 2);
+}
+
+// Set bar graph to value between zero and ten.
+//  This routine sets the bar graph only
+void TLC59116::SetBarGraph(int x)
+{
+    const int d[11][4] = {
+        //LEDOUT0     LEDOUT1     LEDOUT2     LEDOUT3
+        0b00000000, 0b00000000, 0b00000000, 0b00000000,     // 0
+        0b00000000, 0b00000000, 0b00001100, 0b00000000,     // 1
+        0b00000000, 0b00000000, 0b00001111, 0b00000000,     // 2
+        0b00000000, 0b11000000, 0b00001111, 0b00000000,     // 3
+        0b00000000, 0b11110000, 0b00001111, 0b00000000,     // 4
+        0b00000000, 0b11111100, 0b00001111, 0b00000000,     // 5
+        0b00000000, 0b11111111, 0b00001111, 0b00000000,     // 6
+        0b11000000, 0b11111111, 0b00001111, 0b00000000,     // 7
+        0b11110000, 0b11111111, 0b00001111, 0b00000000,     // 8
+        0b11111100, 0b11111111, 0b00001111, 0b00000000,     // 9
+        0b11111111, 0b11111111, 0b00001111, 0b00000000      // 10
+    };
+
+    // read current LED status
+    b[0] = AUTO_INCREMENT_ALL;
+    b[0] = b[0] | LEDOUT0;
+    _i2c.write(TLC59116_ADDRESS, b, 1);
+    _i2c.read(TLC59116_ADDRESS, b, 4);
+
+    b[2] = b[2] & ~d[10][2]; // 0b11110000;
+    b[2] = b[2] | d[x][2];
+
+    b[4] = b[3];
+    b[3] = b[2];
+    b[2] = d[x][1];
+    b[1] = d[x][0];
+    b[0] = AUTO_INCREMENT_ALL;
+    b[0] = b[0] | LEDOUT0;
+    _i2c.write(TLC59116_ADDRESS, b, 5);
+
+}
diff -r d58e47b3cc7e -r 1686fedb8e0a TLC59116.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TLC59116.h	Fri Apr 07 16:12:08 2017 +0000
@@ -0,0 +1,95 @@
+#ifndef TLC59116_H
+#define TLC59116_H
+
+#include <mbed.h>
+
+#define TLC59116_ADDRESS (0x60 << 1)
+
+//********** REGISTER MAP DEFINITION
+#define MODE1       0x00;
+#define MODE2       0x01;
+#define PWM0        0x02;
+#define PWM1        0x03;
+#define PWM2        0x04;
+#define PWM3        0x05;
+#define PWM4        0x06;
+#define PWM5        0x07;
+#define PWM6        0x08;
+#define PWM7        0x09;
+#define PWM8        0x0A;
+#define PWM9        0x0B;
+#define PWM10       0x0C;
+#define PWM11       0x0D;
+#define PWM12       0x0E;
+#define PWM13       0x0F;
+#define PWM14       0x10;
+#define PWM15       0x11;
+#define GRPPWM      0x12;
+#define GRPFREQ     0x13;
+#define LEDOUT0     0x14;
+#define LEDOUT1     0x15;
+#define LEDOUT2     0x16;
+#define LEDOUT3     0x17;
+#define SUBADR1     0x18;
+#define SUBADR2     0x19;
+#define SUBADR3     0x1A;
+#define ALLCALLADR  0x1B;
+#define IREF        0x1C;
+#define EFLAG1      0x1D;
+#define EFLAG2      0x1E;
+
+
+//********** CONTROL REGISTER MASK DEFINITION
+//  Auto Increment options
+//  The CONTROL REGISTER three most significant bits contain the auto incrementing flag
+//      and the five least significant the address the starting register address
+//      A12 A11 A10 D4 D3 D2 D1 D0
+//  The following #defines are masks setting the auto increment flag
+//
+//  * No auto increment
+//  000 is used when the same register must be accessed several times during a single I2C bus communication,
+//  for example, changing the brightness of a single LED. Data is overwritten each time the register
+//  is accessed during a write operation.
+#define AUTO_INCREMENT_NONE             0x00;   // mask = 000xxxxx
+//  * Auto increment all registers
+//  100 is used when all the registers must be sequentially accessed, for example, power-up programming.
+#define AUTO_INCREMENT_ALL              0x80;   // mask = 100xxxxx
+//  * Auto increment brightness
+//  101 is used when the four LED drivers must be individually programmed with different values during the
+//  same I2C bus communication, for example, changing a color setting to another color setting.
+#define AUTO_INCREMENT_BRIGHTNESS       0xA0;   // mask = 101xxxxx
+//  * Auto increment control
+//  110 is used when the LED drivers must be globally programmed with different settings during the same
+//  I2C bus communication, for example, global brightness or blinking change.
+#define AUTO_INCREMENT_GLOBAL_CONTROL   0xC0;   // mask = 110xxxxx
+//  * Auto increment brightness and control
+//  111 is used when individually and global changes must be performed during the same
+//  I2C bus communication, for example, changing color and global brightness at the same time.
+#define AUTO_INCREMENT_BRIGHT_CONTROL   0xE0;   // mask = 111xxxxx
+//**********
+
+// TLC59116 Registers
+#define COMMAND 0x00;
+
+
+class TLC59116
+{
+    private:
+        I2C _i2c;
+        DigitalOut _cs;
+        char b[32];
+    public:
+        TLC59116(PinName sda, PinName scl, PinName cs);
+        void Enable(void);
+        void Disable(void);
+        int  SWRST(void);
+        int  init(void);
+        int  SetBrightness(int x);
+        void SetAuto(bool v);
+        void SetManual(bool v);
+        void SetPower(bool v);
+        void SetFault(bool v);
+        void SetBarGraph(int x);
+};
+
+#endif
\ No newline at end of file
diff -r d58e47b3cc7e -r 1686fedb8e0a main.cpp
--- a/main.cpp	Tue Jun 28 18:29:49 2016 +0000
+++ b/main.cpp	Fri Apr 07 16:12:08 2017 +0000
@@ -1,13 +1,53 @@
 #include "mbed.h"
-I2C i2c(D14, D15);        // sda, scl
-Serial pc(USBTX, USBRX); // tx, rx
+#include "TLC59116.h"
+#include "SWITCH.h"
+
+Serial pc(PA_9, PA_10);                 // USB over serial USBTX, USBRX
+DigitalOut led(PA_5);                   // Nucleo-F303RE LED
+TLC59116 pdu(PB_7, PA_15, PB_11);       // TLC59116 LED driver in pdu SDA, SCL, CS
+SWITCH sw(PB_15, PB_13, PB_1, PB_14);
+bool fault = false;
 
 int main()
 {
-    pc.printf("RUN\r\n");
-    for(int i = 0; i < 128 ; i++) {
-        i2c.start();
-        if(i2c.write(i << 1)) pc.printf("0x%x ACK \r\n",i); // Send command string
-        i2c.stop();
+    pdu.Enable();
+    pdu.SWRST();
+    pdu.init();
+    #define DELAY 20
+    
+    pdu.SetBrightness(128);
+
+    while(1)
+    {
+        pdu.SetPower(true);
+        fault = !fault;
+        pdu.SetFault(fault);
+        if (sw.GetMode() == 1)
+        {
+            pdu.SetAuto(true);
+            pdu.SetManual(false);
+        }
+        if (sw.GetMode() == 2)
+        {
+            pdu.SetAuto(false);
+            pdu.SetManual(true);
+        }
+        
+
+        pc.printf("GetMode = %x\r\n", sw.GetMode());
+        pc.printf("GetMove = %x\r\n", sw.GetMove());        
+        pc.printf("\r\n");
+        
+        for (int i = 0; i <= 10; i++)
+        {
+            pdu.SetBarGraph(i);
+            wait_ms(DELAY);
+        }
+        
+        for (int i = 10; i >= 0; i--)
+        {
+            pdu.SetBarGraph(i);
+            wait_ms(DELAY);
+        }
     }
 }
\ No newline at end of file
diff -r d58e47b3cc7e -r 1686fedb8e0a mbed.bld
--- a/mbed.bld	Tue Jun 28 18:29:49 2016 +0000
+++ b/mbed.bld	Fri Apr 07 16:12:08 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/6c34061e7c34
\ No newline at end of file
+https://mbed.org/users/mbed_official/code/mbed/builds/856d2700e60b
\ No newline at end of file