Jim Cordwell / Mbed 2 deprecated Segments_02_Brightness

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**************************************************************************
00002 *                                                                         *
00003 *   This example builds on Segments_01_Simple by introducing an example   *
00004 *   of how brightness might be achieved.                                  *
00005 *                                                                         *
00006 *   In this case the right hand LED is displayed dimmer than the left.    *
00007 *   With more logic it would be possible to adjust the brightness of      *
00008 *   individual segments within the display.                               *
00009 *                                                                         *
00010 **************************************************************************/
00011 
00012 #include "mbed.h"
00013 
00014 uint8_t left = 0;
00015 uint8_t right = 1;
00016 
00017 uint8_t brightness = 100;
00018 
00019 //The 8 segment outputs and com to switch between the LEDs
00020 DigitalOut seg_a(P1_23), seg_b(P1_28), seg_c(P0_16), seg_d(P1_31), seg_e(P1_13), seg_f(P1_16), seg_g(P1_19), seg_p(P0_23), com(P1_25);
00021 
00022 /******************************************************
00023 *   Set each of the segments for a desired LED        *
00024 ******************************************************/
00025 void SetSegments(uint8_t led, uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e, uint8_t f, uint8_t g, uint8_t p)
00026 { 
00027     //Set desired LED to update
00028     com = led;
00029     
00030     //Switch on the desired segments a number of times based on the brightness required
00031     for(uint8_t i = 0; i < brightness; i++) 
00032     {
00033         seg_a = 1 - a; //setting a segment to 1 means off 
00034         seg_b = 1 - b; //so invert all requested segment values
00035         seg_c = 1 - c;
00036         seg_d = 1 - d;
00037         seg_e = 1 - e;
00038         seg_f = 1 - f;
00039         seg_g = 1 - g;
00040         seg_p = 1 - p;
00041     }
00042     
00043     //Switch off all the segments a remaining number of times to reach 100 updates
00044     for(uint8_t i = 0; i < 100 - brightness; i++) 
00045     {
00046         seg_a = seg_b = seg_c = seg_d = seg_e = seg_f = seg_g = seg_p = 1;
00047     }
00048     
00049     //Finally switch off all segments again, we must do this otherwise when
00050     //switching com the segments could be momentarily duplicated from this led.
00051     seg_a = seg_b = seg_c = seg_d = seg_e = seg_f = seg_g = seg_p = 1;
00052 
00053 }
00054 
00055 /******************************************************
00056 *   Main loop repeatedly updates each of the LEDs     *
00057 ******************************************************/
00058 int main()
00059 {
00060     for(;;) 
00061     {
00062         //Set the display to read '42'
00063         
00064         //The 4 will be displayed at full brightness
00065         brightness = 100;
00066         SetSegments(left,  0,1,1,0,0,1,1,0); //4
00067         
00068         //The 2 will be displayed much more faintly
00069         brightness = 5;
00070         SetSegments(right, 1,1,0,1,1,0,1,0); //2
00071     }
00072 }
00073