Solutions for the 7-Segment Display experiments for LPC812 MAX

Dependencies:   lpc812_exp_lib_PCF8591 mbed

Committer:
embeddedartists
Date:
Fri Nov 22 14:12:41 2013 +0000
Revision:
0:a41af9f7fe16
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:a41af9f7fe16 1 #include "mbed.h"
embeddedartists 0:a41af9f7fe16 2
embeddedartists 0:a41af9f7fe16 3 DigitalOut ssel(D10);
embeddedartists 0:a41af9f7fe16 4 DigitalOut mosi(D11);
embeddedartists 0:a41af9f7fe16 5 DigitalOut sclk(D13);
embeddedartists 0:a41af9f7fe16 6
embeddedartists 0:a41af9f7fe16 7 #define SEG_A 0x80
embeddedartists 0:a41af9f7fe16 8 #define SEG_B 0x40
embeddedartists 0:a41af9f7fe16 9 #define SEG_C 0x20
embeddedartists 0:a41af9f7fe16 10 #define SEG_D 0x10
embeddedartists 0:a41af9f7fe16 11 #define SEG_E 0x08
embeddedartists 0:a41af9f7fe16 12 #define SEG_F 0x04
embeddedartists 0:a41af9f7fe16 13 #define SEG_G 0x02
embeddedartists 0:a41af9f7fe16 14 #define SEG_DP 0x01
embeddedartists 0:a41af9f7fe16 15
embeddedartists 0:a41af9f7fe16 16 const uint8_t segments[16] = {
embeddedartists 0:a41af9f7fe16 17 SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F , //0 = A,B,C,D,E,F
embeddedartists 0:a41af9f7fe16 18 SEG_B | SEG_C , //1 = B,C
embeddedartists 0:a41af9f7fe16 19 SEG_A | SEG_B | SEG_D | SEG_E | SEG_G, //2 = A,B,D,E,G
embeddedartists 0:a41af9f7fe16 20 SEG_A | SEG_B | SEG_C | SEG_D | SEG_G, //3 = A,B,C,D,G
embeddedartists 0:a41af9f7fe16 21 SEG_B | SEG_C | SEG_F | SEG_G, //4 = B,C,F,G
embeddedartists 0:a41af9f7fe16 22 SEG_A | SEG_C | SEG_D | SEG_F | SEG_G, //5 = A,C,D,F,G
embeddedartists 0:a41af9f7fe16 23 SEG_A | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G, //6 = A,C,D,E,F,G
embeddedartists 0:a41af9f7fe16 24 SEG_A | SEG_B | SEG_C , //7 = A,B,C
embeddedartists 0:a41af9f7fe16 25 SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G, //8 = A,B,C,D,E,F,G
embeddedartists 0:a41af9f7fe16 26 SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G, //9 = A,B,C,D,F,G
embeddedartists 0:a41af9f7fe16 27 SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G, //A = A,B,C,E,F,G
embeddedartists 0:a41af9f7fe16 28 SEG_C | SEG_D | SEG_E | SEG_F | SEG_G, //B = C,D,E,F,G
embeddedartists 0:a41af9f7fe16 29 SEG_D | SEG_E | SEG_G, //C = D,E,G
embeddedartists 0:a41af9f7fe16 30 SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, //D = B,C,D,E,G
embeddedartists 0:a41af9f7fe16 31 SEG_A | SEG_D | SEG_E | SEG_F | SEG_G, //E = A,D,E,F,G
embeddedartists 0:a41af9f7fe16 32 SEG_A | SEG_E | SEG_F | SEG_G}; //F = A,E,F,G
embeddedartists 0:a41af9f7fe16 33
embeddedartists 0:a41af9f7fe16 34 static void updateShiftReg(uint8_t segments)
embeddedartists 0:a41af9f7fe16 35 {
embeddedartists 0:a41af9f7fe16 36 uint8_t bitCnt;
embeddedartists 0:a41af9f7fe16 37
embeddedartists 0:a41af9f7fe16 38 //Pull SCK and MOSI low, pull SSEL low
embeddedartists 0:a41af9f7fe16 39 ssel = 0;
embeddedartists 0:a41af9f7fe16 40 mosi = 0;
embeddedartists 0:a41af9f7fe16 41 sclk = 0;
embeddedartists 0:a41af9f7fe16 42
embeddedartists 0:a41af9f7fe16 43 //wait 1us
embeddedartists 0:a41af9f7fe16 44 wait_us(1);
embeddedartists 0:a41af9f7fe16 45
embeddedartists 0:a41af9f7fe16 46 //Loop through all eight bits
embeddedartists 0:a41af9f7fe16 47 for (bitCnt = 0; bitCnt < 8; bitCnt++)
embeddedartists 0:a41af9f7fe16 48 {
embeddedartists 0:a41af9f7fe16 49 //output MOSI value (depends on bit 7 of "segments")
embeddedartists 0:a41af9f7fe16 50 if (segments & 0x80) {
embeddedartists 0:a41af9f7fe16 51 mosi = 1;
embeddedartists 0:a41af9f7fe16 52 } else {
embeddedartists 0:a41af9f7fe16 53 mosi = 0;
embeddedartists 0:a41af9f7fe16 54 }
embeddedartists 0:a41af9f7fe16 55
embeddedartists 0:a41af9f7fe16 56 //wait 1us
embeddedartists 0:a41af9f7fe16 57 wait_us(1);
embeddedartists 0:a41af9f7fe16 58
embeddedartists 0:a41af9f7fe16 59 //pull SCK high
embeddedartists 0:a41af9f7fe16 60 sclk = 1;
embeddedartists 0:a41af9f7fe16 61
embeddedartists 0:a41af9f7fe16 62 //wait 1us
embeddedartists 0:a41af9f7fe16 63 wait_us(1);
embeddedartists 0:a41af9f7fe16 64
embeddedartists 0:a41af9f7fe16 65 //pull SCK low
embeddedartists 0:a41af9f7fe16 66 sclk = 0;
embeddedartists 0:a41af9f7fe16 67
embeddedartists 0:a41af9f7fe16 68 //shift "segments"
embeddedartists 0:a41af9f7fe16 69 segments = segments << 1;
embeddedartists 0:a41af9f7fe16 70 }
embeddedartists 0:a41af9f7fe16 71
embeddedartists 0:a41af9f7fe16 72 //Pull SSEL high
embeddedartists 0:a41af9f7fe16 73 ssel = 1;
embeddedartists 0:a41af9f7fe16 74 }
embeddedartists 0:a41af9f7fe16 75
embeddedartists 0:a41af9f7fe16 76 static void experiment3()
embeddedartists 0:a41af9f7fe16 77 {
embeddedartists 0:a41af9f7fe16 78 while(1) {
embeddedartists 0:a41af9f7fe16 79 for (int i = 0; i < 16; i++) {
embeddedartists 0:a41af9f7fe16 80 updateShiftReg(~segments[i]);
embeddedartists 0:a41af9f7fe16 81 wait(0.4);
embeddedartists 0:a41af9f7fe16 82 }
embeddedartists 0:a41af9f7fe16 83 }
embeddedartists 0:a41af9f7fe16 84 }
embeddedartists 0:a41af9f7fe16 85
embeddedartists 0:a41af9f7fe16 86 int main()
embeddedartists 0:a41af9f7fe16 87 {
embeddedartists 0:a41af9f7fe16 88 experiment3();
embeddedartists 0:a41af9f7fe16 89 }