NXP PCF8576 Universal LCD driver for low multiplex rates + GH08172 LCD library
Dependents: PCF8576_GH08172_test
PCF8576.cpp@0:e62c6477b73b, 2016-03-28 (annotated)
- Committer:
- MACRUM
- Date:
- Mon Mar 28 12:40:44 2016 +0000
- Revision:
- 0:e62c6477b73b
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:e62c6477b73b | 1 | /* Copyright (c) 2016 ARM Ltd., MIT License |
MACRUM | 0:e62c6477b73b | 2 | * |
MACRUM | 0:e62c6477b73b | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
MACRUM | 0:e62c6477b73b | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
MACRUM | 0:e62c6477b73b | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
MACRUM | 0:e62c6477b73b | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
MACRUM | 0:e62c6477b73b | 7 | * Software is furnished to do so, subject to the following conditions: |
MACRUM | 0:e62c6477b73b | 8 | * |
MACRUM | 0:e62c6477b73b | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
MACRUM | 0:e62c6477b73b | 10 | * substantial portions of the Software. |
MACRUM | 0:e62c6477b73b | 11 | * |
MACRUM | 0:e62c6477b73b | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
MACRUM | 0:e62c6477b73b | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
MACRUM | 0:e62c6477b73b | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
MACRUM | 0:e62c6477b73b | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
MACRUM | 0:e62c6477b73b | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
MACRUM | 0:e62c6477b73b | 17 | * |
MACRUM | 0:e62c6477b73b | 18 | */ |
MACRUM | 0:e62c6477b73b | 19 | #include "PCF8576.h" |
MACRUM | 0:e62c6477b73b | 20 | |
MACRUM | 0:e62c6477b73b | 21 | PCF8576::PCF8576(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) |
MACRUM | 0:e62c6477b73b | 22 | { |
MACRUM | 0:e62c6477b73b | 23 | initialize(); |
MACRUM | 0:e62c6477b73b | 24 | } |
MACRUM | 0:e62c6477b73b | 25 | |
MACRUM | 0:e62c6477b73b | 26 | PCF8576::PCF8576(I2C &i2c_obj, int addr) : m_i2c(i2c_obj), m_addr(addr) |
MACRUM | 0:e62c6477b73b | 27 | { |
MACRUM | 0:e62c6477b73b | 28 | initialize(); |
MACRUM | 0:e62c6477b73b | 29 | } |
MACRUM | 0:e62c6477b73b | 30 | |
MACRUM | 0:e62c6477b73b | 31 | PCF8576::~PCF8576() |
MACRUM | 0:e62c6477b73b | 32 | { |
MACRUM | 0:e62c6477b73b | 33 | } |
MACRUM | 0:e62c6477b73b | 34 | |
MACRUM | 0:e62c6477b73b | 35 | void PCF8576::initialize() |
MACRUM | 0:e62c6477b73b | 36 | { |
MACRUM | 0:e62c6477b73b | 37 | m_i2c.frequency(200000); |
MACRUM | 0:e62c6477b73b | 38 | |
MACRUM | 0:e62c6477b73b | 39 | for(uint32_t i = 0; i < 14; i++) { |
MACRUM | 0:e62c6477b73b | 40 | m_lcd_buf[i] = 0; |
MACRUM | 0:e62c6477b73b | 41 | } |
MACRUM | 0:e62c6477b73b | 42 | |
MACRUM | 0:e62c6477b73b | 43 | m_C5_mask = m_C6_mask = 0; |
MACRUM | 0:e62c6477b73b | 44 | |
MACRUM | 0:e62c6477b73b | 45 | m_lcd_buf[0] = PCF8576_CMD_MODE_SET |
MACRUM | 0:e62c6477b73b | 46 | | (0x0 << 0) // 1:4 multiplex |
MACRUM | 0:e62c6477b73b | 47 | | (0 << 2) // LCD 1/3 bias |
MACRUM | 0:e62c6477b73b | 48 | | (1 << 3) // enable display |
MACRUM | 0:e62c6477b73b | 49 | | (1 << 7); // another command to follow |
MACRUM | 0:e62c6477b73b | 50 | |
MACRUM | 0:e62c6477b73b | 51 | m_lcd_buf[1] = PCF8576_CMD_BINK |
MACRUM | 0:e62c6477b73b | 52 | | (0x0 << 0) // blinking off |
MACRUM | 0:e62c6477b73b | 53 | | (0 << 2) // blink mode normal |
MACRUM | 0:e62c6477b73b | 54 | | (0 << 7); // last command |
MACRUM | 0:e62c6477b73b | 55 | |
MACRUM | 0:e62c6477b73b | 56 | m_i2c.write(PCF8576_DEFAULT_SLAVE_ADDRESS, m_lcd_buf, 14); |
MACRUM | 0:e62c6477b73b | 57 | |
MACRUM | 0:e62c6477b73b | 58 | } |
MACRUM | 0:e62c6477b73b | 59 | |
MACRUM | 0:e62c6477b73b | 60 | void PCF8576::icon(uint32_t count) |
MACRUM | 0:e62c6477b73b | 61 | { |
MACRUM | 0:e62c6477b73b | 62 | if (count & 0x08) { |
MACRUM | 0:e62c6477b73b | 63 | m_C5_mask |= 0x01; |
MACRUM | 0:e62c6477b73b | 64 | m_lcd_buf[11] |= 0x01; |
MACRUM | 0:e62c6477b73b | 65 | } else { |
MACRUM | 0:e62c6477b73b | 66 | m_C5_mask &= ~0x01; |
MACRUM | 0:e62c6477b73b | 67 | m_lcd_buf[11] &= ~0x01; |
MACRUM | 0:e62c6477b73b | 68 | } |
MACRUM | 0:e62c6477b73b | 69 | |
MACRUM | 0:e62c6477b73b | 70 | if (count & 0x04) { |
MACRUM | 0:e62c6477b73b | 71 | m_C5_mask |= 0x02; |
MACRUM | 0:e62c6477b73b | 72 | m_lcd_buf[11] |= 0x02; |
MACRUM | 0:e62c6477b73b | 73 | } else { |
MACRUM | 0:e62c6477b73b | 74 | m_C5_mask &= ~0x02; |
MACRUM | 0:e62c6477b73b | 75 | m_lcd_buf[11] &= ~0x02; |
MACRUM | 0:e62c6477b73b | 76 | } |
MACRUM | 0:e62c6477b73b | 77 | |
MACRUM | 0:e62c6477b73b | 78 | if (count & 0x02) { |
MACRUM | 0:e62c6477b73b | 79 | m_C6_mask |= 0x01; |
MACRUM | 0:e62c6477b73b | 80 | m_lcd_buf[13] |= 0x01; |
MACRUM | 0:e62c6477b73b | 81 | } else { |
MACRUM | 0:e62c6477b73b | 82 | m_C6_mask &= ~0x01; |
MACRUM | 0:e62c6477b73b | 83 | m_lcd_buf[13] &= ~0x01; |
MACRUM | 0:e62c6477b73b | 84 | } |
MACRUM | 0:e62c6477b73b | 85 | |
MACRUM | 0:e62c6477b73b | 86 | if (count & 0x01) { |
MACRUM | 0:e62c6477b73b | 87 | m_C6_mask |= 0x02; |
MACRUM | 0:e62c6477b73b | 88 | m_lcd_buf[13] |= 0x02; |
MACRUM | 0:e62c6477b73b | 89 | } else { |
MACRUM | 0:e62c6477b73b | 90 | m_C6_mask &= ~0x02; |
MACRUM | 0:e62c6477b73b | 91 | m_lcd_buf[13] &= ~0x02; |
MACRUM | 0:e62c6477b73b | 92 | } |
MACRUM | 0:e62c6477b73b | 93 | m_i2c.write(PCF8576_DEFAULT_SLAVE_ADDRESS, m_lcd_buf, 14); |
MACRUM | 0:e62c6477b73b | 94 | |
MACRUM | 0:e62c6477b73b | 95 | } |
MACRUM | 0:e62c6477b73b | 96 | |
MACRUM | 0:e62c6477b73b | 97 | |
MACRUM | 0:e62c6477b73b | 98 | void PCF8576::print(char *str) |
MACRUM | 0:e62c6477b73b | 99 | { |
MACRUM | 0:e62c6477b73b | 100 | uint32_t len = strlen(str); |
MACRUM | 0:e62c6477b73b | 101 | if (len == 0) |
MACRUM | 0:e62c6477b73b | 102 | return; |
MACRUM | 0:e62c6477b73b | 103 | if (len > 6) |
MACRUM | 0:e62c6477b73b | 104 | len = 6; |
MACRUM | 0:e62c6477b73b | 105 | |
MACRUM | 0:e62c6477b73b | 106 | m_lcd_buf[0] = PCF8576_CMD_DEVICE_SEL | (1 << 7); |
MACRUM | 0:e62c6477b73b | 107 | m_lcd_buf[1] = PCF8576_CMD_LOAD_DATA; |
MACRUM | 0:e62c6477b73b | 108 | |
MACRUM | 0:e62c6477b73b | 109 | for(uint32_t i = 0; i < len; i++) { |
MACRUM | 0:e62c6477b73b | 110 | m_lcd_buf[(i*2) + 2] = (char)(FontMatrix[(*str - ' ')] & 0xFF); |
MACRUM | 0:e62c6477b73b | 111 | m_lcd_buf[(i*2) + 3] = (char)(FontMatrix[(*str - ' ')] >> 8); |
MACRUM | 0:e62c6477b73b | 112 | str++; |
MACRUM | 0:e62c6477b73b | 113 | } |
MACRUM | 0:e62c6477b73b | 114 | |
MACRUM | 0:e62c6477b73b | 115 | m_lcd_buf[11] |= m_C5_mask; |
MACRUM | 0:e62c6477b73b | 116 | m_lcd_buf[13] |= m_C6_mask; |
MACRUM | 0:e62c6477b73b | 117 | |
MACRUM | 0:e62c6477b73b | 118 | m_i2c.write(PCF8576_DEFAULT_SLAVE_ADDRESS, m_lcd_buf, 2 + (len*2)); |
MACRUM | 0:e62c6477b73b | 119 | } |