Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1.  
  2.  
  3. #define COLUMNS 16
  4. #define HEIGHT 128
  5. #define WIDTH 16
  6.  
  7. long long array[2][68];
  8.  
  9. // Connections to board
  10. const byte latchPin=8;
  11. const byte clockPin=12;
  12. const byte data_R1=10;
  13. const byte data_R2=11;
  14. const byte en_74138=2;
  15. const byte la_74138=3;
  16. const byte lb_74138=4;
  17. const byte lc_74138=5;
  18. const byte ld_74138=6;
  19.  
  20. byte ScanRow = 0;
  21. unsigned long counter;
  22. #define latchPinPORTB latchPin - 8
  23. int n = 0;
  24.  
  25. byte buffer[128*2] = { // Display buffer (which is scanned by the interrupt timer) of 8x32 bytes or 16x16 bytes
  26. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  30. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34.  
  35. };
  36.  
  37. // set a single pixel on or off
  38. void HardwarePlot(byte y, byte x, byte colour) {
  39.  
  40. bitWrite(buffer[(y*COLUMNS)+(x>>3)],7-(x&7),colour); };
  41.  
  42. void shiftOut(byte row) { // fast routine to shove out 8 columns into two rows via board's shift registers
  43. byte rtimesc = row*COLUMNS;
  44. for(byte column=0;column<COLUMNS;column++) {
  45. byte index = column + rtimesc;
  46. for(byte i=0;i<8;i++) {
  47. PORTB &=~(3<<(data_R1-8)); // data_R2 is LOW; data_R1 is LOW;
  48. PORTB &=~(1<<(clockPin-8)); // digitalWrite(clockPin,LOW);
  49. PORTB |= !((buffer[index]>>(7-i)) & 0x01) << (data_R1-8); // top set of rows
  50.  
  51. PORTB |= 1<<(clockPin-8); // digitalWrite(clockPin,HIGH);
  52. };
  53. };
  54. };
  55.  
  56. // Scan a pair of rows on to the display from "buffer" via the interrupt
  57. ISR(TIMER2_COMPA_vect){
  58. cli();
  59. digitalWrite(en_74138, HIGH); // Turn off display
  60. shiftOut(ScanRow); // Shift out 8 (or 16) columns
  61. digitalWrite(latchPin, LOW);
  62. digitalWrite(latchPin, HIGH);
  63.  
  64. PORTD = (ScanRow << 3) | (PORTD & 0X87); // Highlight row: pins 3 4 5 6 (la_74138 lb_74138 lc_74138 ld_74138)
  65. digitalWrite(en_74138, LOW); // Turn on display
  66. ScanRow++; // Do the next pair of rows next time this routine is called
  67. ScanRow &= 0x0F;
  68. sei();
  69.  
  70. };
  71.  
  72. void setup() {
  73.  
  74. cli(); // clear interrupts
  75. TCCR2A = 0; TCCR2B = 0; TCNT2 = 0;
  76. TCCR2B |= (1 << CS12) | (1 << CS10); // Set 1024 prescaler
  77. // 160Hz scan rate = 10 frames/second = 16 (pairs of) row(s)/second
  78. OCR2A = 100; // 97 = (16,000,000 / (1024*160)) - 1
  79. TCCR2A |= (1 << WGM21); TIMSK2 |= (1 << OCIE2A);
  80.  
  81. pinMode(latchPin,OUTPUT); pinMode(clockPin,OUTPUT);
  82. pinMode(data_R1,OUTPUT); pinMode(data_R2,OUTPUT);
  83.  
  84. pinMode(en_74138,OUTPUT);
  85. pinMode(la_74138,OUTPUT); pinMode(lb_74138,OUTPUT);
  86. pinMode(lc_74138,OUTPUT); pinMode(ld_74138,OUTPUT);
  87.  
  88. digitalWrite(en_74138, LOW);
  89. digitalWrite(data_R1, HIGH); digitalWrite(data_R2, HIGH);
  90. sei(); //allow interrupts
  91. };
  92.  
  93.  
  94. // Note that there's no need to do anything with the screen in the main loop.
  95. // Whatever's in "buffer" is constantly scanned out.
  96. void loop() {
  97.  
  98. cm5();
  99.  
  100. };
  101.  
  102.  
  103. byte getPixel(byte y, byte x, byte whichArray){
  104.  
  105. unsigned long long temp = (1ULL << y);
  106. if(array[whichArray][x] & temp){
  107. return 1; //the yth bit was set, so return 1.
  108. }
  109. else {
  110. return 0; //the yth bit was clear, so return 0;
  111. }
  112. }
  113.  
  114.  
  115.  
  116. #define RNUM_SEED 0xBAD
  117. static uint16_t rnum = RNUM_SEED;
  118.  
  119. void paintCM5(){
  120. int i, j;bool pixel;
  121.  
  122. for(i=0; i<WIDTH; i++ ) { for(j=0; j<32; j++) {
  123.  
  124. pixel=getPixel(i,j,0);
  125.  
  126. if (j & 4) { if (pixel==0 ) {
  127.  
  128. HardwarePlot(i,j,0);
  129. if (j+111<127) HardwarePlot(i,j+111,0);
  130. HardwarePlot(i,j+37,0);
  131. HardwarePlot(i,j+74,0);
  132.  
  133.  
  134. } else {
  135.  
  136. HardwarePlot(i,j,1);
  137. if (j+111<127)HardwarePlot(i,j+111,1);
  138. HardwarePlot(i,j+37,1);
  139. HardwarePlot(i,j+74,1);
  140.  
  141. }
  142.  
  143. } else
  144.  
  145. if (pixel==0 ) {
  146.  
  147. HardwarePlot(WIDTH-i-1,j,0);
  148. if (j+111<127)HardwarePlot(WIDTH-i-1,j+111,0);
  149. HardwarePlot(WIDTH-i-1,j+37,0);
  150. HardwarePlot(WIDTH-i-1,j+74,0); }
  151.  
  152. else {
  153. HardwarePlot(WIDTH-i-1,j,1);
  154. if (j+111<127)HardwarePlot(WIDTH-i-1,j+111,1);
  155. HardwarePlot(WIDTH-i-1,j+37,1);
  156. HardwarePlot(WIDTH-i-1,j+74,1);
  157.  
  158. }
  159.  
  160. }
  161.  
  162. }
  163.  
  164. }
  165.  
  166. static uint16_t get_random_bit(void)
  167. {
  168. #define X rnum
  169.  
  170. uint16_t lfsr_bit = ((X >> 0) ^ (X >> 1) ^ (X >> 3) ^ (X >> 12)) & 1;
  171.  
  172. uint16_t rand_bit = (X | (X >> 2)) & 1;
  173.  
  174. #undef X
  175.  
  176. rnum = (lfsr_bit << 15) | (rnum >> 1);
  177.  
  178. return rand_bit;
  179.  
  180. }
  181.  
  182. void setPixel(byte y, byte x, byte whichArray, byte pixel){
  183.  
  184. unsigned long long temp = (1ULL << y);
  185. if (pixel){
  186. array[whichArray][x] |= temp;
  187. }
  188. else {
  189. array[whichArray][x] &= ~temp;
  190. }
  191.  
  192. }
  193.  
  194. void setPixelunsafe(byte y, byte x, byte whichArray, byte pixel){
  195.  
  196. unsigned long long temp = (1ULL << y);
  197. if (pixel){
  198. array[whichArray][x] |= temp;
  199. }
  200. else {
  201. array[whichArray][x] &= ~temp;
  202. }
  203.  
  204. }
  205.  
  206. void cm5() {
  207. uint16_t X=1,X2,c;
  208. int pixel,i,j;
  209.  
  210. //cm5 mode 7
  211.  
  212. for(j=31; j>=0; j-- ){
  213.  
  214. uint16_t bit = get_random_bit();
  215.  
  216. if (bit==0) { setPixelunsafe(WIDTH,j,0,1);
  217.  
  218. }
  219. }
  220.  
  221. for(j=0; j<32; j++) { for(i=0; i<WIDTH+1;i++){
  222. pixel=getPixel(i+1,j,0); if(pixel==1)setPixel(i,j,0,1);else setPixel(i,j,0,0); } }
  223.  
  224.  
  225. paintCM5();
  226. delay(200);
  227.  
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement