4 digits 7-segement LED display multiplexed using 2x 74hc595. counts 0-F hex on each of the displays sequentially. Program can control up to 8 digits
main.cpp
- Committer:
- hainjedaf
- Date:
- 2016-05-07
- Revision:
- 3:131580c6ef9c
- Parent:
- 2:c63fd1eaf215
- Child:
- 4:501f1e469bca
File content as of revision 3:131580c6ef9c:
/* Naam: * SPI_4x7LED * * Geschreven door: * Marout Yasuo Sluijter-Borms * * Omschrijving: * 4 x 7 LED segmentdisplay een 2x 74hc595 schuifregister aansturen op de SPI bus * * Aanmaakdatum: * 5 mei 2016 * Laatste update: * 7 mei 2016 * * Revisie: * 1 - ombouwen spi_led_helloworld * 2 - Patroongenetator en digit selector geconstrueerd * * Copyright (c) 2016, MYSB, The Manhattan Project * Zie einde programma / See end of program. */ /* Libraries en classes insluiten */ #include "mbed.h" /* Definineer macros */ #define SPI_SPEED 48000000 // max speed #define SPI_MOSI P0_9 // MOSI: p5 = P0_9 #define SPI_MISO P0_8 // MISO: p6 = P0_8 #define SPI_SCLK P0_7 // SCLK: p7 = P0_7 #define SPI_MODE 3 // Mode 3: CPOL = 1, CPHA= 1 #define REGISTER_WIDTH 8 // 8-bit register #define REGISTER_COUNT 2 // aantal registers in serie #define DIGIT_COUNT 1 // aantal aan te sturen digits #define SEGMENT_COUNT 7 // aantal segmenten per digit #define DP_OUT 0x80 // register adres decimale punt (MSB = 128 = 0x80) #define LED_ON_TIME 0.2 // tijd dat LED aan is /* Declareer seriële poorten */ SPI spi0( SPI_MOSI, SPI_MISO, SPI_SCLK); // Definieer SPI bus /* Declareer digitale IO */ DigitalOut ss0( P0_6, 1); // p8 = slave select 0, actief LAAG! Dus pin hoog om chip te deselecteren! /* Declareer Analoge IO */ /* Declareer globale variabelen */ int spi_data = 0x0000; // variabele voor date doorgifte /* Declareer globale constanten */ const int digit[] = { // waarden om digits te selecteren (activeer een uitgang van schuifregister 0x01, 0x02, // #1, #2 0x04, 0x08, // #3, #4 0x10, 0x20, // #5, #6 0x40, 0x80 // #7, #8 }; const int patroon[] = { // patroon om hexadecimaal digit te tonen 0x7E, 0x30, 0x6D, 0x79, // 0, 1, 2, 3, 0x33, 0x5B, 0x5F, 0x70, // 4, 5, 6, 7, 0x7F, 0x7B, 0x77, 0x1F, // 8, 9, a, b, 0x4E, 0x3D, 0x4F, 0x47 // c, d, e, f }; const int patroon_aantal = ( // aantal patronen in patroon sizeof( patroon) / sizeof (patroon[0]) ); /* Declareer functies */ /* Start hoofprogramma */ int main() { // intellen SPI bus spi0.frequency( SPI_SPEED); // Snelheid bus instellen spi0.format( REGISTER_WIDTH * REGISTER_COUNT, SPI_MODE); // SPI mode instellen; /* maak schuifregisters leeg */ ss0 = 0; // selecteer chip spi0.write( spi_data); // Schrijf leeg byte naar chip ss0 = 1; // deselecteer chip /* Schijf patroon naar register */ while(1) { spi_data = 0x0000; for( int d = 0; d < REGISTER_WIDTH; ++d) { // selecteer digit for( int v = 0; v < patroon_aantal; ++v) { // selekteer segment spi_data = digit[d]; // laad patroon om digit te selecteren spi_data <<= 8; // schuif naar links spi_data += patroon[v]; // voeg patroon om segment te selecteren toe ss0 = 0; // selekteer chip spi0.write( spi_data); // schrijf data naar bus ss0 = 1; // deselecteer chip wait( LED_ON_TIME); // wacht even } // endfor spi_data = 0x0000; ss0 = 0; // selecteer chip spi0.write( spi_data); // Schrijf leeg byte naar chip ss0 = 1; // deselecteer chip wait( LED_ON_TIME); // wacht even } // endfor }; // enwhile }; // endmain /* * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */