Dr_Doggy
Well-Known Member
#include <Wire.h>
#include "FontMap.h"
unsigned char dataPin = 2; // ic: 14, ser_in Define which pins will be used for the Shift Register control
unsigned char latchPin = 3; // ic:12 silkscreen numbers!
unsigned char clockPin = 4;
const int DS1307 = 0x68; // Address of DS1307 see data sheets
byte second = 0;
byte minute = 0;
byte hour = 0;
unsigned char displayPointer=0; // for interrupt use...
unsigned char buffer1[32]; // buffer for screen
unsigned char backbuffer[32]; // Spare screen for drawing on
unsigned char power[8]={128,64,32,16,8,4,2,1};
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
if(TIFR2) // Make sure its the timer interrupt.
{
setcolumn(displayPointer);
setdata(buffer1[displayPointer]);
digitalWrite(latchPin ,HIGH);digitalWrite(latchPin , LOW ); // STORECLOCK
if(++displayPointer==32) { displayPointer = 0; } // 32 LED row sections in total
}
TIFR2 = 0; // Clear timer 2 interrupt flag
}
byte decToBcd(byte val) {
return ((val/10*16) + (val%10));
}
byte bcdToDec(byte val) {
return ((val/16*10) + (val%16));
}
// This set of codes is allows input of data
void setTime() {
Serial.print("Please enter the current hour in 24hr format, 0-23. - ");
hour = readByte();
Serial.println(hour);
Serial.print("Please enter the current minute, 0-59. - ");
minute = readByte();
Serial.println(minute);
second = 0;
Serial.println("The data has been entered.");
// The following codes transmits the data to the RTC
Wire.beginTransmission(DS1307);
Wire.write(byte(0));
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(byte(0));
Wire.endTransmission();
// Ends transmission of data
}
byte readByte() {
while (!Serial.available()) delay(10);
byte reading = 0;
byte incomingByte = Serial.read();
while (incomingByte != '\n') {
if (incomingByte >= '0' && incomingByte <= '9')
reading = reading * 10 + (incomingByte - '0');
else;
incomingByte = Serial.read();
}
Serial.flush();
return reading;
}
void printTime() {
char buffer[3];
const char* AMPM = 0;
readTime();
if (hour > 12) {
hour -= 12;
AMPM = " PM";
}
else AMPM = " AM";
Serial.print(hour);
Serial.print(":");
sprintf(buffer, "%02d", minute);
Serial.print(buffer);
Serial.println(AMPM);
}
void readTime() {
Wire.beginTransmission(DS1307);
Wire.write(byte(0));
Wire.endTransmission();
Wire.requestFrom(DS1307, 7);
second = bcdToDec(Wire.read());
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read());
}
void setcolumn(unsigned char col){
signed char pos;
for (pos = 32;pos>-1;pos--){
if (col == pos){digitalWrite(dataPin ,HIGH);}else {digitalWrite(dataPin ,LOW);} // PIN1 DATA pin
digitalWrite(clockPin ,HIGH);digitalWrite(clockPin ,LOW);
}}
void setdata(unsigned char dat){
unsigned char pos;
for (pos = 0;pos<8;pos++){
if (dat & 128){dat-=128;digitalWrite(dataPin ,HIGH);}else { digitalWrite(dataPin ,LOW);} // PIN1 DATA pin
dat = dat * 2;
digitalWrite(clockPin ,HIGH);digitalWrite(clockPin ,LOW);
}}
void clr() //clear
{
int addr;
for(addr=0;addr<32;addr++) // Empty display buffer
backbuffer[addr]= 0;
}
void Blit() //transfers data between display buffer to screen buffer
{
int addr=0;
noInterrupts(); // disable all interrupts during setup
for(addr=0;addr < 32;addr ++)
{
buffer1[addr] = backbuffer[addr]; // put all data from display buffer
} // to screen buffer
interrupts(); // enable all interrupts
}
void pixel(signed char x,signed char y,int cond)
{
unsigned char pix,msk;
if(x<0 || y<0) return; // outside drawing limits negative
if(x>31 || y>7) return; // outside drawing limits positive
pix = power[y];
msk = backbuffer[x]; // get exsisting data
if(cond == 2)
pix ^= msk; // XOR data to screen
if (cond == 1)
{
pix = ~pix;
pix &= msk; // AND data to screen
}
if(cond == 0)
pix |= msk; // OR data to screen
backbuffer[x] = pix; // apply changes
}
void charput(unsigned char ch, signed char x,signed char y)
{
signed char x1, y1;
unsigned char disp;
unsigned char disp2;
for( x1=0;x1<8;x1++) // eight rows
{
disp = font[x1+(ch * 8)];
for (y1 = 0; y1<8; y1++) // eight pixels
{
disp2 = disp & power[y1];
if(disp2 > 0)
{
pixel(x+x1,y+y1,0); // OR the pixel to the display buffer
}
}
}
}
void strput(const char* ch, signed char x,signed char y)
{
int addr;
while (*ch )
{
charput(*ch++,x,y); // write a string to the display buffer
x+=7;
}
}
unsigned char Vscroll(unsigned char value,unsigned char valueOL, signed char x,signed char y, unsigned char cntr1){ // Vscroll(hour, hourLA, x,y,scrollctrHR);}
charput((valueOL/10),x,(y + cntr1 - 8));
charput((valueOL%10),x+8,(y + cntr1 - 8));
charput((value/10),x,(y + cntr1 ));
charput((value%10),x+8,(y + cntr1 ));
if (cntr1 > 0){cntr1--;}
return cntr1;
}
void setup() //setup runs once
{
signed char cntr;
unsigned char timeout = 0;
noInterrupts(); // disable all interrupts during setup
DDRD = DDRD | B11111100; //port registers used to set pin directions
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 5; // compare match register 16MHz/256/2Hz -----------------------------------> delay time (lcd flicker/brightness)
TCCR1B |= (1 << WGM12); // CTC mode, free-running, clear on match
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts
clr();
charput(58,1,-1);
Blit();
delay(1000);
Wire.begin();
charput(58,2,0);
Blit();
delay(1000);
Serial.begin(9600);
clr();
charput(58,3,1);
Blit();
delay(1000); // This delay allows the MCU to read the current date and time.
Serial.print("The current date and time is: ");
printTime();
clr();
charput(58,4,2);
Blit();
delay(1000);
Serial.println("Please change to newline ending the settings on the lower right of the Serial Monitor");
Serial.println("Would you like to set the date and time now? Y/N");
while (!Serial.available() & timeout < 54){ delay(20);timeout++;
clr();
charput(58,5,3);
Blit();
}
if (Serial.read() == 'y' || Serial.read() == 'Y')
// This set of functions allows the user to change the date and time
{
Serial.read();
setTime();
Serial.print("The current date and time is now: ");
printTime();
}
Serial.println("Thank you.");
} // End main
void loop() //just sitting here
{
byte secondBK = 0;
byte minuteBK = 0;
byte hourBK = 0;
byte secondLA = 0;
byte minuteLA = 0;
byte hourLA = 0;
unsigned char scrollctrHR= 0;
unsigned char scrollctrMN = 0;
unsigned char scrollctrSE = 0;
while(1){
clr();
charput(58,0,0);
Blit();
delay(300);
charput(58,2,0);
Blit();
delay(300);
readTime();
if (second != secondBK){secondLA = secondBK; secondBK=second;scrollctrSE=8;} else {charput((second/10),3,0);charput((second%10),11,0);}
if (scrollctrSE > 0){scrollctrSE = Vscroll(second, secondLA, 3,0,scrollctrSE);}
if (minute != minuteBK ){minuteLA = minuteBK; minuteBK =minute;scrollctrMN=8;} else {charput((minute/10),19,0);charput((minute%10),27,0);}
if (scrollctrMN > 0){scrollctrMN = Vscroll(minute, minuteLA, 19,0,scrollctrMN);}
//if (hour != hourBK ){hourLA = hourBK; hourBK =hour;scrollctrHR=8;} else {charput((hour/10),x,0);charput((hour%10),x,0);}
//if (scrollctrHR > 0){scrollctrHR = Vscroll(hour, hourLA, x,0,scrollctrHR);}
Blit();
delay(300);
}}
#include "FontMap.h"
unsigned char dataPin = 2; // ic: 14, ser_in Define which pins will be used for the Shift Register control
unsigned char latchPin = 3; // ic:12 silkscreen numbers!
unsigned char clockPin = 4;
const int DS1307 = 0x68; // Address of DS1307 see data sheets
byte second = 0;
byte minute = 0;
byte hour = 0;
unsigned char displayPointer=0; // for interrupt use...
unsigned char buffer1[32]; // buffer for screen
unsigned char backbuffer[32]; // Spare screen for drawing on
unsigned char power[8]={128,64,32,16,8,4,2,1};
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
if(TIFR2) // Make sure its the timer interrupt.
{
setcolumn(displayPointer);
setdata(buffer1[displayPointer]);
digitalWrite(latchPin ,HIGH);digitalWrite(latchPin , LOW ); // STORECLOCK
if(++displayPointer==32) { displayPointer = 0; } // 32 LED row sections in total
}
TIFR2 = 0; // Clear timer 2 interrupt flag
}
byte decToBcd(byte val) {
return ((val/10*16) + (val%10));
}
byte bcdToDec(byte val) {
return ((val/16*10) + (val%16));
}
// This set of codes is allows input of data
void setTime() {
Serial.print("Please enter the current hour in 24hr format, 0-23. - ");
hour = readByte();
Serial.println(hour);
Serial.print("Please enter the current minute, 0-59. - ");
minute = readByte();
Serial.println(minute);
second = 0;
Serial.println("The data has been entered.");
// The following codes transmits the data to the RTC
Wire.beginTransmission(DS1307);
Wire.write(byte(0));
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(byte(0));
Wire.endTransmission();
// Ends transmission of data
}
byte readByte() {
while (!Serial.available()) delay(10);
byte reading = 0;
byte incomingByte = Serial.read();
while (incomingByte != '\n') {
if (incomingByte >= '0' && incomingByte <= '9')
reading = reading * 10 + (incomingByte - '0');
else;
incomingByte = Serial.read();
}
Serial.flush();
return reading;
}
void printTime() {
char buffer[3];
const char* AMPM = 0;
readTime();
if (hour > 12) {
hour -= 12;
AMPM = " PM";
}
else AMPM = " AM";
Serial.print(hour);
Serial.print(":");
sprintf(buffer, "%02d", minute);
Serial.print(buffer);
Serial.println(AMPM);
}
void readTime() {
Wire.beginTransmission(DS1307);
Wire.write(byte(0));
Wire.endTransmission();
Wire.requestFrom(DS1307, 7);
second = bcdToDec(Wire.read());
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read());
}
void setcolumn(unsigned char col){
signed char pos;
for (pos = 32;pos>-1;pos--){
if (col == pos){digitalWrite(dataPin ,HIGH);}else {digitalWrite(dataPin ,LOW);} // PIN1 DATA pin
digitalWrite(clockPin ,HIGH);digitalWrite(clockPin ,LOW);
}}
void setdata(unsigned char dat){
unsigned char pos;
for (pos = 0;pos<8;pos++){
if (dat & 128){dat-=128;digitalWrite(dataPin ,HIGH);}else { digitalWrite(dataPin ,LOW);} // PIN1 DATA pin
dat = dat * 2;
digitalWrite(clockPin ,HIGH);digitalWrite(clockPin ,LOW);
}}
void clr() //clear
{
int addr;
for(addr=0;addr<32;addr++) // Empty display buffer
backbuffer[addr]= 0;
}
void Blit() //transfers data between display buffer to screen buffer
{
int addr=0;
noInterrupts(); // disable all interrupts during setup
for(addr=0;addr < 32;addr ++)
{
buffer1[addr] = backbuffer[addr]; // put all data from display buffer
} // to screen buffer
interrupts(); // enable all interrupts
}
void pixel(signed char x,signed char y,int cond)
{
unsigned char pix,msk;
if(x<0 || y<0) return; // outside drawing limits negative
if(x>31 || y>7) return; // outside drawing limits positive
pix = power[y];
msk = backbuffer[x]; // get exsisting data
if(cond == 2)
pix ^= msk; // XOR data to screen
if (cond == 1)
{
pix = ~pix;
pix &= msk; // AND data to screen
}
if(cond == 0)
pix |= msk; // OR data to screen
backbuffer[x] = pix; // apply changes
}
void charput(unsigned char ch, signed char x,signed char y)
{
signed char x1, y1;
unsigned char disp;
unsigned char disp2;
for( x1=0;x1<8;x1++) // eight rows
{
disp = font[x1+(ch * 8)];
for (y1 = 0; y1<8; y1++) // eight pixels
{
disp2 = disp & power[y1];
if(disp2 > 0)
{
pixel(x+x1,y+y1,0); // OR the pixel to the display buffer
}
}
}
}
void strput(const char* ch, signed char x,signed char y)
{
int addr;
while (*ch )
{
charput(*ch++,x,y); // write a string to the display buffer
x+=7;
}
}
unsigned char Vscroll(unsigned char value,unsigned char valueOL, signed char x,signed char y, unsigned char cntr1){ // Vscroll(hour, hourLA, x,y,scrollctrHR);}
charput((valueOL/10),x,(y + cntr1 - 8));
charput((valueOL%10),x+8,(y + cntr1 - 8));
charput((value/10),x,(y + cntr1 ));
charput((value%10),x+8,(y + cntr1 ));
if (cntr1 > 0){cntr1--;}
return cntr1;
}
void setup() //setup runs once
{
signed char cntr;
unsigned char timeout = 0;
noInterrupts(); // disable all interrupts during setup
DDRD = DDRD | B11111100; //port registers used to set pin directions
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 5; // compare match register 16MHz/256/2Hz -----------------------------------> delay time (lcd flicker/brightness)
TCCR1B |= (1 << WGM12); // CTC mode, free-running, clear on match
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts
clr();
charput(58,1,-1);
Blit();
delay(1000);
Wire.begin();
charput(58,2,0);
Blit();
delay(1000);
Serial.begin(9600);
clr();
charput(58,3,1);
Blit();
delay(1000); // This delay allows the MCU to read the current date and time.
Serial.print("The current date and time is: ");
printTime();
clr();
charput(58,4,2);
Blit();
delay(1000);
Serial.println("Please change to newline ending the settings on the lower right of the Serial Monitor");
Serial.println("Would you like to set the date and time now? Y/N");
while (!Serial.available() & timeout < 54){ delay(20);timeout++;
clr();
charput(58,5,3);
Blit();
}
if (Serial.read() == 'y' || Serial.read() == 'Y')
// This set of functions allows the user to change the date and time
{
Serial.read();
setTime();
Serial.print("The current date and time is now: ");
printTime();
}
Serial.println("Thank you.");
} // End main
void loop() //just sitting here
{
byte secondBK = 0;
byte minuteBK = 0;
byte hourBK = 0;
byte secondLA = 0;
byte minuteLA = 0;
byte hourLA = 0;
unsigned char scrollctrHR= 0;
unsigned char scrollctrMN = 0;
unsigned char scrollctrSE = 0;
while(1){
clr();
charput(58,0,0);
Blit();
delay(300);
charput(58,2,0);
Blit();
delay(300);
readTime();
if (second != secondBK){secondLA = secondBK; secondBK=second;scrollctrSE=8;} else {charput((second/10),3,0);charput((second%10),11,0);}
if (scrollctrSE > 0){scrollctrSE = Vscroll(second, secondLA, 3,0,scrollctrSE);}
if (minute != minuteBK ){minuteLA = minuteBK; minuteBK =minute;scrollctrMN=8;} else {charput((minute/10),19,0);charput((minute%10),27,0);}
if (scrollctrMN > 0){scrollctrMN = Vscroll(minute, minuteLA, 19,0,scrollctrMN);}
//if (hour != hourBK ){hourLA = hourBK; hourBK =hour;scrollctrHR=8;} else {charput((hour/10),x,0);charput((hour%10),x,0);}
//if (scrollctrHR > 0){scrollctrHR = Vscroll(hour, hourLA, x,0,scrollctrHR);}
Blit();
delay(300);
}}