Skip to content

Instantly share code, notes, and snippets.

@dyangrev
Created January 11, 2015 20:05
Show Gist options
  • Select an option

  • Save dyangrev/582ac37539e0b2a4125c to your computer and use it in GitHub Desktop.

Select an option

Save dyangrev/582ac37539e0b2a4125c to your computer and use it in GitHub Desktop.
#include "mbed.h"
#include "N5110.h"
#define TMP102_ADD 0X48
#define TMP102_R_ADD 0x91
#define TMP102_W_ADD 0x90
#define TEMP_REG 0x00
#define CONFIG_REG 0x01
#define THIGH_REG 0x02
#define TLOW_REG 0x03
////////////////////////////////////////////////////////////////////////////////////
N5110 lcd(p7,p8,p9,p10,p11,p13,p21);
BusOut leds(LED4,LED3,LED2,LED1);
LocalFileSystem local("local"); // create local filesystem
float getTemperature();
void initTMP102();
void error(int code);
Serial serial(USBTX,USBRX);
void serialISR(); // ISR that is called when serial data is received
void setTime(); // function to set the UNIX time
void writeDataToFile(int data, float temperature, char *date, char *time);
int setTimeFlag = 0; // flag for ISR
char rxString[16]; // buffer to store received string
I2C tmp102(p28,p27); // SDA, SCL
int main()
{
int counter = 0; // variable to increment
serial.attach(&serialISR); // attach serial ISR
char buffer1[30]; // buffer used to store time string
char buffer2[30]; // buffer used to store time string
set_time(1420195048); // initialise time to 1st January 1970
initTMP102();
lcd.init();
char buffer[30];
while(1)
{
float temperatureTW = getTemperature();
sprintf(buffer,"Temp=%4.2f",temperatureTW);
lcd.printString(buffer,0,3);
//wait(0.2);
time_t seconds = time(NULL); // get current time
// format time into a string (time and date)
strftime(buffer1, 30 , "%X", localtime(&seconds));
// print over serial
serial.printf("Time = %s\n",buffer1);
strftime(buffer2, 30 , "%D", localtime(&seconds));
// print over serial
//serial.printf("Time = %s\n",buffer2);
//wait(1.0); // delay for a second
if (setTimeFlag) { // if updated time has been sent
setTimeFlag = 0; // clear flag
setTime(); // update time
}
lcd.printString(buffer1,0,1);
lcd.printString(buffer2,0,2);
writeDataToFile(counter, temperatureTW, buffer2, buffer1); // write current value to disk
counter++; // increment counter
wait(1.0); // small delay
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void initTMP102()
{
tmp102.frequency(400000); // set bus speed to 400 kHz
int ack; // used to store acknowledgement bit
char data[2]; // array for data
char reg = CONFIG_REG; // register address
//////// Read current status of configuration register ///////
ack = tmp102.write(TMP102_W_ADD,&reg,1); // send the slave write address and the configuration register address
if (ack)
error(1); // if we don't receive acknowledgement, flash error message
ack = tmp102.read(TMP102_R_ADD,data,2); // read default 2 bytes from configuration register and store in buffer
if (ack)
error(2); // if we don't receive acknowledgement, flash error message
///////// Configure the register //////////
// set conversion rate to 1 Hz
data[1] |= (1 << 6); // set bit 6
data[1] &= ~(1 << 7); // clear bit 7
//////// Send the configured register to the slave ////////////
ack = tmp102.write(TMP102_W_ADD,&reg,1); // send the slave write address and the configuration register address
if (ack)
error(3); // if we don't receive acknowledgement, flash error message
ack = tmp102.write(TMP102_W_ADD,data,2); // send 2 data bytes to the configuration register
if (ack)
error(4); // if we don't receive acknowledgement, flash error message
}
// hang in infinite loop flashing error code
void error(int code)
{
while(1) {
leds = 0;
wait(0.25);
leds = code;
wait(0.25);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
float getTemperature()
{
int ack; // used to store acknowledgement bit
char data[2]; // array for data
char reg = TEMP_REG; // temperature register address
ack = tmp102.write(TMP102_W_ADD,&reg,1); // send temperature register address
if (ack)
error(5); // if we don't receive acknowledgement, flash error message
ack = tmp102.read(TMP102_R_ADD,data,2); // read 2 bytes from temperature register and store in array
if (ack)
error(6); // if we don't receive acknowledgement, flash error message
int temperature = (data[0] << 4) | (data[1] >> 4);
return temperature*0.0625;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setTime() {
// print time for debugging
serial.printf("set_time - %s",rxString);
// atoi() converts a string to an integer
int time = atoi(rxString);
// update the time
set_time(time);
}
void serialISR() {
// when a serial interrupt occurs, read rx string into buffer
serial.gets(rxString,16);
// set flag
setTimeFlag = 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void writeDataToFile(int data, float temperature, char *date, char *time)
{
leds = 15; // turn on LEDs for feedback
FILE *fp = fopen("/local/log.txt", "a"); // open 'log.txt' for appending
// if the file doesn't exist it is created, if it exists, data is appended to the end
fprintf(fp, "%s %s, %4.2f", date, time, temperature);
fclose(fp); // close file
leds = 0; // turn off LEDs to signify file access has finished
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment