Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // GLOBAL VARIABLES
- unsigned long beg_time; // the time right before the current number was initially displayed
- int curr_num = 0; // the current number being displayed, start at 0
- // loop through the 10 numbers in the array. Each contains the 5 columns that are used for the persistance of vision of
- // the particular number
- byte numbers[][5] = {{0x3E, 0x51, 0x49, 0x45, 0x3E},{0x00, 0x42, 0x7F, 0x40, 0x00},{0x42, 0x61, 0x51, 0x49, 0x46},
- {0x21, 0x41, 0x45, 0x4B, 0x31},{0x18, 0x14, 0x12, 0x7F, 0x10},{0x27, 0x45, 0x45, 0x45, 0x39},
- {0x3C, 0x4A, 0x49, 0x49, 0x30},{0x01, 0x71, 0x09, 0x05, 0x03},{0x36, 0x49, 0x49, 0x49, 0x36},
- {0x06, 0x49, 0x49, 0x29, 0x1E}}; // an array of arrays containing each number's 5 LED column byte sequences
- void setup()
- {
- // clock is PORTB0, latch is PORTB1, data is PORTB2
- DDRB = DDRB | B00000111; // set latch, clock and data to outputs
- beg_time = millis(); // set the beginning time for the first 0
- }
- // Prof. Rajit said this way of doing it was ok
- void loop()
- {
- // if the current number has been displayed for more than 1 second, change
- if (millis() - beg_time > 1000) {
- beg_time = millis(); // the beginning time of the new number
- // if the number is 9, then change it back to 0, otherwise increment
- if (curr_num >= 9) {
- curr_num = 0;
- } else {
- curr_num++;
- }
- }
- // loop through the five columns of the 5x7 display
- for (int j=0; j < 5; j++) {
- // the baseline bits to set all LEDs in the column off
- // NOTE: this is LSB first, so leds[0] is the first pin and so on. The last 4 don't matter,
- // since we don't use the last 4 output pins of the second shift register
- int leds[16] = {1,0,1,0,0,0,1,1,0,1,0,0,0,0,0,0};
- // our custom function that takes in the desired column as a byte and the array of
- // output pins on the shift registers and changes the array to appropriately set pins
- // on the display. Ex. if pin 1 is low and pin 2 is high, the third row, first column
- // LED is lit up (Understanding of the data sheet)
- convert(j, numbers[curr_num][j], leds);
- PORTB = PORTB & B11111101; // sets latch LOW
- // set all the output pins of the shift register
- for (int i=15; i>=0; i--) {
- PORTB = PORTB & B11111010; // sets clk and data to LOW
- PORTB = PORTB | leds[i] << 2; // sets data to leds[i]
- PORTB = PORTB | B00000001; // sets digital pin 8 to HIGH
- }
- PORTB = PORTB | B00000010; // sets digital pin 9 to HIGH
- }
- }
- void convert(int col, byte values, int *def) {
- // check if any LED was high
- bool saw_high = false;
- // for the 2nd column, the actual pin is 3, so adjust these values
- // based on the LED matrix's data sheet
- int real_col = 0;
- if (col == 0) {
- real_col = 0;
- } else if (col == 1) {
- real_col = 2;
- } else if (col == 2) {
- real_col = 9;
- } else if (col == 3) {
- real_col = 6;
- } else if (col == 4) {
- real_col = 7;
- }
- // loop through the first 7 values of the byte storing the column
- // LEDs (the 8th is useless, since there are only 7 values to represent).
- // If they are high, set the appropriate shift register output pin to
- // high
- for (int i=0; i<7; i++) {
- if (((values >> i) & 0x01) == 1) {
- if (i == 0) {
- def[11] = 1;
- } else if (i == 1) {
- def[10] = 1;
- } else if (i == 2) {
- def[1] = 1;
- } else if (i == 3) {
- def[8] = 1;
- } else if (i == 4) {
- def[3] = 1;
- } else if (i == 5) {
- def[4] = 1;
- } else if (i == 6) {
- def[5] = 1;
- }
- saw_high = true;
- }
- }
- // set the column to ground (so the circuit allows current to flow) if
- // any LED was seen to be on
- if (saw_high) {
- def[real_col] = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement