Skip to content

Instantly share code, notes, and snippets.

Created October 2, 2012 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3817896 to your computer and use it in GitHub Desktop.
Save anonymous/3817896 to your computer and use it in GitHub Desktop.
Exercises SD card and RTC on Winode or Nanode
// This program exercises the MCP94710 Real Time Clock and the microSD card
// This Version to Run on Nanode RF or WiNode hardware
// It sets and reads the time and date back from the RTC
// It sets Alarm 1 and Alarm 2 on the RTC and acts on them accordingly
// It writes a 64 character message to the SRAM - which it pronts out on forst power up - and on every alarm
#include <SD.h>
#include <Wire.h>
#include <math.h>
static int payload;
int old;
int hours = 0 ;
int mins = 0 ;
int secs = 0 ;
unsigned long time_now = 0 ;
int alarm1;
int alarm2;
char RAM_message[64] = "The Quick Brown Foxy Leapt over the Lazy Dog whilst he snoozed";
char out_string[64];
long id = 0 ;
String dataString = " ";
String stringZero = " ";
String stringOne = " ";
String stringTwo = " ";
String stringThree = " ";
String stringId = " ";
//Set by default for the SD Card Library
//MOSI = Pin 11
//MISO = Pin 12
//SCLK = PIN 13
//Set the CS Pin on the SD card as Digital 4
int CS_pin = 4;
void setup()
{
Serial.begin(9600);
Wire.begin();
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
Serial.print("Navitrino Lite V1.0 ");
print_message(); // print out the 64 Character messsage from NV SRAM
// Writing to SRAM from 0x20 to 0x5F
for (int i = 0; i<63; i++)
{
WriteRTCByte(i+0x20,RAM_message[i]); // Write the message to SRAM
}
/*
WriteRTCByte(0,0); //STOP RTC
WriteRTCByte(1,0x11); //MINUTE=54
WriteRTCByte(2,0x17); //HOUR=12
WriteRTCByte(3,0x09); //DAY=1(MONDAY) AND VBAT=1
WriteRTCByte(4,0x01); //DATE=01
WriteRTCByte(5,0x10); //MONTH=10
WriteRTCByte(6,0x12); //YEAR=12
WriteRTCByte(0,0x80); //START RTC, SECOND=00
delay(100);
*/
set_alarm_1();
WriteRTCByte(7,0x30); //Set both alarms active
Serial.println("RTC Initialised");
get_time();
Serial.print(" ");
print_date();
Serial.println();
Serial.println("Initializing Card");
//CS Pin is an output
pinMode(CS_pin, OUTPUT);
if (!SD.begin(CS_pin))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
}
void loop()
{
get_time();
read_alarm_1();
read_alarm_2();
Serial.print(" ");
Serial.print(alarm1);
Serial.print(" ");
Serial.print(alarm2);
Serial.print(" ");
digitalWrite (5, !alarm1);
digitalWrite (6, !alarm1); // set the Red LED to show the alarm status
if (alarm2>>0) { WriteRTCByte(0x14,0x01); } // Cancel the alarm
int temp_0 = analogRead(0);
int temp_1 = analogRead(1);
int temp_2 = analogRead(2);
int temp_3 = analogRead(3);
stringZero = String(temp_0); // Form strings from the individual ADC readings
stringOne = String(temp_1);
stringTwo = String(temp_2);
stringThree = String(temp_3);
stringId = String(id);
//Create Data string for storing to SD card using the CSV Format
dataString = stringId + ", " + stringZero + ", " + stringOne + ", " + stringTwo + ", " + stringThree; // Concatenate the strings to write to the SD card
//Open a file to write to
//Only one file can be open at a time
digitalWrite(6,HIGH);
File dataFile = SD.open("log1.txt", FILE_WRITE);
if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
Serial.print(dataString);
}
else
{
Serial.println("Couldn't open log file");
}
digitalWrite(6,LOW);
if (alarm1>>0) { alarm1_handler(); }
else {
Serial.println("");
}
delay(1000);
}
//******************************************************************************************************
// Misc Routines
unsigned char ReadRTCByte(const unsigned char adr){
unsigned char data;
// Serial.print("*");
Wire.beginTransmission(0x6f);
Wire.write(adr);
Wire.endTransmission();
Wire.requestFrom(0x6f,1);
while (Wire.available()) data=Wire.read();
return data;
}
void WriteRTCByte(const unsigned char adr, const unsigned char data){
Wire.beginTransmission(0x6f);
Wire.write(adr);
Wire.write(data);
Wire.endTransmission();
}
void DisplayRTCData(const unsigned char adr, const unsigned char validbits){
unsigned char data;
data=ReadRTCByte(adr);
data=data & 0xff>>(8-validbits);
if (data<10) Serial.print("0"); //leading zero
Serial.print(data,HEX);
}
void get_time(){
unsigned char data;
data = ReadRTCByte(2);
hours = (data & 0x0f) + 10 *( (data & 0x30)/16);
data = ReadRTCByte(1);
mins = (data & 0x0f) + 10 *( (data & 0x70)/16);
data = ReadRTCByte(0);
secs = (data & 0x0f) + 10 *( (data & 0x70)/16);
Serial.print(hours);
Serial.print(":");
if (mins<10) Serial.print("0"); //leading zero
Serial.print(mins);
Serial.print(":");
if (secs<10) Serial.print("0"); //leading zero
Serial.print(secs);
}
// Print out the RTC
void print_time(){
DisplayRTCData(2,6);
Serial.print(":");
DisplayRTCData(1,7);
Serial.print(":");
DisplayRTCData(0,7);
Serial.print(" ");
}
void print_date(){
DisplayRTCData(4,6);
Serial.print("/");
DisplayRTCData(5,5);
Serial.print("/");
Serial.print("20"); //year beginning with 20xx
DisplayRTCData(6,8);
}
void set_alarm_1()
{
WriteRTCByte(0x0A,0x00); //SECS=00
WriteRTCByte(0x0B,0x45); //MINUTE=45
WriteRTCByte(0x0C,0x14); //HOUR=12
WriteRTCByte(0x0D,0x01); //DAY=1(MONDAY) AND Match = seconds
WriteRTCByte(0x0E,0x01); //DATE=01
WriteRTCByte(0x0F,0x10); //MONTH=10
}
void set_alarm_2()
{
WriteRTCByte(0x11,0x00); //SECS=00
WriteRTCByte(0x12,0x43); //MINUTE=54
WriteRTCByte(0x13,0x13); //HOUR=12
WriteRTCByte(0x14,0x01); //DAY=1(MONDAY) AND Match = seconds
WriteRTCByte(0x15,0x01); //DATE=01
WriteRTCByte(0x16,0x10); //MONTH=10
}
void read_alarm_1()
{
int data = ReadRTCByte(0x0D);
alarm1 = (data & 0x08);
}
void read_alarm_2()
{
int data = ReadRTCByte(0x14);
alarm2 = (data & 0x08);
}
void alarm1_handler() {
WriteRTCByte(0x0D,0x01); // Clear the alarm flag
// Write the RAM message
for (int i = 0; i<=63; i++)
{
int data=ReadRTCByte(i+0x20);
out_string[i] = data ;
}
Serial.print(out_string); // Print out the SRAM message
Serial.println(" ");
} // Cancel the alarm
void print_message() {
// Write the RAM message
for (int i = 0; i<=63; i++)
{
int data=ReadRTCByte(i+0x20);
out_string[i] = data ;
}
Serial.print(out_string); // Print out the SRAM message
Serial.println(" ");
}
//*********************************************************************************************************
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment