Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * main.c
- *
- * Created: 9/1/2016 9:34:16 PM
- * Author : Austin Hull, Drexel University - College of Computing & Informatics, adh86@drexel.edu
- *
- * Urban-Squared Project is the property of Austin Hull, and all rights to this code are hereby reserved to the author, unless otherwise noted.
- */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <avr/pgmspace.h>
- #include <util/delay.h>
- // A value used to set the standard CPU speed to use with the micro-cobtroller.
- #define F_CPU 16000000UL
- // Following definitions represent oft-used pins of the micro-controller system.
- #define PINSS 4
- #define PINMOSI 5
- #define PINSCK 7
- // Following definitions represent register addresses used by the system's MAX7219 display drivers.
- #define DISPLAY_POWER 0x0C
- #define DISPLAY_DECODE_MODE 0x09
- #define DISPLAY_INTENSITY 0x0A
- #define DISPLAY_SCAN_LIMIT 0x0B
- #define DISPLAY_TEST_MODE 0x0F
- // Also try to place "alphabet" of characters in program memory for efficient character access.
- // Font format courtesy of brainybits.ca
- unsigned char alphabetArray[][5] PROGMEM =
- {
- {0x00, 0x00, 0x00, 0x00, 0x00}, // Space character
- {0x5F, 0x00, 0x00, 0x00, 0x00}, // ! character
- {0x00, 0x03, 0x00, 0x03, 0x00}, // " character
- {0x14, 0x3D, 0x14, 0x3D, 0x14}, // # character
- {0x24, 0x6A, 0x2B, 0x12, 0x00}, // $ character
- {0x63, 0x13, 0x08, 0x64, 0x63} // % character
- {0x36, 0x49, 0x56, 0x20, 0x50} // & character
- {0x00, 0x00, 0x03, 0x00, 0x00} // ' character
- {0x00, 0x1C, 0x22, 0x41, 0x00} // ( character
- {0x00, 0x41, 0x22, 0x1C, 0x00} // ) character
- {}
- };
- // Used to hold the text to be scrolled across the display.
- char displayBuffer[][8] =
- {
- {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // SPACE
- {0x00, 0x00, 0x7F, 0x88, 0x88, 0x7F, 0x00, 0x00}, // A
- {0x00, 0x00, 0x0F, 0x01, 0x01, 0x0F, 0x00, 0x00}, // u
- {0x00, 0x00, 0x46, 0x89, 0x89, 0x72, 0x00, 0x00}, // s
- {0x00, 0x00, 0x80, 0x80, 0xFF, 0x80, 0x80, 0x00} // t
- };
- //void initSPI(void);
- //void sendSPI(char addr, char data, int moduleQuantity);
- //void clearDisplay(int modQuantity)
- // This function simply sets some direction registers and SPI registers in order to begin prepping the SPI module for use.
- void initSPI(void)
- {
- // Set MOSI and SCK pins as outputs for SPI use.
- DDRB |= (1 << PINMOSI) | (1 << PINSCK);
- // Enable SPI mode, set system to MASTER.
- SPCR0 |= (1 << SPE0) | (1 << MSTR0);
- }
- // Function which sends SPI commands to a specified number of display modules.
- void sendSPI(char addr, char data, int moduleQuantity)
- {
- // Ensure that SS pin is output low before transmission.
- PORTB &= ~(1 << PINSS);
- /*
- ** Data to be transmitted. First sends address byte, followed by data byte.
- ** System must wait for each transmission to complete before proceeding.
- */
- for(int i = 0; i < moduleQuantity; i++)
- {
- SPDR0 = addr;
- while(!(SPSR0 & (1 << SPIF0)));
- SPDR0 = data;
- while(!(SPSR0 & (1 << SPIF0)));
- }
- // Pulse SS high to lock in transmitted bits.
- PORTB |= (1 << PINSS);
- }
- // Function which handles data to be displayed and translated across the displays.
- void sendColumnSPI(char column, char data, int modQuantity)
- {
- // Ensure that SS pin is output low before transmission.
- PORTB &= ~(1 << PINSS);
- for(int i = 0; i < modQuantity; i++)
- {
- if(i == (column / 8))
- {
- SPDR0 = (column % 8) + 1;
- while(!(SPSR0 & (1 << SPIF0)));
- SPDR0 = data;
- while(!(SPSR0 & (1 << SPIF0)));
- }
- else
- {
- SPDR0 = 0x0;
- while(!(SPSR0 & (1 << SPIF0)));
- SPDR0 = 0x0;
- while(!(SPSR0 & (1 << SPIF0)));
- }
- }
- // Pulse SS high to lock in transmitted bits.
- PORTB |= (1 << PINSS);
- }
- // Experimental function. May hopefully reduce overhead of code for text display.
- void writeCharacterToDisplay(int currentIndex, int charNum, char textBuffer[5][8])
- {
- for(int i = 0; i < 8; i++)
- {
- sendColumnSPI(currentIndex + i, textBuffer[charNum][i], 2);
- }
- }
- // Ensures that all leds on the displays are blanked before use.
- void clearDisplay(int modQuantity)
- {
- int addressTemplate = 0b00000000;
- for(int i = 1; i <= 8; i++)
- {
- sendSPI((addressTemplate+i), 0x00, modQuantity);
- }
- }
- int main(void)
- {
- //sei(); When needed, this line will activate global interrupts.
- // Set some general-use pin directions. Note the SS pin is set as output for SPI purposes.
- DDRB = (1 << PINB0) | (1 << PINSS);
- PORTB = (1 << PINB0);
- // Set up the SPI module for use.
- initSPI();
- // Instantiate the display with the power turned off.
- sendSPI(DISPLAY_POWER, 0x00, 2);
- //Deactivate decode mode.
- sendSPI(DISPLAY_DECODE_MODE, 0x00, 2);
- // Scan limit set for 8 led groups.
- sendSPI(DISPLAY_SCAN_LIMIT, 0x07, 2);
- // Test mode deactivated.
- sendSPI(DISPLAY_TEST_MODE, 0x00, 2);
- // Set intensity of display brightness.
- sendSPI(DISPLAY_INTENSITY, 0x0A, 2);
- // Blank all leds
- clearDisplay(2);
- // Finally, provide power to the display.
- sendSPI(DISPLAY_POWER, 0x01, 2);
- // Main program loop.
- while (1)
- {
- // Scroll loop condition is based on number of characters to be displayed, multiplied by the overall per-character size.
- for(int i = 0; i < (8 * 5); i++)
- {
- /*clearDisplay(2);
- sendColumnSPI(i, displayBuffer[j+1][0], 2); // Should make a display algorithm out of all this!
- sendColumnSPI(i+1, displayBuffer[j+1][1], 2);
- sendColumnSPI(i+2, displayBuffer[j+1][2], 2);
- sendColumnSPI(i+3, displayBuffer[j+1][3], 2);
- sendColumnSPI(i+4, displayBuffer[j+1][4], 2);
- sendColumnSPI(i+5, displayBuffer[j+1][5], 2);
- sendColumnSPI(i+6, displayBuffer[j+1][6], 2);
- sendColumnSPI(i+7, displayBuffer[j+1][7], 2);
- sendColumnSPI(i+8, displayBuffer[j][0], 2);
- sendColumnSPI(i+9, displayBuffer[j][0], 2);
- sendColumnSPI(i+10, displayBuffer[j][0], 2);
- sendColumnSPI(i+11, displayBuffer[j][0], 2);
- sendColumnSPI(i+12, displayBuffer[j][0], 2);
- sendColumnSPI(i+13, displayBuffer[j][0], 2);
- sendColumnSPI(i+14, displayBuffer[j][0], 2);
- sendColumnSPI(i+15, displayBuffer[j][0], 2);
- sendColumnSPI(i - 8, displayBuffer[j+1][0], 2);
- sendColumnSPI(i - 9, displayBuffer[j+1][1], 2);
- sendColumnSPI(i - 10, displayBuffer[j+1][2], 2);
- sendColumnSPI(i-11, displayBuffer[j+1][3], 2);
- sendColumnSPI(i-12, displayBuffer[j+1][4], 2);
- sendColumnSPI(i-13, displayBuffer[j+1][5], 2);
- sendColumnSPI(i-14, displayBuffer[j+1][6], 2);
- sendColumnSPI(i-15, displayBuffer[j+1][7], 2);
- _delay_ms(750); Basically obsolete at this point - only keeping it around for reference purposes.*/
- //writeCharacterToDisplay(i - 0, 0, displayBuffer);
- //writeCharacterToDisplay(i - 6, 1, displayBuffer);
- //writeCharacterToDisplay(i - 12, 2, displayBuffer);
- //writeCharacterToDisplay(i - 18, 3, displayBuffer);
- //writeCharacterToDisplay(i - 24, 4, displayBuffer);
- //writeCharacterToDisplay(i - 30, 0, displayBuffer);
- // Iterate through the various characters to be displayed, and send them to the display.
- for(int k = 0; k <= 30; k+=6)
- {
- writeCharacterToDisplay(i - k, k % 5, displayBuffer);
- }
- _delay_ms(500);
- }
- if (PORTB & (1 << PINB0))
- {
- PORTB ^= (1 << PINB0);
- }
- else
- {
- PORTB ^= (1 << PINB0);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement