Functions

ds1302.c File Reference

#include <ds1302.h>
#include <util/delay_basic.h>
#include <util/delay.h>

Functions

void rtc_clock_up_down (void)
void read_from_rtc (uint8_t command, uint8_t bytes, uint8_t *address_ptr)
 Read a set of data from the DS1302 Real time clock to memory.
void write_to_rtc (uint8_t command, uint8_t bytes, uint8_t *data_ptr)
 Write a set of data to the DS1302 Real time clock from memory.
void read_time (uint8_t *address_ptr)
 Read a set of data (7 bytes) from the DS1302 Real time clock into memory Order is ss,mm,hh,dd,mm,dow,yy.
void send_time_to_rtc (uint8_t *time_ptr)
 Set the clock on the RTC, sending it 8 bytes ss mm hh date month day year 0.
uint8_t bcd_to_hex (uint8_t bcd)
 Convert a BCD value in a byte to the correct hex value.
uint32_t get_fattime (void)
 Read the time and convert to a 32 bit value suitable for FAT filesystem timestamps.
void date_dirname (uint8_t *time_store, uint8_t *dirname)
 Create a 7 byte directory name with date/time value.
void filename_from_date (uint8_t *time_store, uint8_t *filename)
 Create or overwrite a filename with date/time value.
void iso_time (uint8_t *time_store, uint8_t *iso_time_store)
 Converts a time stored in memory (typically from a Real Time Clock chip) into ISO order.
void set_datetime (uint8_t *iso_time_store)
 Take in a pointer to time in ISO format (15 bytes) eg 20101203 141604 and set RTC to that time.
uint8_t hex2ascii_h (uint8_t hexval)
 Convert the high nibble of a supplied byte into an ASCII character.
uint8_t hex2ascii_l (uint8_t hexval)
 Convert the low nibble of a supplied byte into an ASCII character.

Function Documentation

uint8_t bcd_to_hex ( uint8_t  bcd  ) 

Convert a BCD value in a byte to the correct hex value.

Parameters:
bcd uint8_t value in BCD
Returns:
returns uint8_t hex value converted from BCD

For example, 23 -> 0x17

void date_dirname ( uint8_t *  time_store,
uint8_t *  dirname 
)

Create a 7 byte directory name with date/time value.

Parameters:
*time_store memory location of 7 bytes of the time to be converted.
*dirname directory name to be created.

Note that this function only converts. It doesn't update *time_store with the correct time

dirname is overwritten with yymddhh m is 1-12 value expressed in Hex (eg C for December). Other values (BCD from the clock) are copied as is, without any conversion. eg 20101215 13:16:56 -> 11C1513

Note that both time_store and dirname must exist in memory as arrays of 7 or more bytes. To use the dirname as a string, use 8 bytes and null the 8th

void filename_from_date ( uint8_t *  time_store,
uint8_t *  filename 
)

Create or overwrite a filename with date/time value.

Convert a time read from the RTC into a file name. DOESN'T READ THE RTC.

Parameters:
*time_store memory location of 7 bytes of the time to be converted.
*filename filename name to be created or overwritten.

Note that this function only converts. It doesn't update *time_store with the correct time

IMPORTANT Several characters of filename are NOT changed or set. Make sure you set it to a valid character or the filesystem will generate an error.

filename is overwritten with ^mddhhmm^ss^ where positions with ^ are unchanged m is month 1-12 value expressed as a single Hex character (eg C for December). Other values (BCD from the clock) are copied as is, without any conversion.

eg filename ABCDEFGH.XYZ plus date (15th December 2010) 20101215 13:16:56 -> AC151316.56Z

Note that both time_store and filename must exist in memory as arrays of 7 and 12 (or more) bytes respectively. To use the filename as a string, use a 13 byte array and null the 13th, or create filename as a string of the correct length.

uint32_t get_fattime ( void   ) 

Read the time and convert to a 32 bit value suitable for FAT filesystem timestamps.

Reads the time into internal variable time_store[7], and converts for FAT filesystems.

Returns:
uint32_t FAT time encoded in a 32 bit value

See FATFS documentation for the specification of FAT time stamp format time_store must consist of an array of at least 7 bytes A side effect of calling this function is to update the time stores in time_store Format of time_store is (in BCD) ss,mm,hh,date,month,dow,yy

uint8_t hex2ascii_h ( uint8_t  hexval  ) 

Convert the high nibble of a supplied byte into an ASCII character.

Parameters:
hexval Hex value
Returns:
ASCII character of top nibble
uint8_t hex2ascii_l ( uint8_t  hexval  ) 

Convert the low nibble of a supplied byte into an ASCII character.

Parameters:
hexval Hex value
Returns:
ASCII character of bottom nibble
void iso_time ( uint8_t *  time_store,
uint8_t *  iso_time_store 
)

Converts a time stored in memory (typically from a Real Time Clock chip) into ISO order.

Create an ISO time string from a set of data. DOESN'T READ THE RTC.

Parameters:
*time_store memory location of 7 bytes of the time to be converted.
*iso_time_store memory location for the 16 byte result.

Note that this function only converts. It doesn't update *time_store with the correct time

time_store format is ss,mm,hh,dd,mm,dow,yy yy counts from 2000 iso_time_store shows "20yymmdd hhmmss" 16 bytes including trailing null making it a string.

void read_from_rtc ( uint8_t  command,
uint8_t  bytes,
uint8_t *  address_ptr 
)

Read a set of data from the DS1302 Real time clock to memory.

Parameters:
command Command to send to RTC
bytes Number of bytes to be returned
*address_ptr Pointer to location to store returned values
void read_time ( uint8_t *  address_ptr  ) 

Read a set of data (7 bytes) from the DS1302 Real time clock into memory Order is ss,mm,hh,dd,mm,dow,yy.

Parameters:
*address_ptr Pointer to location in memory of first byte of set of data
void rtc_clock_up_down ( void   )  [inline]
void send_time_to_rtc ( uint8_t *  time_ptr  ) 

Set the clock on the RTC, sending it 8 bytes ss mm hh date month day year 0.

Parameters:
*time_ptr Pointer to location where clock set values are stored

Clock values are 8 bytes: ss,mm,hh,dd,mm,dow,yy,0;

void set_datetime ( uint8_t *  iso_time_store  ) 

Take in a pointer to time in ISO format (15 bytes) eg 20101203 141604 and set RTC to that time.

Take in a pointer to time in ISO format (15 bytes) eg 20101203 141604 and set RTC to that time. Time format is xxyymmddxhhmmss where xx and x are don't care Time format does not need to be null terminated Day of week on RTC is set to 1, as I never use dow.

Parameters:
*iso_time_store pointer to memory location holding time as 15 bytes.
void write_to_rtc ( uint8_t  command,
uint8_t  bytes,
uint8_t *  data_ptr 
)

Write a set of data to the DS1302 Real time clock from memory.

Parameters:
command Command to send to RTC. See datasheet of the device for details
bytes Number of bytes to be sent
*data_ptr Pointer to location in memory of first byte of set of data

There is no checking that the data is sensible, or that bytes is the correct number for command