Embedded system Fun Blog
























































Find out all the best information, libraries and circuit about the latest Embedded systems.
Showing posts with label ds1620. Show all posts
Showing posts with label ds1620. Show all posts

Saturday, 7 January 2012

MBED Example: How to use DS1620 Temperature Monitor

.from: http://www.frank-zhao.com/thingspeak_mbed_tut1/

#include "mbed.h"
#include "EthernetNetIf.h"
#include "NTPClient.h"
#include "HTTPClient.h"


// networking stuff
EthernetNetIf eth;
NTPClient ntp;
HTTPClient http;

char* ntpServerUrl = "0.ca.pool.ntp.org";

char* thingSpeakUrl = "http://api.thingspeak.com/update";
char* thingSpeakKey = "IS6YCLKIR423VO5H";

char urlBuffer[256];
char timeBuffer[64];

// pin defs
DigitalOut rst(p30);
DigitalOut clk(p29);
DigitalInOut dq(p28);

void DS1620Send(long data, int numOfBits)
{
    dq.output(); // we are sending
   
    for (int i = 0; i < numOfBits; i++)
    {
        clk = 0;
        wait_us(0.05);
        if (data & (1 << i)) // bit is 1
        {
            dq = 1;
        }
        else // bit is 0
        {
            dq = 0;
        }
        wait_us(0.5);
        clk = 1;
        wait_us(0.5);
    }
   
    dq.input(); // go back to being input
   
    clk = 1; // leave high
}

float DS1620ReadTemp()
{
    rst = 1;
    DS1620Send(0xAA, 8); // read temp command
   
    dq.input();
    int results = 0;
   
    // read in 9 bits
    for (int i = 0; i < 9; i++)
    {
        clk = 0;
        wait_us(0.5);
        clk = 1;
        wait_us(0.05);
        if (dq.read() != 0) // bit is 1
        {
            if (i < 8)
            {
                results |= 1 << i;
            }
            else
            {
                results *= -1; // last bit is sign bit
            }
        }
        wait_us(0.4);
    }
   
    clk = 1; // leave high
   
    rst = 0;
   
    return (float)results / 2; // div by 2 since last bit is 0.5
}

void DS1620Setup()
{
    // initial pin states
    rst = 0;
    clk = 1;
    dq.input();
    wait(0.1);
   
    rst = 1;
    DS1620Send(0x0A0C, 16); // config for continuous mode with CPU mode
    rst = 0;
    wait(0.1);
    rst = 1;
    DS1620Send(0xEE, 8); // start continuous conversions
    rst = 0;
    wait(0.1);
}

int main() {

    printf("Start\r\n");

    printf("Setting up Ethernet...\r\n");
    EthernetErr ethErr = eth.setup();
    if(ethErr)
    {
        printf("Error %d in ethernet setup.\r\n", ethErr);
        return -1;
    }
    printf("Ethernet setup OK\r\n");
   
    // get time from ntp
    time_t ctTime;
    Host server(IpAddr(), 123, ntpServerUrl);
    ntp.setTime(server);
   
    // start the sensor
    DS1620Setup();
   
    while(1)
    {
        // update data here
        ctTime = time(NULL);
        float temperature = DS1620ReadTemp();
       
        // format time here
        timeBuffer[0] = 0;
        strftime(timeBuffer, 64, "%Y-%m-%d %H:%M:%S", localtime(&ctTime));
       
        // for debug
        printf("Time: %s, Temperature: %f\r\n", timeBuffer, temperature);
       
        // format url here
        urlBuffer[0] = 0;
        sprintf(urlBuffer, "%s?key=%s&field1=%s&field2=%f", thingSpeakUrl, thingSpeakKey, timeBuffer, temperature);
        printf("Request to %s\r\n", urlBuffer);
       
        HTTPText resp;
        HTTPResult res = http.get(urlBuffer, &resp);
        if (res == HTTP_OK)
        {
            printf("Result :\"%s\"\r\n", resp.gets());
        }
        else
        {
            printf("Error %d\r\n", res);
        }
       
        wait(16); // limited by ThingSpeak's API
    }
   
    return 0;
   
}