Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1. /*
  2. * main.c
  3. *
  4. * Created: 9/1/2016 9:34:16 PM
  5. * Author : Austin Hull, Drexel University - College of Computing & Informatics, adh86@drexel.edu
  6. *
  7. * Urban-Squared Project is the property of Austin Hull, and all rights to this code are hereby reserved to the author, unless otherwise noted.
  8. */
  9.  
  10. #include <avr/io.h>
  11. #include <avr/interrupt.h>
  12. #include <avr/pgmspace.h>
  13. #include <util/delay.h>
  14.  
  15. // A value used to set the standard CPU speed to use with the micro-cobtroller.
  16. #define F_CPU 16000000UL
  17.  
  18. // Following definitions represent oft-used pins of the micro-controller system.
  19. #define PINSS 4
  20. #define PINMOSI 5
  21. #define PINSCK 7
  22.  
  23. // Following definitions represent register addresses used by the system's MAX7219 display drivers.
  24. #define DISPLAY_POWER 0x0C
  25. #define DISPLAY_DECODE_MODE 0x09
  26. #define DISPLAY_INTENSITY 0x0A
  27. #define DISPLAY_SCAN_LIMIT 0x0B
  28. #define DISPLAY_TEST_MODE 0x0F
  29.  
  30. // Also try to place "alphabet" of characters in program memory for efficient character access.
  31. // Font format courtesy of brainybits.ca
  32. unsigned char alphabetArray[][5] PROGMEM =
  33. {
  34. {0x00, 0x00, 0x00, 0x00, 0x00}, // Space character
  35. {0x5F, 0x00, 0x00, 0x00, 0x00}, // ! character
  36. {0x00, 0x03, 0x00, 0x03, 0x00}, // " character
  37. {0x14, 0x3D, 0x14, 0x3D, 0x14}, // # character
  38. {0x24, 0x6A, 0x2B, 0x12, 0x00}, // $ character
  39. {0x63, 0x13, 0x08, 0x64, 0x63} // % character
  40. {0x36, 0x49, 0x56, 0x20, 0x50} // & character
  41. {0x00, 0x00, 0x03, 0x00, 0x00} // ' character
  42. {0x00, 0x1C, 0x22, 0x41, 0x00} // ( character
  43. {0x00, 0x41, 0x22, 0x1C, 0x00} // ) character
  44. {}
  45. };
  46.  
  47. // Used to hold the text to be scrolled across the display.
  48. char displayBuffer[][8] =
  49. {
  50. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // SPACE
  51. {0x00, 0x00, 0x7F, 0x88, 0x88, 0x7F, 0x00, 0x00}, // A
  52. {0x00, 0x00, 0x0F, 0x01, 0x01, 0x0F, 0x00, 0x00}, // u
  53. {0x00, 0x00, 0x46, 0x89, 0x89, 0x72, 0x00, 0x00}, // s
  54. {0x00, 0x00, 0x80, 0x80, 0xFF, 0x80, 0x80, 0x00} // t
  55. };
  56.  
  57. //void initSPI(void);
  58. //void sendSPI(char addr, char data, int moduleQuantity);
  59. //void clearDisplay(int modQuantity)
  60.  
  61. // This function simply sets some direction registers and SPI registers in order to begin prepping the SPI module for use.
  62. void initSPI(void)
  63. {
  64. // Set MOSI and SCK pins as outputs for SPI use.
  65. DDRB |= (1 << PINMOSI) | (1 << PINSCK);
  66.  
  67. // Enable SPI mode, set system to MASTER.
  68. SPCR0 |= (1 << SPE0) | (1 << MSTR0);
  69. }
  70.  
  71. // Function which sends SPI commands to a specified number of display modules.
  72. void sendSPI(char addr, char data, int moduleQuantity)
  73. {
  74. // Ensure that SS pin is output low before transmission.
  75. PORTB &= ~(1 << PINSS);
  76.  
  77. /*
  78. ** Data to be transmitted. First sends address byte, followed by data byte.
  79. ** System must wait for each transmission to complete before proceeding.
  80. */
  81. for(int i = 0; i < moduleQuantity; i++)
  82. {
  83. SPDR0 = addr;
  84. while(!(SPSR0 & (1 << SPIF0)));
  85. SPDR0 = data;
  86. while(!(SPSR0 & (1 << SPIF0)));
  87. }
  88.  
  89. // Pulse SS high to lock in transmitted bits.
  90. PORTB |= (1 << PINSS);
  91. }
  92.  
  93. // Function which handles data to be displayed and translated across the displays.
  94. void sendColumnSPI(char column, char data, int modQuantity)
  95. {
  96. // Ensure that SS pin is output low before transmission.
  97. PORTB &= ~(1 << PINSS);
  98.  
  99. for(int i = 0; i < modQuantity; i++)
  100. {
  101. if(i == (column / 8))
  102. {
  103. SPDR0 = (column % 8) + 1;
  104. while(!(SPSR0 & (1 << SPIF0)));
  105. SPDR0 = data;
  106. while(!(SPSR0 & (1 << SPIF0)));
  107. }
  108. else
  109. {
  110. SPDR0 = 0x0;
  111. while(!(SPSR0 & (1 << SPIF0)));
  112. SPDR0 = 0x0;
  113. while(!(SPSR0 & (1 << SPIF0)));
  114. }
  115. }
  116.  
  117. // Pulse SS high to lock in transmitted bits.
  118. PORTB |= (1 << PINSS);
  119. }
  120.  
  121. // Experimental function. May hopefully reduce overhead of code for text display.
  122. void writeCharacterToDisplay(int currentIndex, int charNum, char textBuffer[5][8])
  123. {
  124. for(int i = 0; i < 8; i++)
  125. {
  126. sendColumnSPI(currentIndex + i, textBuffer[charNum][i], 2);
  127. }
  128. }
  129.  
  130. // Ensures that all leds on the displays are blanked before use.
  131. void clearDisplay(int modQuantity)
  132. {
  133. int addressTemplate = 0b00000000;
  134.  
  135. for(int i = 1; i <= 8; i++)
  136. {
  137. sendSPI((addressTemplate+i), 0x00, modQuantity);
  138. }
  139. }
  140.  
  141. int main(void)
  142. {
  143. //sei(); When needed, this line will activate global interrupts.
  144.  
  145. // Set some general-use pin directions. Note the SS pin is set as output for SPI purposes.
  146. DDRB = (1 << PINB0) | (1 << PINSS);
  147. PORTB = (1 << PINB0);
  148.  
  149. // Set up the SPI module for use.
  150. initSPI();
  151.  
  152. // Instantiate the display with the power turned off.
  153. sendSPI(DISPLAY_POWER, 0x00, 2);
  154. //Deactivate decode mode.
  155. sendSPI(DISPLAY_DECODE_MODE, 0x00, 2);
  156. // Scan limit set for 8 led groups.
  157. sendSPI(DISPLAY_SCAN_LIMIT, 0x07, 2);
  158. // Test mode deactivated.
  159. sendSPI(DISPLAY_TEST_MODE, 0x00, 2);
  160. // Set intensity of display brightness.
  161. sendSPI(DISPLAY_INTENSITY, 0x0A, 2);
  162. // Blank all leds
  163. clearDisplay(2);
  164. // Finally, provide power to the display.
  165. sendSPI(DISPLAY_POWER, 0x01, 2);
  166.  
  167. // Main program loop.
  168. while (1)
  169. {
  170. // Scroll loop condition is based on number of characters to be displayed, multiplied by the overall per-character size.
  171. for(int i = 0; i < (8 * 5); i++)
  172. {
  173. /*clearDisplay(2);
  174. sendColumnSPI(i, displayBuffer[j+1][0], 2); // Should make a display algorithm out of all this!
  175. sendColumnSPI(i+1, displayBuffer[j+1][1], 2);
  176. sendColumnSPI(i+2, displayBuffer[j+1][2], 2);
  177. sendColumnSPI(i+3, displayBuffer[j+1][3], 2);
  178. sendColumnSPI(i+4, displayBuffer[j+1][4], 2);
  179. sendColumnSPI(i+5, displayBuffer[j+1][5], 2);
  180. sendColumnSPI(i+6, displayBuffer[j+1][6], 2);
  181. sendColumnSPI(i+7, displayBuffer[j+1][7], 2);
  182. sendColumnSPI(i+8, displayBuffer[j][0], 2);
  183. sendColumnSPI(i+9, displayBuffer[j][0], 2);
  184. sendColumnSPI(i+10, displayBuffer[j][0], 2);
  185. sendColumnSPI(i+11, displayBuffer[j][0], 2);
  186. sendColumnSPI(i+12, displayBuffer[j][0], 2);
  187. sendColumnSPI(i+13, displayBuffer[j][0], 2);
  188. sendColumnSPI(i+14, displayBuffer[j][0], 2);
  189. sendColumnSPI(i+15, displayBuffer[j][0], 2);
  190. sendColumnSPI(i - 8, displayBuffer[j+1][0], 2);
  191. sendColumnSPI(i - 9, displayBuffer[j+1][1], 2);
  192. sendColumnSPI(i - 10, displayBuffer[j+1][2], 2);
  193. sendColumnSPI(i-11, displayBuffer[j+1][3], 2);
  194. sendColumnSPI(i-12, displayBuffer[j+1][4], 2);
  195. sendColumnSPI(i-13, displayBuffer[j+1][5], 2);
  196. sendColumnSPI(i-14, displayBuffer[j+1][6], 2);
  197. sendColumnSPI(i-15, displayBuffer[j+1][7], 2);
  198. _delay_ms(750); Basically obsolete at this point - only keeping it around for reference purposes.*/
  199.  
  200. //writeCharacterToDisplay(i - 0, 0, displayBuffer);
  201. //writeCharacterToDisplay(i - 6, 1, displayBuffer);
  202. //writeCharacterToDisplay(i - 12, 2, displayBuffer);
  203. //writeCharacterToDisplay(i - 18, 3, displayBuffer);
  204. //writeCharacterToDisplay(i - 24, 4, displayBuffer);
  205. //writeCharacterToDisplay(i - 30, 0, displayBuffer);
  206.  
  207. // Iterate through the various characters to be displayed, and send them to the display.
  208. for(int k = 0; k <= 30; k+=6)
  209. {
  210. writeCharacterToDisplay(i - k, k % 5, displayBuffer);
  211. }
  212.  
  213. _delay_ms(500);
  214. }
  215.  
  216. if (PORTB & (1 << PINB0))
  217. {
  218. PORTB ^= (1 << PINB0);
  219. }
  220. else
  221. {
  222. PORTB ^= (1 << PINB0);
  223. }
  224. }
  225.  
  226. return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement