Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
u8g_dev_ht1632.c
00001 /* 00002 00003 u8g_dev_ht1632.c 00004 00005 1-Bit (BW) Driver for HT1632 controller 00006 00007 Universal 8bit Graphics Library 00008 00009 Copyright (c) 2013, olikraus@gmail.com 00010 All rights reserved. 00011 00012 Redistribution and use in source and binary forms, with or without modification, 00013 are permitted provided that the following conditions are met: 00014 00015 * Redistributions of source code must retain the above copyright notice, this list 00016 of conditions and the following disclaimer. 00017 00018 * Redistributions in binary form must reproduce the above copyright notice, this 00019 list of conditions and the following disclaimer in the documentation and/or other 00020 materials provided with the distribution. 00021 00022 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 00023 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 00024 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00025 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00026 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00027 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00028 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00029 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00030 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00032 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00033 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 00034 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 00036 00037 00038 U8G_PIN_NONE can be used as argument 00039 00040 uint8_t u8g_InitSPI(u8g_t *u8g, u8g_dev_t *dev, uint8_t sck, uint8_t mosi, uint8_t cs, uint8_t a0, uint8_t reset) 00041 { 00042 ... 00043 u8g->pin_list[U8G_PI_SCK] = sck; 00044 u8g->pin_list[U8G_PI_MOSI] = mosi; 00045 u8g->pin_list[U8G_PI_CS] = cs; 00046 u8g->pin_list[U8G_PI_A0] = a0; 00047 u8g->pin_list[U8G_PI_RESET] = reset; 00048 00049 mapping 00050 00051 #define DATA_PIN --> U8G_PI_MOSI 00052 #define WR_PIN --> U8G_PI_SCK 00053 #define CS_PIN --> U8G_PI_CS 00054 U8G_PI_A0 --> not used 00055 U8G_PI_RESET --> not used 00056 00057 Usage: 00058 00059 u8g_InitSPI(&u8g, &u8g_dev_ht1632_24x16, WR_PIN, DATA_IN, CS_PIN, U8G_PIN_NONE, U8G_PIN_NONE) 00060 00061 */ 00062 00063 #include "u8g.h" 00064 00065 #define WIDTH 24 00066 #define HEIGHT 16 00067 #define PAGE_HEIGHT 16 00068 00069 /* http://forum.arduino.cc/index.php?topic=168537.0 */ 00070 00071 #define HT1632_CMD_SYSDIS 0x00 // CMD= 0000-0000-x Turn off oscil 00072 #define HT1632_CMD_SYSON 0x01 // CMD= 0000-0001-x Enable system oscil 00073 #define HT1632_CMD_LEDOFF 0x02 // CMD= 0000-0010-x LED duty cycle gen off 00074 #define HT1632_CMD_LEDON 0x03 // CMD= 0000-0011-x LEDs ON 00075 #define HT1632_CMD_BLOFF 0x08 // CMD= 0000-1000-x Blink OFF 00076 #define HT1632_CMD_BLON 0x09 // CMD= 0000-1001-x Blink On 00077 #define HT1632_CMD_SLVMD 0x10 // CMD= 0001-00xx-x Slave Mode 00078 #define HT1632_CMD_MSTMD 0x14 // CMD= 0001-01xx-x Master Mode 00079 #define HT1632_CMD_RCCLK 0x18 // CMD= 0001-10xx-x Use on-chip clock 00080 #define HT1632_CMD_EXTCLK 0x1C // CMD= 0001-11xx-x Use external clock 00081 #define HT1632_CMD_COMS00 0x20 // CMD= 0010-ABxx-x commons options 00082 #define HT1632_CMD_COMS01 0x24 // CMD= 0010-ABxx-x commons options 00083 #define HT1632_CMD_COMS10 0x28 // CMD= 0010-ABxx-x commons options 00084 #define HT1632_CMD_COMS11 0x2C // P-MOS OUTPUT AND 16COMMON OPTION 00085 #define HT1632_CMD_PWM 0xA0 // CMD= 101x-PPPP-x PWM duty cycle 00086 00087 #define HT1632_ID_CMD 4 /* ID = 100 - Commands */ 00088 #define HT1632_ID_RD 6 /* ID = 110 - Read RAM */ 00089 #define HT1632_ID_WR 5 /* ID = 101 - Write RAM */ 00090 00091 #define HT1632_ID_LEN 3 // IDs are 3 bits 00092 #define HT1632_CMD_LEN 8 // CMDs are 8 bits 00093 #define HT1632_DATA_LEN 8 // Data are 4*2 bits 00094 #define HT1632_ADDR_LEN 7 // Address are 7 bits 00095 00096 #if defined(ARDUINO) 00097 00098 #if ARDUINO < 100 00099 #include <WProgram.h> 00100 #else 00101 #include <Arduino.h> 00102 #endif 00103 00104 //#define WR_PIN 3 00105 //#define DATA_PIN 2 00106 //#define CS_PIN 4 00107 00108 void ht1632_write_data_MSB(u8g_t *u8g, uint8_t cnt, uint8_t data, uint8_t extra) 00109 { 00110 int8_t i; 00111 uint8_t data_pin = u8g->pin_list[U8G_PI_MOSI]; 00112 uint8_t wr_pin = u8g->pin_list[U8G_PI_SCK]; 00113 00114 for(i = cnt - 1; i >= 0; i--) 00115 { 00116 if ((data >> i) & 1) 00117 { 00118 digitalWrite(data_pin, HIGH); 00119 } 00120 else 00121 { 00122 digitalWrite(data_pin, LOW); 00123 } 00124 00125 digitalWrite(wr_pin, LOW); 00126 u8g_MicroDelay(); 00127 digitalWrite(wr_pin, HIGH); 00128 u8g_MicroDelay(); 00129 } 00130 00131 // Send an extra bit 00132 if (extra) 00133 { 00134 digitalWrite(data_pin, HIGH); 00135 digitalWrite(wr_pin, LOW); 00136 u8g_MicroDelay(); 00137 digitalWrite(wr_pin, HIGH); 00138 u8g_MicroDelay(); 00139 } 00140 } 00141 00142 void ht1632_write_data(u8g_t *u8g, uint8_t cnt, uint8_t data) 00143 { 00144 uint8_t i; 00145 uint8_t data_pin = u8g->pin_list[U8G_PI_MOSI]; 00146 uint8_t wr_pin = u8g->pin_list[U8G_PI_SCK]; 00147 for (i = 0; i < cnt; i++) 00148 { 00149 00150 if ((data >> i) & 1) { 00151 digitalWrite(data_pin, HIGH); 00152 } 00153 else { 00154 digitalWrite(data_pin, LOW); 00155 } 00156 00157 digitalWrite(wr_pin, LOW); 00158 u8g_MicroDelay(); 00159 digitalWrite(wr_pin, HIGH); 00160 u8g_MicroDelay(); 00161 } 00162 } 00163 00164 00165 void ht1632_init(u8g_t *u8g) 00166 { 00167 //uint8_t i; 00168 uint8_t data_pin = u8g->pin_list[U8G_PI_MOSI]; 00169 uint8_t wr_pin = u8g->pin_list[U8G_PI_SCK]; 00170 uint8_t cs_pin = u8g->pin_list[U8G_PI_CS]; 00171 pinMode(data_pin, OUTPUT); 00172 pinMode(wr_pin, OUTPUT); 00173 pinMode(cs_pin, OUTPUT); 00174 00175 digitalWrite(data_pin, HIGH); 00176 digitalWrite(wr_pin, HIGH); 00177 digitalWrite(cs_pin, HIGH); 00178 00179 digitalWrite(cs_pin, LOW); 00180 /* init display once after startup */ 00181 ht1632_write_data_MSB(u8g, 3, HT1632_ID_CMD, false); // IDs are 3 bits 00182 ht1632_write_data_MSB(u8g, 8, HT1632_CMD_SYSDIS, true); // 8 bits 00183 ht1632_write_data_MSB(u8g, 8, HT1632_CMD_SYSON, true); // 8 bits 00184 ht1632_write_data_MSB(u8g, 8, HT1632_CMD_COMS11, true); // 8 bits 00185 ht1632_write_data_MSB(u8g, 8, HT1632_CMD_LEDON, true); // 8 bits 00186 ht1632_write_data_MSB(u8g, 8, HT1632_CMD_BLOFF, true); // 8 bits 00187 ht1632_write_data_MSB(u8g, 8, HT1632_CMD_PWM+15, true); // 8 bits 00188 digitalWrite(cs_pin, HIGH); 00189 00190 /* removed following (debug) code */ 00191 /* 00192 digitalWrite(cs_pin, LOW); 00193 ht1632_write_data_MSB(u8g, 3, HT1632_ID_WR, false); // Send "write to display" command 00194 ht1632_write_data_MSB(u8g, 7, 0, false); 00195 for(i = 0; i<48; ++i) 00196 { 00197 ht1632_write_data(u8g, 8, 0xFF); 00198 } 00199 digitalWrite(cs_pin, HIGH); 00200 */ 00201 } 00202 00203 /* 00204 page: 0=data contain lines 0..16, 1=data contain lines 16..32 (a 24x16 display will only have page 0) 00205 cnt: width of the display 00206 data: pointer to a buffer with 2*cnt bytes. 00207 */ 00208 void ht1632_transfer_data(u8g_t *u8g, uint8_t page, uint8_t cnt, uint8_t *data) 00209 { 00210 uint8_t addr; 00211 uint8_t cs_pin = u8g->pin_list[U8G_PI_CS]; 00212 /* send data to the ht1632 */ 00213 digitalWrite(cs_pin, LOW); 00214 ht1632_write_data_MSB(u8g, 3, HT1632_ID_WR, false); // Send "write to display" command 00215 ht1632_write_data_MSB(u8g, 7, page*2*cnt, false); 00216 00217 // Operating in progressive addressing mode 00218 for (addr = 0; addr < cnt; addr++) 00219 { 00220 ht1632_write_data(u8g, 8, data[addr]); 00221 ht1632_write_data(u8g, 8, data[addr+cnt]); 00222 } 00223 digitalWrite(cs_pin, HIGH); 00224 } 00225 00226 /* value is between 0...15 */ 00227 void ht1632_set_contrast(u8g_t *u8g, uint8_t value) 00228 { 00229 uint8_t cs_pin = u8g->pin_list[U8G_PI_CS]; 00230 digitalWrite(cs_pin, LOW); 00231 ht1632_write_data_MSB(u8g, 3, HT1632_ID_CMD, false); 00232 ht1632_write_data_MSB(u8g, 8, HT1632_CMD_PWM + value, false); 00233 digitalWrite(cs_pin, HIGH); 00234 } 00235 00236 #else 00237 void ht1632_init(u8g_t *u8g) 00238 { 00239 } 00240 00241 void ht1632_transfer_data(u8g_t *u8g, uint8_t page, uint8_t cnt, uint8_t *data) 00242 { 00243 } 00244 00245 void ht1632_set_contrast(u8g_t *u8g, uint8_t value) 00246 { 00247 } 00248 00249 #endif /* ARDUINO */ 00250 00251 00252 uint8_t u8g_dev_ht1632_24x16_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) 00253 { 00254 switch(msg) 00255 { 00256 case U8G_DEV_MSG_INIT: 00257 ht1632_init(u8g); 00258 break; 00259 case U8G_DEV_MSG_STOP: 00260 break; 00261 case U8G_DEV_MSG_PAGE_NEXT: 00262 { 00263 u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem); 00264 00265 /* current page: pb->p.page */ 00266 /* ptr to the buffer: pb->buf */ 00267 ht1632_transfer_data(u8g, pb->p.page, WIDTH, pb->buf); 00268 } 00269 break; 00270 case U8G_DEV_MSG_CONTRAST: 00271 /* values passed to SetContrast() are between 0 and 255, scale down to 0...15 */ 00272 ht1632_set_contrast(u8g, (*(uint8_t *)arg) >> 4); 00273 return 1; 00274 } 00275 return u8g_dev_pb16v1_base_fn(u8g, dev, msg, arg); 00276 } 00277 00278 uint8_t u8g_dev_ht1632_24x16_buf[WIDTH*2] U8G_NOCOMMON ; 00279 u8g_pb_t u8g_dev_ht1632_24x16_pb = { {16, HEIGHT, 0, 0, 0}, WIDTH, u8g_dev_ht1632_24x16_buf}; 00280 u8g_dev_t u8g_dev_ht1632_24x16 = { u8g_dev_ht1632_24x16_fn, &u8g_dev_ht1632_24x16_pb, u8g_com_null_fn }; 00281 00282
Generated on Tue Jul 12 2022 17:30:57 by
1.7.2