7-Segment Display Driver class, via an 8-bit shift register (such as the 74HC595)

Committer:
paul80nd
Date:
Sun May 01 17:34:00 2011 +0000
Revision:
1:e55f543bc06b
Parent:
0:1832d0ed8da8
Gettin\ me 7 and 8\s mixed up

Who changed what in which revision?

UserRevisionLine numberNew contents of line
paul80nd 0:1832d0ed8da8 1 /* mbed 7-Segment Display Driver Library (via an 8bit Shift Register)
paul80nd 0:1832d0ed8da8 2 * Copyright (c) 2011 Paul Law
paul80nd 0:1832d0ed8da8 3 *
paul80nd 0:1832d0ed8da8 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
paul80nd 0:1832d0ed8da8 5 * of this software and associated documentation files (the "Software"), to deal
paul80nd 0:1832d0ed8da8 6 * in the Software without restriction, including without limitation the rights
paul80nd 0:1832d0ed8da8 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
paul80nd 0:1832d0ed8da8 8 * copies of the Software, and to permit persons to whom the Software is
paul80nd 0:1832d0ed8da8 9 * furnished to do so, subject to the following conditions:
paul80nd 0:1832d0ed8da8 10 *
paul80nd 0:1832d0ed8da8 11 * The above copyright notice and this permission notice shall be included in
paul80nd 0:1832d0ed8da8 12 * all copies or substantial portions of the Software.
paul80nd 0:1832d0ed8da8 13 *
paul80nd 0:1832d0ed8da8 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
paul80nd 0:1832d0ed8da8 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
paul80nd 0:1832d0ed8da8 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
paul80nd 0:1832d0ed8da8 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
paul80nd 0:1832d0ed8da8 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
paul80nd 0:1832d0ed8da8 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
paul80nd 0:1832d0ed8da8 20 * THE SOFTWARE.
paul80nd 0:1832d0ed8da8 21 */
paul80nd 0:1832d0ed8da8 22
paul80nd 0:1832d0ed8da8 23 #ifndef LIB_SSEGSRDRIVER_H
paul80nd 0:1832d0ed8da8 24 #define LIB_SSEGSRDRIVER_H
paul80nd 0:1832d0ed8da8 25
paul80nd 0:1832d0ed8da8 26 #include "mbed.h"
paul80nd 0:1832d0ed8da8 27
paul80nd 0:1832d0ed8da8 28 #define SSegSRDriver_COMN_ANODE 0
paul80nd 0:1832d0ed8da8 29 #define SSegSRDriver_COMN_CATHODE 1
paul80nd 0:1832d0ed8da8 30
paul80nd 0:1832d0ed8da8 31 //Char defs: 0123456789AbCdEF
paul80nd 0:1832d0ed8da8 32 const unsigned char SSegSRDriver_chardefs[16] = {0x3F, 0x6, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x7, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 };
paul80nd 0:1832d0ed8da8 33
paul80nd 1:e55f543bc06b 34 /** 7-Segment Display Driver class, via a 8-bit shift register (such as the 74HC595)
paul80nd 0:1832d0ed8da8 35 *
paul80nd 0:1832d0ed8da8 36 * Display should be hooked up to shift register as follows:
paul80nd 0:1832d0ed8da8 37 * Q0 : Decimal Point, Q1-Q7 : Segments a-g
paul80nd 0:1832d0ed8da8 38 *
paul80nd 0:1832d0ed8da8 39 * Example:
paul80nd 0:1832d0ed8da8 40 * @code
paul80nd 0:1832d0ed8da8 41 * #include "mbed.h"
paul80nd 0:1832d0ed8da8 42 * #include "7SegSRDriver.h"
paul80nd 0:1832d0ed8da8 43 *
paul80nd 0:1832d0ed8da8 44 * SSegSRDriver display(p27,p25,p26, SSegSRDriver_COMN_ANODE);
paul80nd 0:1832d0ed8da8 45 *
paul80nd 0:1832d0ed8da8 46 * int main() {
paul80nd 0:1832d0ed8da8 47 * while (1) {
paul80nd 0:1832d0ed8da8 48 * // Show the chars 0-9 then A-F, flashing the decimal point on and off
paul80nd 0:1832d0ed8da8 49 * for (int i=0; i<16; i++) {
paul80nd 0:1832d0ed8da8 50 * display.write(i,i%2 == 0);
paul80nd 0:1832d0ed8da8 51 * wait(1);
paul80nd 0:1832d0ed8da8 52 * }
paul80nd 0:1832d0ed8da8 53 * }
paul80nd 0:1832d0ed8da8 54 * }
paul80nd 0:1832d0ed8da8 55 * @endcode
paul80nd 0:1832d0ed8da8 56 */
paul80nd 0:1832d0ed8da8 57 class SSegSRDriver {
paul80nd 0:1832d0ed8da8 58
paul80nd 0:1832d0ed8da8 59 public:
paul80nd 0:1832d0ed8da8 60
paul80nd 0:1832d0ed8da8 61 /** Create a 7-Segment Display Driver object connected to a 8-bit shift register on the given DigitalOut pins
paul80nd 0:1832d0ed8da8 62 *
paul80nd 0:1832d0ed8da8 63 * @param srData Shift Register Data pin (DigitalOut)
paul80nd 0:1832d0ed8da8 64 * @param srClock Shift Register Clock pin (DigitalOut)
paul80nd 0:1832d0ed8da8 65 * @param srLatch Shift Register Latch pin (DigitalOut)
paul80nd 0:1832d0ed8da8 66 * @param disp_type Display Type: Common Anode/Cathode (SSegSRDriver_COMN_ANODE (0) or SSegSRDriver_COMN_CATHODE (1))
paul80nd 0:1832d0ed8da8 67 */
paul80nd 0:1832d0ed8da8 68 SSegSRDriver(PinName srData, PinName srClock, PinName srLatch, bool disp_type);
paul80nd 0:1832d0ed8da8 69
paul80nd 0:1832d0ed8da8 70 /** Change the type of 7-Segment Display
paul80nd 0:1832d0ed8da8 71 *
paul80nd 0:1832d0ed8da8 72 * @param disp_type Display Type: Common Anode/Cathode (SSegSRDriver_COMN_ANODE (0) or SSegSRDriver_COMN_CATHODE (1))
paul80nd 0:1832d0ed8da8 73 */
paul80nd 0:1832d0ed8da8 74 void set_type(bool disp_type);
paul80nd 0:1832d0ed8da8 75
paul80nd 0:1832d0ed8da8 76 /** Sets the currently shown digit on the display
paul80nd 0:1832d0ed8da8 77 *
paul80nd 0:1832d0ed8da8 78 * @param number The digit to display (0-15) to show numbers 0-9 and letters A-F
paul80nd 0:1832d0ed8da8 79 */
paul80nd 0:1832d0ed8da8 80 void write(unsigned char number);
paul80nd 0:1832d0ed8da8 81
paul80nd 0:1832d0ed8da8 82 /** Sets the currently shown digit on the display along with the decimal place
paul80nd 0:1832d0ed8da8 83 *
paul80nd 0:1832d0ed8da8 84 * @param number The digit to display (0-15) to show numbers 0-9 and letters A-F
paul80nd 0:1832d0ed8da8 85 * @param dp If the decimal place should be lit (0:Off, 1:On)
paul80nd 0:1832d0ed8da8 86 */
paul80nd 0:1832d0ed8da8 87 void write(unsigned char number, bool dp);
paul80nd 0:1832d0ed8da8 88
paul80nd 0:1832d0ed8da8 89 /** Sets the segments of the display directly
paul80nd 0:1832d0ed8da8 90 * Segments are lit by binary flags where LSB is the decimal point, then segments a-g up to the MSB
paul80nd 0:1832d0ed8da8 91 * e.g. to light segments e, f and the decimal place = 01100001
paul80nd 0:1832d0ed8da8 92 *
paul80nd 0:1832d0ed8da8 93 * @param bValue The segments to light
paul80nd 0:1832d0ed8da8 94 */
paul80nd 0:1832d0ed8da8 95 void write_raw(unsigned char number); // Write directly to the display - Q0/Q1-Q7
paul80nd 0:1832d0ed8da8 96
paul80nd 0:1832d0ed8da8 97 /** Turn all segments of the display off
paul80nd 0:1832d0ed8da8 98 */
paul80nd 0:1832d0ed8da8 99 void clear(); // Clears the display
paul80nd 0:1832d0ed8da8 100
paul80nd 0:1832d0ed8da8 101 private:
paul80nd 0:1832d0ed8da8 102
paul80nd 0:1832d0ed8da8 103 DigitalOut _srData;
paul80nd 0:1832d0ed8da8 104 DigitalOut _srClock;
paul80nd 0:1832d0ed8da8 105 DigitalOut _srLatch;
paul80nd 0:1832d0ed8da8 106 bool _disp_type;
paul80nd 0:1832d0ed8da8 107 };
paul80nd 0:1832d0ed8da8 108
paul80nd 0:1832d0ed8da8 109 #endif