Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define COLUMNS 16
- #define HEIGHT 128
- #define WIDTH 16
- long long array[2][68];
- // Connections to board
- const byte latchPin=8;
- const byte clockPin=12;
- const byte data_R1=10;
- const byte data_R2=11;
- const byte en_74138=2;
- const byte la_74138=3;
- const byte lb_74138=4;
- const byte lc_74138=5;
- const byte ld_74138=6;
- byte ScanRow = 0;
- unsigned long counter;
- #define latchPinPORTB latchPin - 8
- int n = 0;
- byte buffer[128*2] = { // Display buffer (which is scanned by the interrupt timer) of 8x32 bytes or 16x16 bytes
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- };
- // set a single pixel on or off
- void HardwarePlot(byte y, byte x, byte colour) {
- bitWrite(buffer[(y*COLUMNS)+(x>>3)],7-(x&7),colour); };
- void shiftOut(byte row) { // fast routine to shove out 8 columns into two rows via board's shift registers
- byte rtimesc = row*COLUMNS;
- for(byte column=0;column<COLUMNS;column++) {
- byte index = column + rtimesc;
- for(byte i=0;i<8;i++) {
- PORTB &=~(3<<(data_R1-8)); // data_R2 is LOW; data_R1 is LOW;
- PORTB &=~(1<<(clockPin-8)); // digitalWrite(clockPin,LOW);
- PORTB |= !((buffer[index]>>(7-i)) & 0x01) << (data_R1-8); // top set of rows
- PORTB |= 1<<(clockPin-8); // digitalWrite(clockPin,HIGH);
- };
- };
- };
- // Scan a pair of rows on to the display from "buffer" via the interrupt
- ISR(TIMER2_COMPA_vect){
- cli();
- digitalWrite(en_74138, HIGH); // Turn off display
- shiftOut(ScanRow); // Shift out 8 (or 16) columns
- digitalWrite(latchPin, LOW);
- digitalWrite(latchPin, HIGH);
- PORTD = (ScanRow << 3) | (PORTD & 0X87); // Highlight row: pins 3 4 5 6 (la_74138 lb_74138 lc_74138 ld_74138)
- digitalWrite(en_74138, LOW); // Turn on display
- ScanRow++; // Do the next pair of rows next time this routine is called
- ScanRow &= 0x0F;
- sei();
- };
- void setup() {
- cli(); // clear interrupts
- TCCR2A = 0; TCCR2B = 0; TCNT2 = 0;
- TCCR2B |= (1 << CS12) | (1 << CS10); // Set 1024 prescaler
- // 160Hz scan rate = 10 frames/second = 16 (pairs of) row(s)/second
- OCR2A = 100; // 97 = (16,000,000 / (1024*160)) - 1
- TCCR2A |= (1 << WGM21); TIMSK2 |= (1 << OCIE2A);
- pinMode(latchPin,OUTPUT); pinMode(clockPin,OUTPUT);
- pinMode(data_R1,OUTPUT); pinMode(data_R2,OUTPUT);
- pinMode(en_74138,OUTPUT);
- pinMode(la_74138,OUTPUT); pinMode(lb_74138,OUTPUT);
- pinMode(lc_74138,OUTPUT); pinMode(ld_74138,OUTPUT);
- digitalWrite(en_74138, LOW);
- digitalWrite(data_R1, HIGH); digitalWrite(data_R2, HIGH);
- sei(); //allow interrupts
- };
- // Note that there's no need to do anything with the screen in the main loop.
- // Whatever's in "buffer" is constantly scanned out.
- void loop() {
- cm5();
- };
- byte getPixel(byte y, byte x, byte whichArray){
- unsigned long long temp = (1ULL << y);
- if(array[whichArray][x] & temp){
- return 1; //the yth bit was set, so return 1.
- }
- else {
- return 0; //the yth bit was clear, so return 0;
- }
- }
- #define RNUM_SEED 0xBAD
- static uint16_t rnum = RNUM_SEED;
- void paintCM5(){
- int i, j;bool pixel;
- for(i=0; i<WIDTH; i++ ) { for(j=0; j<32; j++) {
- pixel=getPixel(i,j,0);
- if (j & 4) { if (pixel==0 ) {
- HardwarePlot(i,j,0);
- if (j+111<127) HardwarePlot(i,j+111,0);
- HardwarePlot(i,j+37,0);
- HardwarePlot(i,j+74,0);
- } else {
- HardwarePlot(i,j,1);
- if (j+111<127)HardwarePlot(i,j+111,1);
- HardwarePlot(i,j+37,1);
- HardwarePlot(i,j+74,1);
- }
- } else
- if (pixel==0 ) {
- HardwarePlot(WIDTH-i-1,j,0);
- if (j+111<127)HardwarePlot(WIDTH-i-1,j+111,0);
- HardwarePlot(WIDTH-i-1,j+37,0);
- HardwarePlot(WIDTH-i-1,j+74,0); }
- else {
- HardwarePlot(WIDTH-i-1,j,1);
- if (j+111<127)HardwarePlot(WIDTH-i-1,j+111,1);
- HardwarePlot(WIDTH-i-1,j+37,1);
- HardwarePlot(WIDTH-i-1,j+74,1);
- }
- }
- }
- }
- static uint16_t get_random_bit(void)
- {
- #define X rnum
- uint16_t lfsr_bit = ((X >> 0) ^ (X >> 1) ^ (X >> 3) ^ (X >> 12)) & 1;
- uint16_t rand_bit = (X | (X >> 2)) & 1;
- #undef X
- rnum = (lfsr_bit << 15) | (rnum >> 1);
- return rand_bit;
- }
- void setPixel(byte y, byte x, byte whichArray, byte pixel){
- unsigned long long temp = (1ULL << y);
- if (pixel){
- array[whichArray][x] |= temp;
- }
- else {
- array[whichArray][x] &= ~temp;
- }
- }
- void setPixelunsafe(byte y, byte x, byte whichArray, byte pixel){
- unsigned long long temp = (1ULL << y);
- if (pixel){
- array[whichArray][x] |= temp;
- }
- else {
- array[whichArray][x] &= ~temp;
- }
- }
- void cm5() {
- uint16_t X=1,X2,c;
- int pixel,i,j;
- //cm5 mode 7
- for(j=31; j>=0; j-- ){
- uint16_t bit = get_random_bit();
- if (bit==0) { setPixelunsafe(WIDTH,j,0,1);
- }
- }
- for(j=0; j<32; j++) { for(i=0; i<WIDTH+1;i++){
- pixel=getPixel(i+1,j,0); if(pixel==1)setPixel(i,j,0,1);else setPixel(i,j,0,0); } }
- paintCM5();
- delay(200);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement