added RTC functions

git-svn-id: https://svn.salfter.gotdns.org/svn/a2bfc/trunk@89 1b90f75b-8b96-4784-87c0-14078182fce6
This commit is contained in:
(no author)
2007-09-24 05:41:06 +00:00
parent 973382c13c
commit d8a3fd2194
6 changed files with 92 additions and 8 deletions

BIN
DS2417.pdf Normal file
View File

Binary file not shown.

30
main.c
View File

@@ -2,6 +2,8 @@
#include <conio.h>
#include "ow.h"
#include "ow-temp.h"
#include "ow-time.h"
#include <errno.h>
int main (void)
{
@@ -9,24 +11,46 @@ int main (void)
unsigned char devs[80];
unsigned char* tempsensor=NULL;
unsigned char* clock=NULL;
time_t t;
// unsigned char c1, c2;
devcount=ow_enumeratedevices(devs, 10);
/*//
for (c1=0; c1<devcount; c1++)
{
printf("%i: ", c1);
for (c2=0; c2<8; c2++)
printf("%02X ", devs[(c1<<3)+c2]);
printf("\n");
}
//*/
for (i=0; i<devcount; i++)
{
if (ow_temp_idcheck(&devs[i<<3]))
tempsensor=&devs[i<<3];
if (tempsensor==NULL)
if (ow_time_idcheck(&devs[i<<3]))
clock=&devs[i<<3];
}
if (tempsensor==NULL || clock==NULL)
{
printf("Error: no temperature sensor found.\n");
printf("Error: temperature sensor and/or clock not found.\n");
return -1;
}
ow_time_set(clock, 1190611728-28800);
do
{
do
{
for (i=0; i<250; i++)
ow_mswait();
printf("%i deg F\n", ow_temp_read(tempsensor, OW_TEMP_FAHRENHEIT));
t=ow_time_read(clock);
printf("%i deg F %s",
ow_temp_read(tempsensor, OW_TEMP_FAHRENHEIT),
asctime(localtime(&t)));
}
while (!kbhit());
}

View File

@@ -1,4 +1,5 @@
#include "ow-temp.h"
#include <errno.h>
short ow_temp_read(unsigned char* devid, unsigned char format)
{
@@ -7,7 +8,10 @@ short ow_temp_read(unsigned char* devid, unsigned char format)
unsigned char i;
if (!ow_temp_idcheck(devid))
return OW_ERR_INVALIDDEVICE;
{
errno=EINVAL;
return -1;
}
ow_selectdevice(devid);
ow_writebyte(68); // ask for a temperature reading
ow_wait(); // wait for operation to complete
@@ -17,6 +21,7 @@ short ow_temp_read(unsigned char* devid, unsigned char format)
data[i]=ow_readbyte();
rawtemp=(short)(data[0]+((int)data[1]<<8));
errno=0;
switch (format)
{
case OW_TEMP_FAHRENHEIT:

46
ow-time.c Normal file
View File

@@ -0,0 +1,46 @@
#include "ow-time.h"
#include <errno.h>
time_t ow_time_read(unsigned char* devid)
{
time_t t;
unsigned char i;
if (!ow_time_idcheck(devid))
{
errno=EINVAL;
return -1;
}
ow_selectdevice(devid);
ow_writebyte(102); // read clock
ow_readbyte(); // skip first byte returned
for (i=0; i<4; i++) // read time
((unsigned char*)&t)[i]=ow_readbyte();
ow_reset();
errno=0;
return t;
}
void ow_time_set(unsigned char* devid, time_t newtime)
{
unsigned char i;
if (!ow_time_idcheck(devid))
{
errno=EINVAL;
return;
}
ow_selectdevice(devid);
ow_writebyte(153); // set clock
ow_writebyte(12); // device control: start oscillator
for (i=0; i<4; i++) // write time
ow_writebyte(((unsigned char*)&newtime)[i]);
ow_reset();
errno=0;
return;
}
unsigned char ow_time_idcheck(unsigned char* devid)
{
return (devid[0]==39)?1:0;
}

13
ow-time.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef OW_TIME_H
#define OW_TIME_H
#include "ow.h"
#include <time.h>
// 1-Wire real-time clock functions
time_t ow_time_read(unsigned char* devid);
void ow_time_set(unsigned char* devid, time_t newtime);
unsigned char ow_time_idcheck(unsigned char* devid);
#endif // OW_TIME_H

4
ow.h
View File

@@ -17,8 +17,4 @@ void ow_selectdevice(unsigned char* devid);
unsigned char ow_crc(unsigned char* data, unsigned char len);
unsigned char ow_enumeratedevices(unsigned char* devids, unsigned char maxdevs);
// common 1-Wire constants
#define OW_ERR_INVALIDDEVICE -32767
#endif // OW_H