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@5:f16387ae15af, 2016-05-08 (annotated)
- Committer:
- hainjedaf
- Date:
- Sun May 08 15:55:21 2016 +0000
- Revision:
- 5:f16387ae15af
- Parent:
- 4:501f1e469bca
Final version: 1-8 digits each counts 0-F in hex.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hainjedaf | 0:f44d91c545d9 | 1 | /* Naam: |
hainjedaf | 3:131580c6ef9c | 2 | * SPI_4x7LED |
hainjedaf | 0:f44d91c545d9 | 3 | * |
hainjedaf | 0:f44d91c545d9 | 4 | * Geschreven door: |
hainjedaf | 0:f44d91c545d9 | 5 | * Marout Yasuo Sluijter-Borms |
hainjedaf | 0:f44d91c545d9 | 6 | * |
hainjedaf | 0:f44d91c545d9 | 7 | * Omschrijving: |
hainjedaf | 5:f16387ae15af | 8 | * 4 x 7 LED segmentdisplay en 2x 74hc595 schuifregister aansturen op de SPI bus |
hainjedaf | 5:f16387ae15af | 9 | * teller 0 tot 9999 |
hainjedaf | 0:f44d91c545d9 | 10 | * |
hainjedaf | 0:f44d91c545d9 | 11 | * Aanmaakdatum: |
hainjedaf | 3:131580c6ef9c | 12 | * 5 mei 2016 |
hainjedaf | 0:f44d91c545d9 | 13 | * Laatste update: |
hainjedaf | 3:131580c6ef9c | 14 | * 7 mei 2016 |
hainjedaf | 0:f44d91c545d9 | 15 | * |
hainjedaf | 0:f44d91c545d9 | 16 | * Revisie: |
hainjedaf | 4:501f1e469bca | 17 | * 1 - Ombouwen spi_led_helloworld |
hainjedaf | 3:131580c6ef9c | 18 | * 2 - Patroongenetator en digit selector geconstrueerd |
hainjedaf | 4:501f1e469bca | 19 | * 3 - Inversie van uitgangen |
hainjedaf | 4:501f1e469bca | 20 | * - MSB = DP, LSB = segment A |
hainjedaf | 0:f44d91c545d9 | 21 | * |
hainjedaf | 0:f44d91c545d9 | 22 | * Copyright (c) 2016, MYSB, The Manhattan Project |
hainjedaf | 0:f44d91c545d9 | 23 | * Zie einde programma / See end of program. |
hainjedaf | 0:f44d91c545d9 | 24 | */ |
hainjedaf | 0:f44d91c545d9 | 25 | /* Libraries en classes insluiten */ |
hainjedaf | 0:f44d91c545d9 | 26 | #include "mbed.h" |
hainjedaf | 0:f44d91c545d9 | 27 | |
hainjedaf | 3:131580c6ef9c | 28 | /* Definineer macros */ |
hainjedaf | 3:131580c6ef9c | 29 | #define SPI_SPEED 48000000 // max speed |
hainjedaf | 3:131580c6ef9c | 30 | #define SPI_MOSI P0_9 // MOSI: p5 = P0_9 |
hainjedaf | 3:131580c6ef9c | 31 | #define SPI_MISO P0_8 // MISO: p6 = P0_8 |
hainjedaf | 3:131580c6ef9c | 32 | #define SPI_SCLK P0_7 // SCLK: p7 = P0_7 |
hainjedaf | 3:131580c6ef9c | 33 | #define SPI_MODE 3 // Mode 3: CPOL = 1, CPHA= 1 |
hainjedaf | 3:131580c6ef9c | 34 | #define REGISTER_WIDTH 8 // 8-bit register |
hainjedaf | 3:131580c6ef9c | 35 | #define REGISTER_COUNT 2 // aantal registers in serie |
hainjedaf | 4:501f1e469bca | 36 | #define DIGIT_COUNT 4 // aantal aan te sturen digits |
hainjedaf | 3:131580c6ef9c | 37 | #define SEGMENT_COUNT 7 // aantal segmenten per digit |
hainjedaf | 3:131580c6ef9c | 38 | #define DP_OUT 0x80 // register adres decimale punt (MSB = 128 = 0x80) |
hainjedaf | 3:131580c6ef9c | 39 | #define LED_ON_TIME 0.2 // tijd dat LED aan is |
hainjedaf | 3:131580c6ef9c | 40 | |
hainjedaf | 1:a50c12484eb9 | 41 | /* Declareer seriële poorten */ |
hainjedaf | 3:131580c6ef9c | 42 | SPI spi0( SPI_MOSI, SPI_MISO, SPI_SCLK); |
hainjedaf | 3:131580c6ef9c | 43 | // Definieer SPI bus |
hainjedaf | 0:f44d91c545d9 | 44 | |
hainjedaf | 1:a50c12484eb9 | 45 | /* Declareer digitale IO */ |
hainjedaf | 2:c63fd1eaf215 | 46 | DigitalOut ss0( P0_6, 1); // p8 = slave select 0, actief LAAG! Dus pin hoog om chip te deselecteren! |
hainjedaf | 0:f44d91c545d9 | 47 | |
hainjedaf | 1:a50c12484eb9 | 48 | /* Declareer Analoge IO */ |
hainjedaf | 1:a50c12484eb9 | 49 | /* Declareer globale variabelen */ |
hainjedaf | 5:f16387ae15af | 50 | int spi_data = 0x0000; |
hainjedaf | 3:131580c6ef9c | 51 | |
hainjedaf | 1:a50c12484eb9 | 52 | /* Declareer globale constanten */ |
hainjedaf | 3:131580c6ef9c | 53 | const int digit[] = { // waarden om digits te selecteren (activeer een uitgang van schuifregister |
hainjedaf | 3:131580c6ef9c | 54 | 0x01, 0x02, // #1, #2 |
hainjedaf | 3:131580c6ef9c | 55 | 0x04, 0x08, // #3, #4 |
hainjedaf | 3:131580c6ef9c | 56 | 0x10, 0x20, // #5, #6 |
hainjedaf | 3:131580c6ef9c | 57 | 0x40, 0x80 // #7, #8 |
hainjedaf | 2:c63fd1eaf215 | 58 | }; |
hainjedaf | 4:501f1e469bca | 59 | const int decimalepunt = 0x80; // waarde van de decimale punt (DP = MSB) |
hainjedaf | 4:501f1e469bca | 60 | const int patroon[] = { // patroon om hexadecimaal digit te tonen (LSB a..g MSB-1, actief LAAG) |
hainjedaf | 4:501f1e469bca | 61 | 0x40, 0x79, 0x24, 0x30, // 0, 1, 2, 3, |
hainjedaf | 4:501f1e469bca | 62 | 0x19, 0x12, 0x02, 0x78, // 4, 5, 6, 7, |
hainjedaf | 4:501f1e469bca | 63 | 0x00, 0x10, 0x08, 0x03, // 8, 9, A, B, |
hainjedaf | 4:501f1e469bca | 64 | 0x46, 0x21, 0x06, 0x0E // C, D, E, F |
hainjedaf | 3:131580c6ef9c | 65 | }; |
hainjedaf | 3:131580c6ef9c | 66 | const int patroon_aantal = ( // aantal patronen in patroon |
hainjedaf | 3:131580c6ef9c | 67 | sizeof( patroon) / |
hainjedaf | 3:131580c6ef9c | 68 | sizeof (patroon[0]) |
hainjedaf | 3:131580c6ef9c | 69 | ); |
hainjedaf | 2:c63fd1eaf215 | 70 | |
hainjedaf | 1:a50c12484eb9 | 71 | /* Declareer functies */ |
hainjedaf | 0:f44d91c545d9 | 72 | /* Start hoofprogramma */ |
hainjedaf | 0:f44d91c545d9 | 73 | int main() |
hainjedaf | 0:f44d91c545d9 | 74 | { |
hainjedaf | 2:c63fd1eaf215 | 75 | // intellen SPI bus |
hainjedaf | 2:c63fd1eaf215 | 76 | spi0.frequency( SPI_SPEED); // Snelheid bus instellen |
hainjedaf | 3:131580c6ef9c | 77 | spi0.format( REGISTER_WIDTH * REGISTER_COUNT, SPI_MODE); |
hainjedaf | 2:c63fd1eaf215 | 78 | // SPI mode instellen; |
hainjedaf | 2:c63fd1eaf215 | 79 | |
hainjedaf | 2:c63fd1eaf215 | 80 | /* maak schuifregisters leeg */ |
hainjedaf | 3:131580c6ef9c | 81 | ss0 = 0; // selecteer chip |
hainjedaf | 5:f16387ae15af | 82 | spi0.write( 0); // Schrijf leeg byte naar chip |
hainjedaf | 3:131580c6ef9c | 83 | ss0 = 1; // deselecteer chip |
hainjedaf | 4:501f1e469bca | 84 | wait( LED_ON_TIME); // wacht even |
hainjedaf | 2:c63fd1eaf215 | 85 | /* Schijf patroon naar register */ |
hainjedaf | 2:c63fd1eaf215 | 86 | while(1) { |
hainjedaf | 3:131580c6ef9c | 87 | spi_data = 0x0000; |
hainjedaf | 4:501f1e469bca | 88 | for( int d = 0; d < DIGIT_COUNT; ++d) { |
hainjedaf | 3:131580c6ef9c | 89 | // selecteer digit |
hainjedaf | 3:131580c6ef9c | 90 | for( int v = 0; v < patroon_aantal; ++v) { |
hainjedaf | 3:131580c6ef9c | 91 | // selekteer segment |
hainjedaf | 3:131580c6ef9c | 92 | spi_data = digit[d]; |
hainjedaf | 3:131580c6ef9c | 93 | // laad patroon om digit te selecteren |
hainjedaf | 4:501f1e469bca | 94 | spi_data <<= 8; // schuif digit naar MSB |
hainjedaf | 3:131580c6ef9c | 95 | spi_data += patroon[v]; |
hainjedaf | 4:501f1e469bca | 96 | // voeg patroon om segmenten te selecteren toe |
hainjedaf | 4:501f1e469bca | 97 | // write |
hainjedaf | 3:131580c6ef9c | 98 | ss0 = 0; // selekteer chip |
hainjedaf | 3:131580c6ef9c | 99 | spi0.write( spi_data); |
hainjedaf | 3:131580c6ef9c | 100 | // schrijf data naar bus |
hainjedaf | 3:131580c6ef9c | 101 | ss0 = 1; // deselecteer chip |
hainjedaf | 3:131580c6ef9c | 102 | wait( LED_ON_TIME); // wacht even |
hainjedaf | 3:131580c6ef9c | 103 | } // endfor |
hainjedaf | 3:131580c6ef9c | 104 | } // endfor |
hainjedaf | 2:c63fd1eaf215 | 105 | }; // enwhile |
hainjedaf | 2:c63fd1eaf215 | 106 | }; // endmain |
hainjedaf | 0:f44d91c545d9 | 107 | /* |
hainjedaf | 0:f44d91c545d9 | 108 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
hainjedaf | 0:f44d91c545d9 | 109 | * of this software and associated documentation files (the "Software"), to deal |
hainjedaf | 0:f44d91c545d9 | 110 | * in the Software without restriction, including without limitation the rights |
hainjedaf | 0:f44d91c545d9 | 111 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
hainjedaf | 0:f44d91c545d9 | 112 | * copies of the Software, and to permit persons to whom the Software is |
hainjedaf | 0:f44d91c545d9 | 113 | * furnished to do so, subject to the following conditions: |
hainjedaf | 0:f44d91c545d9 | 114 | * |
hainjedaf | 0:f44d91c545d9 | 115 | * The above copyright notice and this permission notice shall be included in |
hainjedaf | 0:f44d91c545d9 | 116 | * all copies or substantial portions of the Software. |
hainjedaf | 0:f44d91c545d9 | 117 | * |
hainjedaf | 0:f44d91c545d9 | 118 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
hainjedaf | 0:f44d91c545d9 | 119 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
hainjedaf | 0:f44d91c545d9 | 120 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
hainjedaf | 0:f44d91c545d9 | 121 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
hainjedaf | 0:f44d91c545d9 | 122 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
hainjedaf | 0:f44d91c545d9 | 123 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
hainjedaf | 0:f44d91c545d9 | 124 | * THE SOFTWARE. |
hainjedaf | 0:f44d91c545d9 | 125 | */ |