Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author: Phil Kaziewicz 19th July 2014
- // 64x32 LED display matrix test code
- // based upon original code from canton-electonics
- // Arduino 1.0.5 UNO R3
- #include <avr/pgmspace.h>
- //Ikeliama RTC library
- #include "Wire.h"
- #define DS1307_ADDRESS 0x68
- // 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;
- volatile uint8_t tukstantis, simtai, desimtys, vienetai;
- const char message[] = "Austeja ir Genute !!! ";
- //Ikeliamas papildomos funkcijos
- #include "sriftas.h";
- byte buffer[256] = { // Display buffer (which is scanned by the interrupt timer) of 8x32 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,
- 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,
- };
- // 16 * 8 digital font
- const uint8_t digitals[] = {
- 0x00, 0x1C, 0x36, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00, 0x00, 0x00, 0x00, // 0
- 0x00, 0x18, 0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7E, 0x00, 0x00, 0x00, 0x00, // 1
- 0x00, 0x3E, 0x63, 0x63, 0x63, 0x06, 0x06, 0x0C, 0x18, 0x30, 0x63, 0x7F, 0x00, 0x00, 0x00, 0x00, // 2
- 0x00, 0x3E, 0x63, 0x63, 0x06, 0x1C, 0x06, 0x03, 0x03, 0x63, 0x66, 0x3C, 0x00, 0x00, 0x00, 0x00, // 3
- 0x00, 0x06, 0x0E, 0x1E, 0x36, 0x36, 0x66, 0x66, 0x7F, 0x06, 0x06, 0x1F, 0x00, 0x00, 0x00, 0x00, // 4
- 0x00, 0x7F, 0x60, 0x60, 0x60, 0x7C, 0x76, 0x03, 0x03, 0x63, 0x66, 0x3C, 0x00, 0x00, 0x00, 0x00, // 5
- 0x00, 0x1E, 0x36, 0x60, 0x60, 0x7C, 0x76, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00, 0x00, 0x00, 0x00, // 6
- 0x00, 0x7F, 0x66, 0x66, 0x0C, 0x0C, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, // 7
- 0x00, 0x3E, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x36, 0x63, 0x63, 0x63, 0x3E, 0x00, 0x00, 0x00, 0x00, // 8
- 0x00, 0x1C, 0x36, 0x63, 0x63, 0x63, 0x37, 0x1F, 0x03, 0x03, 0x36, 0x3C, 0x00, 0x00, 0x00, 0x00, // 9
- };
- // Routine to print a single character in one of 8 columns
- // Inputs:
- // x is one of (0,16,24,32,40,48,56),
- // y (0 to 16 or 24 depending upon font size),
- // n is either (0 to 9) or an ascii value,
- // font=0 for big font, 2 for small font,
- // inverse is true for an inverted character
- void drawChar(uint16_t x, uint16_t y, byte n, byte font, boolean inverse) {
- byte charbytes[16], fontrows, xover8 = x>>3;
- int index;
- if (0 != (x % 8)) return; // x not a multiple of 8
- if ((n>9) && (n<32)) return; // invalid character
- if (font==2) fontrows=16; else fontrows=8;
- if ((n>=0) && (n<=9)) index=(n+16)*fontrows; else index=(n-32)*fontrows; // go to the right code for this character
- // addressing start at buffer and add y (rows) * (WIDTH is 64 so WIDTH/8) is 8 plus (x / 8) is 0 to 7
- byte *pDst = buffer + (y << 3) + xover8;
- for (byte i=0;i<fontrows;i++) { // fill up the charbytes array with the right bits
- if (font==0) charbytes[i] = pgm_read_byte(&(font8x8_basic[index+i]));
- // if (font==1) charbytes[i] = pgm_read_byte(&(font8x8_extended[index+i]));
- if (font==2) charbytes[i] = pgm_read_byte(&(font8x16_basic[index+i]));
- // reverse bit order for fonts 0 and 1
- if (font != 2) {
- charbytes[i] = (charbytes[i] & 0xF0) >> 4 | (charbytes[i] & 0x0F) << 4;
- charbytes[i] = (charbytes[i] & 0xCC) >> 2 | (charbytes[i] & 0x33) << 2;
- charbytes[i] = (charbytes[i] & 0xAA) >> 1 | (charbytes[i] & 0x55) << 1;
- };
- if (inverse) charbytes[i] = ~charbytes[i];
- };
- const byte *pSrc = charbytes; // point at the first set of 8 pixels
- for (byte i=0; i<fontrows; i++) {
- *pDst = *pSrc; // populate the destination byte
- pDst += 8; // go to next row on buffer
- pSrc++; // go to next set of 8 pixels in character
- }
- };
- void moveLeft(byte pixels, byte rowstart, byte rowstop) { // routine to move certain rows on the screen "pixels" pixels to the left
- byte row, column;
- short unsigned int address;
- for (column=0;column<8;column++) {
- for (row=rowstart;row<rowstop;row++) {
- address = (row<<3) + column; /// right here!
- if (column==7)
- buffer[address] = buffer[address]<<pixels; // shuffle pixels left on last column and fill with a blank
- else { // shuffle pixels left and add leftmost pixels from next column
- byte incomingchar = buffer[address+1];
- buffer[address] = buffer[address]<<pixels;
- for (byte x=0;x<pixels;x++) { buffer[address] += ((incomingchar & (128>>x)) >> (7-x))<<(pixels-x-1); };
- }
- }
- }
- };
- // set a single pixel on or off
- void setPixel(byte x, byte y, byte colour) { bitWrite(buffer[(y<<3)+(x>>3)],7-(x&7),colour); };
- void drawRect(byte x1, byte y1, byte x2, byte y2, byte colour) {
- for (byte x=x1;x<=x2;x++) { setPixel(x,y1,colour); setPixel(x,y2,colour); };
- for (byte y=y1;y<=y2;y++) { setPixel(x1,y,colour); setPixel(x2,y,colour); };
- };
- void shiftOut(byte row) { // fast routine to shove out 8 columns into two rows via board's shift registers
- for(byte column=0;column<8;column++) {
- byte index = column + (row<<3);
- 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 |= !((~buffer[index+128]>>(7-i)) & 0x01) << (data_R2-8); // bottom set of rows
- PORTB |= 1<<(clockPin-8); // digitalWrite(clockPin,HIGH);
- };
- };
- };
- void drawPoint(uint16_t x, uint16_t y, uint8_t pixel){
- uint8_t *byte = buffer + x / 8 + y * 64 / 8;
- uint8_t bit = x % 8;
- if (pixel) {
- *byte |= 0x80 >> bit;
- } else {
- *byte &= ~(0x80 >> bit);
- }}
- // (x, y) top-left position, x should be multiple of 8
- void drawDigital(uint16_t x, uint16_t y, uint8_t n)
- {
- if ((n >= 10) || (0 != (x % 8))) {
- return;
- }
- uint8_t *pDst = buffer + y * (64 / 8) + x / 8;
- const uint8_t *pSrc = digitals + n * 16;
- for (uint8_t i = 0; i < 16; i++) {
- *pDst = *pSrc;
- pDst += 64 / 8;
- pSrc++;
- }
- }
- void clear(){
- uint8_t *ptr = buffer;
- for (uint16_t i = 0; i < (64 * 32 / 8); i++) {
- *ptr = 0x00;
- ptr++;
- }
- }
- // 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 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
- // if (ScanRow==16) ScanRow=0;
- // sei();
- //};
- byte bcdToDec(byte val) {return ( (val/16*10) + (val%16) );}
- void setup() {
- // Set up Timer2 as the scanning interrupt timer
- //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 rows)
- //OCR2A =97; // 97 = (16,000,000 / (1024*160)) - 1
- // TCCR2A |= (1 << WGM21); TIMSK2 |= (1 << OCIE2A);
- Wire.begin();
- 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);
- counter = millis();
- //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() {
- static int count=0;
- Wire.beginTransmission(DS1307_ADDRESS);
- byte zero = 0x00;
- Wire.write(zero);
- Wire.endTransmission();
- Wire.requestFrom(DS1307_ADDRESS, 7);
- int second = bcdToDec(Wire.read());
- int minute = bcdToDec(Wire.read());
- int hour = bcdToDec(Wire.read() & 0b111111);
- //int weekDay = bcdToDec(Wire.read());
- //int monthDay = bcdToDec(Wire.read());
- //int month = bcdToDec(Wire.read());
- //int year = bcdToDec(Wire.read());
- int taskas=second;
- //minutes
- minute = minute % 100;
- int min_d = minute/10;
- minute = minute % 10;
- int min_v = minute;
- //sekundes
- second = second % 100;
- int sec_d = second/10;
- second = second % 10;
- int sec_v = second;
- //valandos
- hour = hour % 100;
- int val_d = hour/10;
- hour = hour % 10;
- int val_v = hour;
- clear();
- // display clock
- //drawChar(0,0,val_d,2,false);
- drawDigital(0,0,val_d);
- //drawChar(8,0,val_v,2,false);
- drawDigital(8,0,val_v);
- //drawChar(16,0,'.',2,false);
- //drawChar(24,0,min_d,2,false);
- drawDigital(24,0,min_d);
- //drawChar(32,0,min_v,2,false);
- drawDigital(32,0,min_v);
- //rawChar(40,0,'.',2,false);
- //drawChar(48,5,sec_d,0,false);
- drawDigital(48,0,sec_d);
- //rawChar(56,5,sec_v,0,false);
- drawDigital(56,0,sec_v);
- //setPixel(taskas+2,0,1);
- //setPixel(taskas+2,1,1);
- drawPoint(taskas+2,15+sec_v,1);
- drawPoint(taskas+2,14+sec_v,1);
- drawPoint(taskas+3,14+sec_v,1);
- drawPoint(taskas+3,15+sec_v,1);
- //seconds = (clock/20)%10;
- //colour = (clock/200)&1;
- // draw a rectangle
- //drawRect(0,0,63,16,1);
- //drawRect(seconds*5+1,19,seconds*5+3,22,!colour);
- // draw a blob
- //setPixel(56,20,colour); setPixel(57,20,colour); setPixel(58,20,colour);
- //setPixel(57,19,colour); setPixel(57,21,colour);
- // display next character of message
- //drawChar(56,20,message[count%(sizeof(message)-1)],0,false);
- //count = (count + 1);
- // move the text 7 pixels (not 8 because it looks better) to the left
- //for (byte i=0; i<7; i++) {
- //delay(100);
- //moveLeft(1,20,32);
- //};
- digitalWrite(en_74138, HIGH); // Turn off display
- shiftOut(ScanRow); // Shift out 8 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
- if (ScanRow==16) ScanRow=0;
- //delay(200);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement