Hello All,
I'm currently trying to develop a program that combines the PCD8544, an SD module and RTC (DS1307). As for my current progress, I have been successful at running the RTC and PCD8544 together to display the current date and time. I also have had the SD read/write with the SD.h ReadWrite program.
Now that I have begun to combine all three, I started to run into a problem. I have read on a few forum posts not to run the LCD through software SPI along with the SD using hardware SPI. I am currently using the Adafruit_GFX.h along with Adafruit_PCD8544.h for the LCD (which seems to have support to at least select the hardware pins) and SD.h for the SD card. I tried to use the older PCD8544.h library, but didn't have much success (i'm willing to try again if someone can help me implement the correct code).
The pins I have been using for the LCD are listed in the code. SD pins are 10-13. 10 being CS and the hardware SPI pins for 11-13.
Basically, what is the easiest way to get this LCD working on hardware SPI along with the SD? Here is the code I have been trying to use. I don't mind to alter the library as long as someone is willing to help teach me how.
/* Logging Test
Test DS1307 RTC, PCD8544 LCD, and SD card (w/75LVC245 level shifter)
by Jeremy Phillips
vitruvianaquatics.webs.com
*/
#include <SD.h> //SD Card Library
const int SDSelect = 10; //Set SD Chip Select Pin
#include <RTClib.h> //RTC Library
#include <Wire.h> //SPI Interface Library
RTC_DS1307 RTC;
#include <Adafruit_GFX.h> //PCD8544 Library (call first)
#include <Adafruit_PCD8544.h> //PCD8544 Library
// pin(13) - Serial clock out (SCLK)
// pin(11) - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
const int LCDSelect = 4;
Adafruit_PCD8544 display = Adafruit_PCD8544(13, 11, 5, LCDSelect, 3);
//Define Variables
long id = 1;
int lastTime = -1;
void setup() {
Serial.begin(9600);
pinMode(SDSelect, OUTPUT);
// don't talk to LCD while we init the SD
digitalWrite(LCDSelect, HIGH);
digitalWrite(SDSelect, LOW);
// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!SD.begin(SDSelect))
{
Serial.println("Card Failure");
// don't talk to SD while we init the LCD
digitalWrite(LCDSelect, LOW);
digitalWrite(SDSelect, HIGH);
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.println("Card Failure!");
} else {
Serial.println("Card Sucessfully Initialized!");
}
// don't talk to SD while we init the LCD
digitalWrite(SDSelect, HIGH);
display.begin();
// init done
display.setContrast(60); //set LCD contrast
display.display(); // show splashscreen
delay(1000);
display.clearDisplay(); // clears the screen and buffer
digitalWrite(LCDSelect, HIGH);
RTC.begin();
//initialize RTC
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running");
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
} else {
Serial.println("RTC initialized");
}
// don't talk to LCD while we init the SD
digitalWrite(LCDSelect, HIGH);
digitalWrite(SDSelect, LOW);
//Begin Log File Header
File logFile = SD.open("DATALOG.csv", FILE_WRITE);
if (logFile)
{
logFile.println(", , , , , ,"); //Just a leading blank line, incase there was previous data
String header = "ID, Year, Month, Day, Hour, Minute, Second";
logFile.println(header);
logFile.close();
Serial.println(header);
}
else
{
Serial.println("Couldn't open log file");
// don't talk to SD while we init the LCD
digitalWrite(LCDSelect, LOW);
digitalWrite(SDSelect, HIGH);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.println("Couldn't open log file");
}
}
void loop ()
{
DateTime now = RTC.now(); //Get Date/Time
int time = now.second();
if (abs(time - lastTime) > 1)
{
// don't talk to SD while we init the LCD
digitalWrite(LCDSelect, LOW);
digitalWrite(SDSelect, HIGH);
display.clearDisplay(); //clear Display
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0); //set where to write on LCD
int Year = now.year();
int Mon = now.year();
int Day = now.day();
int Hour = now.day();
int Min = now.minute();
int Sec = now.second();
display.print(Year);
display.print("/");
display.print(Mon);
display.print("/");
display.println(Day);
display.print(Hour);
display.print(":");
display.print(Min);
display.print(":");
display.print(Sec);
display.display(); //display
// don't talk to LCD while we init the SD
digitalWrite(LCDSelect, HIGH);
digitalWrite(SDSelect, LOW);
//Create Data string for storing to SD card
//We will use CSV Format
String dataString = String(id) + ", " + String(Year) + ", " + String(Mon) + ", " + String(Day) + ", " + String(Hour) + ", " + String(Min) + ", " + String(Sec);
//Open a file to write to
//Only one file can be open at a time
File logFile = SD.open("DATALOG.csv", FILE_WRITE);
if (logFile)
{
logFile.println(dataString);
logFile.close();
Serial.println(dataString);
}
else
{
Serial.println("Couldn't open log file");
// don't talk to SD while we init the LCD
digitalWrite(LCDSelect, LOW);
digitalWrite(SDSelect, HIGH);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.println("Couldn't open log file");
}
//Increment ID number
id++;
lastTime = time;
}
}