NXP PCF8576 Universal LCD driver for low multiplex rates + GH08172 LCD library

Dependents:   PCF8576_GH08172_test

Files at this revision

API Documentation at this revision

Comitter:
MACRUM
Date:
Mon Mar 28 12:40:44 2016 +0000
Child:
1:427ffdda29a3
Commit message:
Initial commit

Changed in this revision

PCF8576.cpp Show annotated file Show diff for this revision Revisions of this file
PCF8576.h Show annotated file Show diff for this revision Revisions of this file
char_pattern.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCF8576.cpp	Mon Mar 28 12:40:44 2016 +0000
@@ -0,0 +1,119 @@
+/* Copyright (c) 2016 ARM Ltd., MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*
+*/
+#include "PCF8576.h"
+
+PCF8576::PCF8576(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr)
+{
+    initialize();
+}
+ 
+PCF8576::PCF8576(I2C &i2c_obj, int addr) : m_i2c(i2c_obj), m_addr(addr)
+{
+    initialize();
+}
+ 
+PCF8576::~PCF8576()
+{
+}
+
+void PCF8576::initialize()
+{
+    m_i2c.frequency(200000);
+
+    for(uint32_t i = 0; i < 14; i++) {
+        m_lcd_buf[i] = 0;
+    }
+    
+    m_C5_mask = m_C6_mask = 0;
+
+    m_lcd_buf[0]      =  PCF8576_CMD_MODE_SET
+                       | (0x0 << 0)          // 1:4 multiplex
+                       | (0   << 2)          // LCD 1/3 bias
+                       | (1   << 3)          // enable display
+                       | (1   << 7);         // another command to follow
+
+    m_lcd_buf[1]      =  PCF8576_CMD_BINK
+                       | (0x0 << 0)          // blinking off
+                       | (0   << 2)          // blink mode normal
+                       | (0   << 7);         // last command
+
+    m_i2c.write(PCF8576_DEFAULT_SLAVE_ADDRESS, m_lcd_buf, 14);
+
+}
+
+void PCF8576::icon(uint32_t count)
+{
+    if (count & 0x08) {
+        m_C5_mask     |= 0x01;
+        m_lcd_buf[11] |= 0x01;
+    } else {
+        m_C5_mask     &= ~0x01;
+        m_lcd_buf[11] &= ~0x01;
+    }
+
+    if (count & 0x04) {
+        m_C5_mask     |= 0x02;
+        m_lcd_buf[11] |= 0x02;
+    } else {
+        m_C5_mask     &= ~0x02;
+        m_lcd_buf[11] &= ~0x02;
+    }
+
+    if (count & 0x02) {
+        m_C6_mask     |= 0x01;
+        m_lcd_buf[13] |= 0x01;
+    } else {
+        m_C6_mask     &= ~0x01;
+        m_lcd_buf[13] &= ~0x01;
+    }
+
+    if (count & 0x01) {
+        m_C6_mask     |= 0x02;
+        m_lcd_buf[13] |= 0x02;
+    } else {
+        m_C6_mask     &= ~0x02;
+        m_lcd_buf[13] &= ~0x02;
+    }
+    m_i2c.write(PCF8576_DEFAULT_SLAVE_ADDRESS, m_lcd_buf, 14);
+
+}
+
+
+void PCF8576::print(char *str)
+{
+    uint32_t len = strlen(str);
+    if (len == 0)
+        return;
+    if (len > 6)
+        len = 6;
+
+    m_lcd_buf[0] = PCF8576_CMD_DEVICE_SEL | (1 << 7);
+    m_lcd_buf[1] = PCF8576_CMD_LOAD_DATA;
+
+    for(uint32_t i = 0; i < len; i++) {
+        m_lcd_buf[(i*2) + 2] = (char)(FontMatrix[(*str - ' ')] & 0xFF);
+        m_lcd_buf[(i*2) + 3] = (char)(FontMatrix[(*str - ' ')] >> 8);
+        str++;
+    }
+
+    m_lcd_buf[11] |= m_C5_mask;
+    m_lcd_buf[13] |= m_C6_mask;
+
+    m_i2c.write(PCF8576_DEFAULT_SLAVE_ADDRESS, m_lcd_buf, 2 + (len*2));
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCF8576.h	Mon Mar 28 12:40:44 2016 +0000
@@ -0,0 +1,112 @@
+/* Copyright (c) 2016 ARM Ltd., MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*
+*
+*  PCF8576 Universal LCD driver library
+*
+*  @author  Toyomasa Watarai
+*  @version 1.0
+*  @date    28-March-2016
+*
+*  Library for "PCF8576 Universal LCD driver for low multiplex rates"
+*
+*/
+ 
+#ifndef PCF8576_H
+#define PCF8576_H
+
+#include "mbed.h"
+#include "char_pattern.h"
+
+#define PCF8576_CMD_MODE_SET    0x40
+#define PCF8576_CMD_LOAD_DATA   0x00
+#define PCF8576_CMD_DEVICE_SEL  0x60
+#define PCF8576_CMD_BANK_SEL    0x78
+#define PCF8576_CMD_BINK        0x70
+
+#define PCF8576_DEFAULT_SLAVE_ADDRESS 0x70
+
+/**
+* PCF8576 Universal LCD driver library example
+*
+* @code
+* #include "PCF8576.h"
+* PCF8576 lcd(D14, D15);
+* 
+* int main() {
+*     lcd.print("mbed");
+*     while(1) {
+*     }
+* }
+* @endcode
+*/
+class PCF8576 {
+public:
+    /**
+    * PCF8576 constructor
+    *
+    * @param sda SDA pin
+    * @param sdl SCL pin
+    * @param addr slave address of the I2C peripheral (default: 0x70)
+    */
+    PCF8576(PinName sda, PinName scl, int addr = PCF8576_DEFAULT_SLAVE_ADDRESS);
+ 
+    /**
+     * Create a PCF8576 instance which is connected to specified I2C pins
+     * with specified address
+     *
+     * @param i2c_obj I2C object (instance)
+     * @param addr slave address of the I2C-bus peripheral (default: 0x70)
+     */
+    PCF8576(I2C &i2c_obj, int addr = PCF8576_DEFAULT_SLAVE_ADDRESS);
+ 
+    /**
+    * PCF8576 destructor
+    */
+    ~PCF8576();
+ 
+    /** Initializa PCF8576 device
+     *
+     *  Configure LCD setting
+     *
+     */
+    void initialize(void);
+
+    /**
+     * Print string
+     *
+     * @param str poiner to print string
+     */
+    void print(char *str);
+    
+    /**
+     * Print icon bar
+     *
+     * @param level-bar pictogram icon from 0 (all off) to 0xF (all on)
+     */
+    void icon(uint32_t icon);
+
+private:
+    I2C  m_i2c;
+    int  m_addr;
+    char m_lcd_buf[14];
+    char m_C5_mask;
+    char m_C6_mask;
+
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/char_pattern.h	Mon Mar 28 12:40:44 2016 +0000
@@ -0,0 +1,139 @@
+/***************************************************************************
+*  connections PCF8576 to GH08172:
+*
+*             a
+*         ---------
+*        | \  j  / |
+*      f |  h | k  | b   *i
+*        |   \|/   |
+*         -g-- --m-
+*        |   /|\   |
+*      e |  q | n  | c   *i
+*        | /  p  \ |
+*         ---------  *l
+*             d
+*
+**   definition of segments in 16 bits:
+*   15  14  13  12 | 11  10  9   8  |  7   6   5   4  |  3   2   1   0
+*   d   e   n   p  | c   m   l   i  |  f   g   h   q  |  a   b   j   k
+*
+***************************************************************************/
+// segment bit masks
+#define SEGK    0x0001u
+#define SEGJ    0x0002u
+#define SEGB    0x0004u
+#define SEGA    0x0008u
+#define SEGQ    0x0010u
+#define SEGH    0x0020u
+#define SEGG    0x0040u
+#define SEGF    0x0080u
+#define SEGI    0x0100u
+#define SEGL    0x0200u
+#define SEGM    0x0400u
+#define SEGC    0x0800u
+#define SEGP    0x1000u
+#define SEGN    0x2000u
+#define SEGE    0x4000u
+#define SEGD    0x8000u
+
+const uint16_t FontMatrix[] =
+{
+    0,                                                      // ' ' (space)
+    SEGF | SEGE,                                            // ! (ugly)
+    SEGF | SEGB,                                            // "
+    SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG |
+    SEGH | SEGJ | SEGK | SEGM | SEGN | SEGP | SEGQ,         // #
+    SEGA | SEGF | SEGG | SEGM | SEGC | SEGD | SEGJ | SEGP,  // $
+    SEGF | SEGK | SEGQ | SEGC,                              // %
+    SEGA | SEGK | SEGG | SEGE | SEGD | SEGN | SEGH,         // &
+    SEGJ,                                                   // '
+    SEGK | SEGN,                                            // ( (== '<')
+    SEGH | SEGQ,                                            // ) (== '>')
+    SEGH | SEGJ | SEGK | SEGG | SEGM | SEGQ | SEGP | SEGN,  // *
+    SEGJ | SEGG | SEGM | SEGP,                              // +
+    SEGP,                                                   // ,
+    SEGG | SEGM,                                            // -
+    SEGD,                                                   // . (== '_')
+    SEGK | SEGQ,                                            // /
+    SEGA | SEGB | SEGC | SEGD | SEGE | SEGF,                // 0
+    SEGC | SEGB,                                            // 1
+    SEGA | SEGB | SEGM | SEGG | SEGE | SEGD,                // 2
+    SEGA | SEGB | SEGM | SEGG | SEGC | SEGD,                // 3
+    SEGF | SEGG | SEGM | SEGB | SEGC,                       // 4
+    SEGA | SEGF | SEGG | SEGM | SEGC | SEGD,                // 5
+    SEGA | SEGF | SEGE | SEGD | SEGC | SEGM | SEGG,         // 6
+    SEGA | SEGB | SEGC,                                     // 7
+    SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG | SEGM,  // 8
+    SEGA | SEGF | SEGG | SEGM | SEGB | SEGC | SEGD,         // 9 (== 'g')
+    SEGJ | SEGP,                                            // : (ugly)
+    SEGJ | SEGQ,                                            // ; (ugly)
+    SEGK | SEGN,                                            // < (== '(')
+    SEGG | SEGM | SEGD,                                     // =
+    SEGH | SEGQ,                                            // > (== ')')
+    SEGA | SEGB | SEGM | SEGP,                              // ?
+    SEGA | SEGB | SEGD | SEGE | SEGF | SEGJ | SEGM,         // @
+    SEGE | SEGF | SEGA | SEGB | SEGC | SEGG | SEGM,         // A
+    SEGA | SEGB | SEGC | SEGD | SEGJ | SEGP | SEGM,         // B
+    SEGA | SEGF | SEGE | SEGD,                              // C
+    SEGA | SEGB | SEGC | SEGD | SEGJ | SEGP,                // D
+    SEGA | SEGF | SEGE | SEGD | SEGG | SEGM,                // E
+    SEGA | SEGF | SEGE | SEGG | SEGM,                       // F
+    SEGA | SEGF | SEGE | SEGD | SEGC | SEGM,                // G
+    SEGF | SEGE | SEGG | SEGM | SEGB | SEGC,                // H
+    SEGA | SEGJ | SEGP | SEGD,                              // I
+    SEGB | SEGC | SEGD | SEGE,                              // J
+    SEGF | SEGE | SEGG | SEGK | SEGN,                       // K
+    SEGF | SEGE | SEGD,                                     // L
+    SEGE | SEGF | SEGH | SEGK | SEGB | SEGC,                // M
+    SEGE | SEGF | SEGH | SEGN | SEGC | SEGB,                // N
+    SEGA | SEGB | SEGC | SEGD | SEGE | SEGF,                // O
+    SEGE | SEGF | SEGA | SEGB | SEGG | SEGM,                // P
+    SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGN,         // Q
+    SEGE | SEGF | SEGA | SEGB | SEGG | SEGM | SEGN,         // R
+    SEGA | SEGF | SEGG | SEGM | SEGC | SEGD,                // S
+    SEGA | SEGJ | SEGP,                                     // T
+    SEGF | SEGE | SEGD | SEGC | SEGB,                       // U
+    SEGF | SEGE | SEGQ | SEGK,                              // V
+    SEGF | SEGE | SEGQ | SEGN | SEGC | SEGB,                // W
+    SEGH | SEGK | SEGQ | SEGN,                              // X
+    SEGF | SEGG | SEGM | SEGB | SEGP,                       // Y
+    SEGA | SEGK | SEGQ | SEGD,                              // Z
+    SEGA | SEGF | SEGE | SEGD,                              // [
+    SEGH | SEGN,                                            // backslash
+    SEGA | SEGB | SEGC | SEGD,                              // ]
+    SEGF | SEGH,                                            // ^
+    SEGD,                                                   // _ (== '.')
+    SEGH,                                                   // `
+    SEGG | SEGM | SEGE | SEGD | SEGN,                       // a
+    SEGF | SEGE | SEGD | SEGC | SEGG | SEGM,                // b
+    SEGG | SEGM | SEGE | SEGD,                              // c
+    SEGG | SEGM | SEGE | SEGD | SEGC | SEGB,                // d
+    SEGQ | SEGG | SEGE | SEGD,                              // e
+    SEGK | SEGG | SEGM | SEGP,                              // f
+    SEGA | SEGF | SEGG | SEGM | SEGB | SEGC | SEGD,         // g (== '9')
+    SEGF | SEGE | SEGG | SEGM | SEGC,                       // h
+    SEGC,                                                   // i
+    SEGB | SEGC | SEGN,                                     // j
+    SEGJ | SEGP | SEGM | SEGN,                              // k
+    SEGB | SEGC,                                            // l
+    SEGE | SEGG | SEGM | SEGC | SEGP,                       // m
+    SEGE | SEGG | SEGM | SEGC,                              // n
+    SEGE | SEGG | SEGM | SEGC | SEGD,                       // o
+    SEGF | SEGE | SEGH | SEGG,                              // p
+    SEGB | SEGC | SEGM | SEGK,                              // q
+    SEGE | SEGG | SEGM,                                     // r
+    SEGM | SEGN | SEGD,                                     // s
+    SEGJ | SEGP | SEGG | SEGM,                              // t
+    SEGE | SEGD | SEGC,                                     // u
+    SEGE | SEGQ,                                            // v
+    SEGE | SEGQ | SEGN | SEGC,                              // w
+    SEGH | SEGK | SEGN | SEGQ,                              // x
+    SEGH | SEGK | SEGP,                                     // y
+    SEGG | SEGQ | SEGD,                                     // z
+    SEGK | SEGG | SEGN,                                     // {
+    SEGJ | SEGP,                                            // |
+    SEGH | SEGM | SEGQ,                                     // }
+    SEGF | SEGH | SEGJ,                                     // ~
+    SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG |
+    SEGH | SEGJ | SEGK | SEGM | SEGN | SEGP | SEGQ          // DEL (== '#')
+};