Advertisement
Guest User

Untitled

a guest
Jan 1st, 2016
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.17 KB | None | 0 0
  1. // Author: Phil Kaziewicz 19th July 2014
  2. // 64x32 LED display matrix test code
  3. // based upon original code from canton-electonics
  4. // Arduino 1.0.5 UNO R3
  5. #include <avr/pgmspace.h>
  6. //Ikeliama RTC library
  7. #include "Wire.h"
  8. #define DS1307_ADDRESS 0x68
  9.  
  10. // Connections to board
  11. const byte latchPin=8;
  12. const byte clockPin=12;
  13. const byte data_R1=10;
  14. const byte data_R2=11;
  15. const byte en_74138=2;
  16. const byte la_74138=3;
  17. const byte lb_74138=4;
  18. const byte lc_74138=5;
  19. const byte ld_74138=6;
  20. byte ScanRow = 0;
  21. unsigned long counter;
  22.  
  23. volatile uint8_t tukstantis, simtai, desimtys, vienetai;
  24.  
  25. const char message[] = "Austeja ir Genute !!! ";
  26.  
  27. //Ikeliamas papildomos funkcijos
  28. #include "sriftas.h";
  29.  
  30. byte buffer[256] = { // Display buffer (which is scanned by the interrupt timer) of 8x32 bytes
  31.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63. };
  64.  
  65. // 16 * 8 digital font
  66. const uint8_t digitals[] = {
  67.     0x00, 0x1C, 0x36, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00, 0x00, 0x00, 0x00, // 0
  68.     0x00, 0x18, 0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7E, 0x00, 0x00, 0x00, 0x00, // 1
  69.     0x00, 0x3E, 0x63, 0x63, 0x63, 0x06, 0x06, 0x0C, 0x18, 0x30, 0x63, 0x7F, 0x00, 0x00, 0x00, 0x00, // 2
  70.     0x00, 0x3E, 0x63, 0x63, 0x06, 0x1C, 0x06, 0x03, 0x03, 0x63, 0x66, 0x3C, 0x00, 0x00, 0x00, 0x00, // 3
  71.     0x00, 0x06, 0x0E, 0x1E, 0x36, 0x36, 0x66, 0x66, 0x7F, 0x06, 0x06, 0x1F, 0x00, 0x00, 0x00, 0x00, // 4
  72.     0x00, 0x7F, 0x60, 0x60, 0x60, 0x7C, 0x76, 0x03, 0x03, 0x63, 0x66, 0x3C, 0x00, 0x00, 0x00, 0x00, // 5
  73.     0x00, 0x1E, 0x36, 0x60, 0x60, 0x7C, 0x76, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00, 0x00, 0x00, 0x00, // 6
  74.     0x00, 0x7F, 0x66, 0x66, 0x0C, 0x0C, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, // 7
  75.     0x00, 0x3E, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x36, 0x63, 0x63, 0x63, 0x3E, 0x00, 0x00, 0x00, 0x00, // 8
  76.     0x00, 0x1C, 0x36, 0x63, 0x63, 0x63, 0x37, 0x1F, 0x03, 0x03, 0x36, 0x3C, 0x00, 0x00, 0x00, 0x00, // 9
  77. };
  78.  
  79. // Routine to print a single character in one of 8 columns
  80. // Inputs:
  81. // x is one of (0,16,24,32,40,48,56),
  82. // y (0 to 16 or 24 depending upon font size),
  83. // n is either (0 to 9) or an ascii value,
  84. // font=0 for big font, 2 for small font,
  85. // inverse is true for an inverted character
  86. void drawChar(uint16_t x, uint16_t y, byte n, byte font, boolean inverse) {
  87.     byte charbytes[16], fontrows, xover8 = x>>3;
  88.     int index;
  89.     if (0 != (x % 8)) return;    // x not a multiple of 8
  90.     if ((n>9) && (n<32)) return; // invalid character
  91.     if (font==2) fontrows=16; else fontrows=8;
  92.     if ((n>=0) && (n<=9)) index=(n+16)*fontrows; else index=(n-32)*fontrows; // go to the right code for this character
  93.  
  94. // addressing start at buffer and add y (rows) * (WIDTH is 64 so WIDTH/8) is 8 plus (x / 8) is 0 to 7
  95.     byte *pDst = buffer + (y << 3) + xover8;
  96.     for (byte i=0;i<fontrows;i++) {  // fill up the charbytes array with the right bits
  97.       if (font==0) charbytes[i] = pgm_read_byte(&(font8x8_basic[index+i]));
  98. //      if (font==1) charbytes[i] = pgm_read_byte(&(font8x8_extended[index+i]));
  99.       if (font==2) charbytes[i] = pgm_read_byte(&(font8x16_basic[index+i]));
  100.       // reverse bit order for fonts 0 and 1
  101.       if (font != 2) {
  102.         charbytes[i] = (charbytes[i] & 0xF0) >> 4 | (charbytes[i] & 0x0F) << 4;
  103.         charbytes[i] = (charbytes[i] & 0xCC) >> 2 | (charbytes[i] & 0x33) << 2;
  104.         charbytes[i] = (charbytes[i] & 0xAA) >> 1 | (charbytes[i] & 0x55) << 1;
  105.       };
  106.       if (inverse) charbytes[i] = ~charbytes[i];
  107.       };
  108.     const byte *pSrc = charbytes; // point at the first set of 8 pixels    
  109.     for (byte i=0; i<fontrows; i++) {
  110.         *pDst = *pSrc;     // populate the destination byte
  111.         pDst += 8;         // go to next row on buffer
  112.         pSrc++;            // go to next set of 8 pixels in character
  113.     }
  114. };
  115.  
  116. void moveLeft(byte pixels, byte rowstart, byte rowstop) { // routine to move certain rows on the screen "pixels" pixels to the left
  117. byte row, column;
  118. short unsigned int address;
  119.   for (column=0;column<8;column++) {
  120.     for (row=rowstart;row<rowstop;row++) {
  121.       address = (row<<3) + column; /// right here!
  122.       if (column==7)
  123.          buffer[address] = buffer[address]<<pixels; // shuffle pixels left on last column and fill with a blank
  124.         else {                // shuffle pixels left and add leftmost pixels from next column
  125.          byte incomingchar = buffer[address+1];
  126.          buffer[address] = buffer[address]<<pixels;
  127.          for (byte x=0;x<pixels;x++) { buffer[address] += ((incomingchar & (128>>x)) >> (7-x))<<(pixels-x-1); };
  128.          }
  129.      }
  130.   }
  131. };
  132.  
  133. // set a single pixel on or off
  134. void setPixel(byte x, byte y, byte colour) { bitWrite(buffer[(y<<3)+(x>>3)],7-(x&7),colour); };
  135.  
  136. void drawRect(byte x1, byte y1, byte x2, byte y2, byte colour) {
  137.   for (byte x=x1;x<=x2;x++) { setPixel(x,y1,colour); setPixel(x,y2,colour); };
  138.   for (byte y=y1;y<=y2;y++) { setPixel(x1,y,colour); setPixel(x2,y,colour); };
  139. };
  140.  
  141. void shiftOut(byte row) { // fast routine to shove out 8 columns into two rows via board's shift registers
  142.   for(byte column=0;column<8;column++) {
  143.     byte index = column + (row<<3);  
  144.     for(byte i=0;i<8;i++) {    
  145.       PORTB &=~(3<<(data_R1-8));                                 // data_R2 is LOW; data_R1 is LOW;
  146.       PORTB &=~(1<<(clockPin-8));                                // digitalWrite(clockPin,LOW);
  147.       PORTB |= !((~buffer[index]>>(7-i)) & 0x01) << (data_R1-8);  // top set of rows
  148.       PORTB |= !((~buffer[index+128]>>(7-i)) & 0x01) << (data_R2-8); // bottom set of rows
  149.       PORTB |= 1<<(clockPin-8);                                  // digitalWrite(clockPin,HIGH);
  150.     };
  151.   };
  152. };
  153.  
  154. void drawPoint(uint16_t x, uint16_t y, uint8_t pixel){
  155.  
  156.     uint8_t *byte = buffer + x / 8 + y * 64 / 8;
  157.     uint8_t  bit = x % 8;
  158.  
  159.     if (pixel) {
  160.         *byte |= 0x80 >> bit;
  161.     } else {
  162.         *byte &= ~(0x80 >> bit);
  163.     }}
  164.    
  165. // (x, y) top-left position, x should be multiple of 8
  166. void drawDigital(uint16_t x, uint16_t y, uint8_t n)
  167. {
  168.     if ((n >= 10) || (0 != (x % 8))) {
  169.         return;
  170.     }
  171.  
  172.     uint8_t *pDst = buffer + y * (64 / 8) + x / 8;
  173.     const uint8_t *pSrc = digitals + n * 16;
  174.     for (uint8_t i = 0; i < 16; i++) {
  175.         *pDst = *pSrc;
  176.         pDst += 64 / 8;
  177.         pSrc++;
  178.     }
  179. }
  180.  
  181. void clear(){
  182.     uint8_t *ptr = buffer;
  183.     for (uint16_t i = 0; i < (64 * 32 / 8); i++) {
  184.         *ptr = 0x00;
  185.         ptr++;
  186.     }
  187. }
  188.  
  189. // Scan a pair of rows on to the display from "buffer" via the interrupt
  190. //ISR(TIMER2_COMPA_vect){
  191.      //cli();
  192.      //digitalWrite(en_74138, HIGH); // Turn off display
  193.      //shiftOut(ScanRow);                // Shift out 8 columns
  194.      //digitalWrite(latchPin, LOW);
  195.      //digitalWrite(latchPin, HIGH);
  196.      //PORTD = (ScanRow << 3) | (PORTD & 0X87); // Highlight row: pins 3 4 5 6 (la_74138 lb_74138 lc_74138 ld_74138)
  197.      //digitalWrite(en_74138, LOW);     // Turn on display
  198.      //ScanRow++;                       // Do the next pair of rows next time this routine is called
  199.     // if (ScanRow==16) ScanRow=0;
  200.      
  201.     // sei();
  202.      
  203. //};
  204.  
  205. byte bcdToDec(byte val) {return ( (val/16*10) + (val%16) );}
  206.  
  207.  
  208. void setup() {
  209.  
  210. // Set up Timer2 as the scanning interrupt timer
  211.   //cli(); // clear interrupts
  212.   //TCCR2A = 0; TCCR2B = 0; TCNT2  = 0;
  213.   //TCCR2B |= (1 << CS12) | (1 << CS10);    // Set 1024 prescaler
  214.                // 160Hz scan rate = 10 frames/second (16 pairs of rows)
  215.   //OCR2A =97;  // 97 = (16,000,000 / (1024*160)) - 1
  216.  // TCCR2A |= (1 << WGM21); TIMSK2 |= (1 << OCIE2A);
  217.  
  218.   Wire.begin();
  219.  
  220.   pinMode(latchPin,OUTPUT);  pinMode(clockPin,OUTPUT);
  221.   pinMode(data_R1,OUTPUT);   pinMode(data_R2,OUTPUT);
  222.    
  223.   pinMode(en_74138,OUTPUT);
  224.   pinMode(la_74138,OUTPUT);  pinMode(lb_74138,OUTPUT);
  225.   pinMode(lc_74138,OUTPUT);  pinMode(ld_74138,OUTPUT);
  226.  
  227.   digitalWrite(en_74138, LOW);
  228.   digitalWrite(data_R1, HIGH); digitalWrite(data_R2, HIGH);
  229.   counter = millis();
  230.   //sei(); //allow interrupts
  231. };
  232.  
  233. // Note that there's no need to do anything with the screen in the main loop.
  234. // Whatever's in "buffer" is constantly scanned out.
  235. void loop() {
  236. static int count=0;
  237. Wire.beginTransmission(DS1307_ADDRESS);
  238. byte zero = 0x00;
  239. Wire.write(zero);
  240. Wire.endTransmission();
  241. Wire.requestFrom(DS1307_ADDRESS, 7);
  242. int second = bcdToDec(Wire.read());
  243. int minute = bcdToDec(Wire.read());
  244. int hour = bcdToDec(Wire.read() & 0b111111);
  245. //int weekDay = bcdToDec(Wire.read());
  246. //int monthDay = bcdToDec(Wire.read());
  247. //int month = bcdToDec(Wire.read());
  248. //int year = bcdToDec(Wire.read());
  249.  
  250. int taskas=second;
  251.  
  252.   //minutes
  253.     minute = minute % 100;
  254.     int min_d = minute/10;
  255.    
  256.     minute = minute % 10;
  257.     int min_v = minute;
  258.     //sekundes
  259.     second = second % 100;
  260.     int sec_d = second/10;
  261.    
  262.     second = second % 10;
  263.     int sec_v = second;
  264. //valandos
  265.     hour = hour % 100;
  266.     int val_d = hour/10;
  267.    
  268.     hour = hour % 10;
  269.     int val_v = hour;
  270.  
  271. clear();
  272. // display clock
  273.   //drawChar(0,0,val_d,2,false);
  274.   drawDigital(0,0,val_d);
  275.   //drawChar(8,0,val_v,2,false);
  276.   drawDigital(8,0,val_v);
  277.   //drawChar(16,0,'.',2,false);
  278.   //drawChar(24,0,min_d,2,false);
  279.   drawDigital(24,0,min_d);
  280.   //drawChar(32,0,min_v,2,false);
  281.   drawDigital(32,0,min_v);
  282.   //rawChar(40,0,'.',2,false);
  283.   //drawChar(48,5,sec_d,0,false);
  284.   drawDigital(48,0,sec_d);
  285.   //rawChar(56,5,sec_v,0,false);
  286.   drawDigital(56,0,sec_v);
  287.  
  288.   //setPixel(taskas+2,0,1);
  289.   //setPixel(taskas+2,1,1);
  290.  
  291.   drawPoint(taskas+2,15+sec_v,1);
  292.   drawPoint(taskas+2,14+sec_v,1);
  293.   drawPoint(taskas+3,14+sec_v,1);
  294.   drawPoint(taskas+3,15+sec_v,1);
  295.  
  296.   //seconds = (clock/20)%10;
  297.   //colour = (clock/200)&1;
  298.  
  299. // draw a rectangle  
  300.   //drawRect(0,0,63,16,1);
  301.   //drawRect(seconds*5+1,19,seconds*5+3,22,!colour);
  302.  
  303. // draw a blob  
  304.   //setPixel(56,20,colour); setPixel(57,20,colour); setPixel(58,20,colour);
  305.   //setPixel(57,19,colour); setPixel(57,21,colour);
  306.    
  307. // display next character of message
  308.     //drawChar(56,20,message[count%(sizeof(message)-1)],0,false);
  309.     //count = (count + 1);
  310.     // move the text 7 pixels (not 8 because it looks better) to the left
  311.     //for (byte i=0; i<7; i++) {
  312.        //delay(100);
  313.        //moveLeft(1,20,32);
  314.     //};
  315.     digitalWrite(en_74138, HIGH); // Turn off display
  316.      shiftOut(ScanRow);                // Shift out 8 columns
  317.      digitalWrite(latchPin, LOW);
  318.      digitalWrite(latchPin, HIGH);
  319.      PORTD = (ScanRow << 3) | (PORTD & 0X87); // Highlight row: pins 3 4 5 6 (la_74138 lb_74138 lc_74138 ld_74138)
  320.      digitalWrite(en_74138, LOW);     // Turn on display
  321.      ScanRow++;                       // Do the next pair of rows next time this routine is called
  322.      if (ScanRow==16) ScanRow=0;
  323.     //delay(200);
  324. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement