Advertisement
Guest User

Untitled

a guest
May 30th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.35 KB | None | 0 0
  1. /*
  2.  
  3.   u8g_dev_ht1632_26x16.c
  4.  
  5.   1-Bit (BW) Driver for HT1632 controller
  6.  
  7.   Universal 8bit Graphics Library
  8.  
  9.   Copyright (c) 2013, olikraus@gmail.com
  10.   All rights reserved.
  11.  
  12.   Redistribution and use in source and binary forms, with or without modification,
  13.   are permitted provided that the following conditions are met:
  14.  
  15.   * Redistributions of source code must retain the above copyright notice, this list
  16.     of conditions and the following disclaimer.
  17.    
  18.   * Redistributions in binary form must reproduce the above copyright notice, this
  19.     list of conditions and the following disclaimer in the documentation and/or other
  20.     materials provided with the distribution.
  21.  
  22.   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  23.   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  24.   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25.   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26.   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27.   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28.   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  29.   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30.   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31.   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  32.   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33.   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  34.   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.  
  36. */
  37.  
  38. #include "u8g.h"
  39.  
  40. #define WIDTH 24
  41. #define HEIGHT 16
  42. #define PAGE_HEIGHT 8
  43.  
  44. /* http://forum.arduino.cc/index.php?topic=168537.0 */
  45.  
  46. #define HT1632_CMD_SYSDIS   0x00    // CMD= 0000-0000-x Turn off oscil
  47. #define HT1632_CMD_SYSON    0x01    // CMD= 0000-0001-x Enable system oscil
  48. #define HT1632_CMD_LEDOFF   0x02    // CMD= 0000-0010-x LED duty cycle gen off
  49. #define HT1632_CMD_LEDON    0x03    // CMD= 0000-0011-x LEDs ON
  50. #define HT1632_CMD_BLOFF    0x08    // CMD= 0000-1000-x Blink OFF
  51. #define HT1632_CMD_BLON     0x09    // CMD= 0000-1001-x Blink On
  52. #define HT1632_CMD_SLVMD    0x10    // CMD= 0001-00xx-x Slave Mode
  53. #define HT1632_CMD_MSTMD    0x14    // CMD= 0001-01xx-x Master Mode
  54. #define HT1632_CMD_RCCLK    0x18    // CMD= 0001-10xx-x Use on-chip clock
  55. #define HT1632_CMD_EXTCLK   0x1C    // CMD= 0001-11xx-x Use external clock
  56. #define HT1632_CMD_COMS00   0x20    // CMD= 0010-ABxx-x commons options
  57. #define HT1632_CMD_COMS01   0x24    // CMD= 0010-ABxx-x commons options
  58. #define HT1632_CMD_COMS10   0x28    // CMD= 0010-ABxx-x commons options
  59. #define HT1632_CMD_COMS11   0x2C    // P-MOS OUTPUT AND 16COMMON OPTION
  60. #define HT1632_CMD_PWM      0xA0    // CMD= 101x-PPPP-x PWM duty cycle
  61.  
  62. #define HT1632_ID_CMD   4   /* ID = 100 - Commands */
  63. #define HT1632_ID_RD    6   /* ID = 110 - Read RAM */
  64. #define HT1632_ID_WR    5   /* ID = 101 - Write RAM */
  65.  
  66. #define HT1632_ID_LEN       3       // IDs are 3 bits
  67. #define HT1632_CMD_LEN      8       // CMDs are 8 bits
  68. #define HT1632_DATA_LEN     8       // Data are 4*2 bits
  69. #define HT1632_ADDR_LEN     7       // Address are 7 bits
  70.  
  71. #if defined(ARDUINO)
  72.  
  73. #if ARDUINO < 100
  74. #include <WProgram.h>
  75. #else
  76. #include <Arduino.h>
  77. #endif
  78.  
  79. #define DATA_PIN 2
  80. #define WR_PIN 3
  81. #define CS_PIN 4
  82.  
  83. void ht1632_write_data_MSB(uint8_t cnt, uint8_t data, uint8_t extra)
  84. {
  85.   int8_t i;
  86.  
  87.   for(i = cnt - 1; i >= 0; i--)
  88.   {
  89.     digitalWrite(WR_PIN, LOW);
  90.     if ((data >> i) & 1)
  91.     {  
  92.       digitalWrite(DATA_PIN, HIGH);
  93.     }
  94.     else
  95.     {
  96.       digitalWrite(DATA_PIN, LOW);
  97.     }
  98.  
  99.     digitalWrite(WR_PIN, HIGH);
  100.   }
  101.  
  102.   // Send an extra bit
  103.   if (extra)
  104.   {
  105.     digitalWrite(WR_PIN, LOW);
  106.     digitalWrite(DATA_PIN, HIGH);
  107.     digitalWrite(WR_PIN, HIGH);
  108.   }
  109. }
  110.  
  111. void ht1632_write_data(uint8_t cnt, uint8_t data)
  112. {
  113.   uint8_t i;
  114.   for (i = 0; i < cnt; i++)
  115.   {
  116.     digitalWrite(WR_PIN, LOW);
  117.  
  118.     if ((data >> i) & 1) {
  119.       digitalWrite(DATA_PIN, HIGH);
  120.     }
  121.     else {
  122.       digitalWrite(DATA_PIN, LOW);
  123.     }
  124.  
  125.     digitalWrite(WR_PIN, HIGH);
  126.   }
  127. }
  128.  
  129.  
  130. void ht1632_init(void)
  131. {
  132.   pinMode(DATA_PIN, OUTPUT);
  133.   pinMode(WR_PIN, OUTPUT);
  134.   pinMode(CS_PIN, OUTPUT);
  135.  
  136.   digitalWrite(DATA_PIN, HIGH);
  137.   digitalWrite(WR_PIN, HIGH);
  138.   digitalWrite(CS_PIN, HIGH);
  139.  
  140.   digitalWrite(CS_PIN, LOW);
  141.   /* init display once after startup */
  142.   ht1632_write_data_MSB(3, HT1632_ID_CMD, false); // IDs are 3 bits
  143.   ht1632_write_data_MSB(8, HT1632_CMD_SYSDIS, true); // 8 bits
  144.   ht1632_write_data_MSB(8, HT1632_CMD_SYSON, true); // 8 bits
  145.   ht1632_write_data_MSB(8, HT1632_CMD_COMS11, true); // 8 bits
  146.   ht1632_write_data_MSB(8, HT1632_CMD_LEDON, true); // 8 bits
  147.   ht1632_write_data_MSB(8, HT1632_CMD_BLOFF, true); // 8 bits
  148.   ht1632_write_data_MSB(8, HT1632_CMD_PWM+15, true); // 8 bits  
  149.   digitalWrite(CS_PIN, HIGH);
  150.  
  151.   digitalWrite(CS_PIN, LOW);
  152.   ht1632_write_data_MSB(3, HT1632_ID_WR, false); // Send "write to display" command
  153.   ht1632_write_data_MSB(7, 0, false);
  154.   uint8_t i;
  155.   for(i = 0; i<48; ++i)
  156.   {
  157.     ht1632_write_data(8, 0xFF);
  158.   }
  159.   digitalWrite(CS_PIN, HIGH);
  160. }
  161.  
  162. /*
  163.   page: 0=data contain lines 0..7, 1=data contain lines 8..16
  164.   cnt: number of bytes in the buffer
  165.   data: pointer to a buffer with "cnt" bytes.
  166. */
  167. void ht1632_transfer_data(uint8_t page, uint8_t cnt, uint8_t *data)
  168. {
  169.   uint8_t addr;
  170.   /* send data to the ht1632 */
  171.   digitalWrite(CS_PIN, LOW);
  172.   ht1632_write_data_MSB(3, HT1632_ID_WR, false); // Send "write to display" command
  173.   ht1632_write_data_MSB(7, page*8, false); // Oliver: Not sure if this calculation is correct
  174.  
  175.   // Operating in progressive addressing mode
  176.   for (addr = 0; addr < cnt; addr++)
  177.   {
  178.     ht1632_write_data(8, data[addr]);  
  179.   }  
  180.   digitalWrite(CS_PIN, HIGH);
  181. }
  182.  
  183. #else
  184. void ht1632_init(void)
  185. {
  186. }
  187.  
  188. void ht1632_transfer_data(uint8_t page, uint8_t cnt, uint8_t *data)
  189. {
  190. }
  191.  
  192. #endif /* ARDUINO */
  193.  
  194.  
  195. uint8_t u8g_dev_ht1632_26x16_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  196. {
  197.   switch(msg)
  198.   {
  199.     case U8G_DEV_MSG_INIT:
  200.         ht1632_init();
  201.       break;
  202.     case U8G_DEV_MSG_STOP:
  203.       break;
  204.     case U8G_DEV_MSG_PAGE_NEXT:
  205.       {
  206.         u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  207.        
  208.     /* current page: pb->p.page */
  209.     /* ptr to the buffer: pb->buf */
  210.     ht1632_transfer_data(pb->p.page, WIDTH, pb->buf);
  211.       }
  212.       break;
  213.     case U8G_DEV_MSG_CONTRAST:
  214.       return 1;
  215.   }
  216.   return u8g_dev_pb8v1_base_fn(u8g, dev, msg, arg);
  217. }
  218.  
  219. U8G_PB_DEV(u8g_dev_ht1632_26x16 , WIDTH, HEIGHT, PAGE_HEIGHT, u8g_dev_ht1632_26x16_fn, u8g_com_null_fn);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement