Jim Cordwell
/
Segments_01_Simple
A stripped down simple seven segement example for RedBED
main.cpp@4:84ce87a4a8e8, 2014-05-24 (annotated)
- Committer:
- JimCordwell
- Date:
- Sat May 24 19:03:48 2014 +0000
- Revision:
- 4:84ce87a4a8e8
- Parent:
- 3:6b50a41e54f3
- Child:
- 5:dfcd93886d8a
Optimised the segment setting function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JimCordwell | 0:308c4a8d774b | 1 | /************************************************************************** |
JimCordwell | 0:308c4a8d774b | 2 | * * |
JimCordwell | 0:308c4a8d774b | 3 | * A stripped down simple seven segement example for RedBED * |
JimCordwell | 0:308c4a8d774b | 4 | * * |
JimCordwell | 0:308c4a8d774b | 5 | * The LED block actually has 8 illuminable sections * |
JimCordwell | 0:308c4a8d774b | 6 | * segments 'a' to 'g' for the main display and 'p' for a decimal point * |
JimCordwell | 0:308c4a8d774b | 7 | * _________ * |
JimCordwell | 0:308c4a8d774b | 8 | * <____a____> * |
JimCordwell | 0:308c4a8d774b | 9 | * / \ / \ * |
JimCordwell | 0:308c4a8d774b | 10 | * | | | | * |
JimCordwell | 0:308c4a8d774b | 11 | * |f| |b| * |
JimCordwell | 0:308c4a8d774b | 12 | * | | | | * |
JimCordwell | 0:308c4a8d774b | 13 | * \ /_________ \ / * |
JimCordwell | 0:308c4a8d774b | 14 | * <____g____> * |
JimCordwell | 0:308c4a8d774b | 15 | * / \ / \ * |
JimCordwell | 0:308c4a8d774b | 16 | * | | | | * |
JimCordwell | 0:308c4a8d774b | 17 | * |e| |c| * |
JimCordwell | 0:308c4a8d774b | 18 | * | | | | * |
JimCordwell | 0:308c4a8d774b | 19 | * \ /_________ \ / * |
JimCordwell | 0:308c4a8d774b | 20 | * <____d____> |p| * |
JimCordwell | 0:308c4a8d774b | 21 | * * |
JimCordwell | 0:308c4a8d774b | 22 | **************************************************************************/ |
JimCordwell | 0:308c4a8d774b | 23 | |
JimCordwell | 0:308c4a8d774b | 24 | #include "mbed.h" |
JimCordwell | 0:308c4a8d774b | 25 | |
JimCordwell | 0:308c4a8d774b | 26 | uint8_t left = 0; |
JimCordwell | 0:308c4a8d774b | 27 | uint8_t right = 1; |
JimCordwell | 0:308c4a8d774b | 28 | |
JimCordwell | 0:308c4a8d774b | 29 | //The 8 segment outputs and com to switch between the LEDs |
JimCordwell | 0:308c4a8d774b | 30 | 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); |
JimCordwell | 0:308c4a8d774b | 31 | |
JimCordwell | 0:308c4a8d774b | 32 | /****************************************************** |
JimCordwell | 2:91808e29f193 | 33 | * Set each of the segments for a desired LED * |
JimCordwell | 0:308c4a8d774b | 34 | ******************************************************/ |
JimCordwell | 0:308c4a8d774b | 35 | 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) |
JimCordwell | 0:308c4a8d774b | 36 | { |
JimCordwell | 1:1336ea4dd725 | 37 | //Set desired LED to update |
JimCordwell | 1:1336ea4dd725 | 38 | com = led; |
JimCordwell | 0:308c4a8d774b | 39 | |
JimCordwell | 4:84ce87a4a8e8 | 40 | //Update the segments to the desired values. |
JimCordwell | 4:84ce87a4a8e8 | 41 | //the loop ensures the segments are on for an amount |
JimCordwell | 4:84ce87a4a8e8 | 42 | //of time before being switched off again. |
JimCordwell | 0:308c4a8d774b | 43 | for(uint8_t i = 0; i < 100; i++) |
JimCordwell | 0:308c4a8d774b | 44 | { |
JimCordwell | 1:1336ea4dd725 | 45 | seg_a = 1 - a; //setting a segment to 1 means off |
JimCordwell | 1:1336ea4dd725 | 46 | seg_b = 1 - b; //so invert all requested segment values |
JimCordwell | 0:308c4a8d774b | 47 | seg_c = 1 - c; |
JimCordwell | 0:308c4a8d774b | 48 | seg_d = 1 - d; |
JimCordwell | 0:308c4a8d774b | 49 | seg_e = 1 - e; |
JimCordwell | 0:308c4a8d774b | 50 | seg_f = 1 - f; |
JimCordwell | 0:308c4a8d774b | 51 | seg_g = 1 - g; |
JimCordwell | 0:308c4a8d774b | 52 | seg_p = 1 - p; |
JimCordwell | 0:308c4a8d774b | 53 | } |
JimCordwell | 3:6b50a41e54f3 | 54 | |
JimCordwell | 3:6b50a41e54f3 | 55 | //Finally switch off all segments again, we must do this otherwise when |
JimCordwell | 3:6b50a41e54f3 | 56 | //switching com the segments will momentarily duplicated from this led. |
JimCordwell | 3:6b50a41e54f3 | 57 | seg_a = seg_b = seg_c = seg_d = seg_e = seg_f = seg_g = seg_p = 1; |
JimCordwell | 0:308c4a8d774b | 58 | } |
JimCordwell | 0:308c4a8d774b | 59 | |
JimCordwell | 0:308c4a8d774b | 60 | /****************************************************** |
JimCordwell | 0:308c4a8d774b | 61 | * Main loop repeatedly updates each of the LEDs * |
JimCordwell | 0:308c4a8d774b | 62 | ******************************************************/ |
JimCordwell | 0:308c4a8d774b | 63 | int main() |
JimCordwell | 0:308c4a8d774b | 64 | { |
JimCordwell | 0:308c4a8d774b | 65 | for(;;) |
JimCordwell | 0:308c4a8d774b | 66 | { |
JimCordwell | 3:6b50a41e54f3 | 67 | //Set the display to read '42' |
JimCordwell | 0:308c4a8d774b | 68 | SetSegments(left, 0,1,1,0,0,1,1,0); //4 |
JimCordwell | 0:308c4a8d774b | 69 | SetSegments(right, 1,1,0,1,1,0,1,0); //2 |
JimCordwell | 0:308c4a8d774b | 70 | } |
JimCordwell | 0:308c4a8d774b | 71 | } |
JimCordwell | 0:308c4a8d774b | 72 |