test test / Mbed 2 deprecated calcRate
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers 7SegSRDriver.cpp Source File

7SegSRDriver.cpp

00001 #include "mbed.h"
00002 #include "7SegSRDriver.h"
00003 
00004 /* mbed 7-Segment Display Driver Library (via an 8bit Shift Register)
00005  * Copyright (c) 2011 Paul Law
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a copy
00008  * of this software and associated documentation files (the "Software"), to deal
00009  * in the Software without restriction, including without limitation the rights
00010  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011  * copies of the Software, and to permit persons to whom the Software is
00012  * furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in
00015  * all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023  * THE SOFTWARE.
00024  */
00025 
00026 // Display should be hooked up to shift register as follows:
00027 // Q0 : Decimal Point, Q1-Q7 : Segments a-g
00028 
00029 SSegSRDriver::SSegSRDriver(PinName srData, PinName srClock, PinName srLatch, bool disp_type): _srData(srData), _srClock(srClock), _srLatch(srLatch) {
00030     _disp_type = disp_type;
00031     write_raw(0);
00032 }
00033 
00034 void SSegSRDriver::set_type(bool disp_type) {
00035     _disp_type = disp_type;
00036 }
00037 
00038 void SSegSRDriver::clear() {
00039     write_raw(0);
00040 }
00041 
00042 void SSegSRDriver::write(unsigned char number) {
00043     write(number,0);
00044 }
00045 
00046 void SSegSRDriver::write(unsigned char number, bool dp) {
00047     if (number<16) {
00048         // Find the definition of the number in chardefs, shift left, set dp (LSB)
00049         if(!_disp_type) //if Common Anode
00050             write_raw((SSegSRDriver_chardefs[number])<<1 | dp); //small change to fit taobao buy LEDs.
00051         else //if common Cathode
00052             write_raw((SSegSRDriver_chardefs[number]) | (dp<<0x7)); //small change to fit taobao buy LEDs.
00053     }
00054 }
00055 
00056 void SSegSRDriver::write_raw(unsigned char bValue) {
00057 
00058     if (!_disp_type) bValue = ~bValue;  // Reverse value for Common Anode
00059 
00060     if (bValue<=0xFF) {
00061         // Push value to shift register and latch
00062         _srLatch = 0;
00063         for (int i=7;i>=0;i--) {    // Output MSB to LSB
00064             _srClock = 0;
00065             _srData = (bValue & (1<<i));
00066             _srClock = 1;
00067         }
00068         _srLatch = 1;
00069         _srData = 0;
00070     }
00071 }