\hypertarget{ds1302_8c}{
\section{ds1302.c File Reference}
\label{ds1302_8c}\index{ds1302.c@{ds1302.c}}
}
{\tt \#include $<$ds1302.h$>$}\par
{\tt \#include $<$util/delay\_\-basic.h$>$}\par
{\tt \#include $<$util/delay.h$>$}\par
\subsection*{Functions}
\begin{CompactItemize}
\item 
void \hyperlink{ds1302_8c_bca8f18f7639500cd6a376df5483d96b}{rtc\_\-clock\_\-up\_\-down} (void)
\item 
void \hyperlink{ds1302_8c_801ff91e864a53bce4df8f3fbe78fc36}{read\_\-from\_\-rtc} (uint8\_\-t command, uint8\_\-t bytes, uint8\_\-t $\ast$address\_\-ptr)
\begin{CompactList}\small\item\em Read a set of data from the DS1302 Real time clock to memory. \item\end{CompactList}\item 
void \hyperlink{ds1302_8c_4ca5f5739aa4f67dcd686ff9bd18f999}{write\_\-to\_\-rtc} (uint8\_\-t command, uint8\_\-t bytes, uint8\_\-t $\ast$data\_\-ptr)
\begin{CompactList}\small\item\em Write a set of data to the DS1302 Real time clock from memory. \item\end{CompactList}\item 
void \hyperlink{ds1302_8c_1be18823028e4c5e0de6b4a6935a06ef}{read\_\-time} (uint8\_\-t $\ast$address\_\-ptr)
\begin{CompactList}\small\item\em Read a set of data from the DS1302 Real time clock into memory. \item\end{CompactList}\item 
void \hyperlink{ds1302_8c_973f402d4f02f05fc570d46127ad8f2b}{send\_\-time\_\-to\_\-rtc} (uint8\_\-t $\ast$time\_\-ptr)
\begin{CompactList}\small\item\em Set the clock on the RTC, sending it 8 bytes ss mm hh date month day year 0. \item\end{CompactList}\item 
uint8\_\-t \hyperlink{ds1302_8c_2f379772b393d02eab966d60b3491916}{bcd\_\-to\_\-hex} (uint8\_\-t bcd)
\begin{CompactList}\small\item\em Convert a BCD value in a byte to the correct hex value. \item\end{CompactList}\item 
uint32\_\-t \hyperlink{ds1302_8c_d0c62071669b119f418fd895941a36af}{get\_\-fattime} ()
\begin{CompactList}\small\item\em Read the time and convert to a 32 bit value suitable for FAT filesystem timestamps. \item\end{CompactList}\item 
void \hyperlink{ds1302_8c_daa9db54674bd946656f85c26ee314c4}{date\_\-dirname} (uint8\_\-t $\ast$time\_\-store, uint8\_\-t $\ast$dirname)
\begin{CompactList}\small\item\em Create a 7 byte directory name with date/time value. \item\end{CompactList}\item 
void \hyperlink{ds1302_8c_e6f9bcc843e553bbd1dc28214f9666d9}{filename\_\-from\_\-date} (uint8\_\-t $\ast$time\_\-store, uint8\_\-t $\ast$filename)
\begin{CompactList}\small\item\em Create or overwrite a filename with date/time value. \item\end{CompactList}\item 
void \hyperlink{ds1302_8c_07630d75a7323cc856505f9013888646}{iso\_\-time} (uint8\_\-t $\ast$time\_\-store, uint8\_\-t $\ast$iso\_\-time\_\-store)
\begin{CompactList}\small\item\em Converts a time stored in memory (typically from a Real Time Clock chip) into ISO order. \item\end{CompactList}\item 
uint8\_\-t \hyperlink{ds1302_8c_f52153b03b1a05fea5b4eb3eadf52a25}{hex2ascii\_\-h} (uint8\_\-t hexval)
\begin{CompactList}\small\item\em Convert the top nibble of a btye to ASCII. \item\end{CompactList}\item 
uint8\_\-t \hyperlink{ds1302_8c_68043f0b43c0d8b5f39a370f055e7a74}{hex2ascii\_\-l} (uint8\_\-t hexval)
\begin{CompactList}\small\item\em Convert the bottom nibble of a btye to ASCII. \item\end{CompactList}\end{CompactItemize}


\subsection{Function Documentation}
\hypertarget{ds1302_8c_2f379772b393d02eab966d60b3491916}{
\index{ds1302.c@{ds1302.c}!bcd\_\-to\_\-hex@{bcd\_\-to\_\-hex}}
\index{bcd\_\-to\_\-hex@{bcd\_\-to\_\-hex}!ds1302.c@{ds1302.c}}
\subsubsection[{bcd\_\-to\_\-hex}]{\setlength{\rightskip}{0pt plus 5cm}uint8\_\-t bcd\_\-to\_\-hex (uint8\_\-t {\em bcd})}}
\label{ds1302_8c_2f379772b393d02eab966d60b3491916}


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

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em bcd}]uint8\_\-t value in BCD \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]returns uint8\_\-t hex value converted from BCD\end{Desc}
For example, 23 -$>$ 0x17 \hypertarget{ds1302_8c_daa9db54674bd946656f85c26ee314c4}{
\index{ds1302.c@{ds1302.c}!date\_\-dirname@{date\_\-dirname}}
\index{date\_\-dirname@{date\_\-dirname}!ds1302.c@{ds1302.c}}
\subsubsection[{date\_\-dirname}]{\setlength{\rightskip}{0pt plus 5cm}void date\_\-dirname (uint8\_\-t $\ast$ {\em time\_\-store}, \/  uint8\_\-t $\ast$ {\em dirname})}}
\label{ds1302_8c_daa9db54674bd946656f85c26ee314c4}


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

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em $\ast$time\_\-store}]memory location of 7 bytes of the time to be converted. \item[{\em $\ast$dirname}]directory name to be created.\end{description}
\end{Desc}
Note that this function only converts. It doesn't update $\ast$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 \hypertarget{ds1302_8c_e6f9bcc843e553bbd1dc28214f9666d9}{
\index{ds1302.c@{ds1302.c}!filename\_\-from\_\-date@{filename\_\-from\_\-date}}
\index{filename\_\-from\_\-date@{filename\_\-from\_\-date}!ds1302.c@{ds1302.c}}
\subsubsection[{filename\_\-from\_\-date}]{\setlength{\rightskip}{0pt plus 5cm}void filename\_\-from\_\-date (uint8\_\-t $\ast$ {\em time\_\-store}, \/  uint8\_\-t $\ast$ {\em filename})}}
\label{ds1302_8c_e6f9bcc843e553bbd1dc28214f9666d9}


Create or overwrite a filename with date/time value. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em $\ast$time\_\-store}]memory location of 7 bytes of the time to be converted. \item[{\em $\ast$filename}]filename name to be created or overwritten.\end{description}
\end{Desc}
Note that this function only converts. It doesn't update $\ast$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 $^\wedge$mddhhmm$^\wedge$ss$^\wedge$ where positions with $^\wedge$ 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. \hypertarget{ds1302_8c_d0c62071669b119f418fd895941a36af}{
\index{ds1302.c@{ds1302.c}!get\_\-fattime@{get\_\-fattime}}
\index{get\_\-fattime@{get\_\-fattime}!ds1302.c@{ds1302.c}}
\subsubsection[{get\_\-fattime}]{\setlength{\rightskip}{0pt plus 5cm}uint32\_\-t get\_\-fattime ()}}
\label{ds1302_8c_d0c62071669b119f418fd895941a36af}


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

\begin{Desc}
\item[Returns:]uint32\_\-t FAT time encoded in a 32 bit value\end{Desc}
See \hyperlink{structFATFS}{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 \hypertarget{ds1302_8c_f52153b03b1a05fea5b4eb3eadf52a25}{
\index{ds1302.c@{ds1302.c}!hex2ascii\_\-h@{hex2ascii\_\-h}}
\index{hex2ascii\_\-h@{hex2ascii\_\-h}!ds1302.c@{ds1302.c}}
\subsubsection[{hex2ascii\_\-h}]{\setlength{\rightskip}{0pt plus 5cm}uint8\_\-t hex2ascii\_\-h (uint8\_\-t {\em hexval})}}
\label{ds1302_8c_f52153b03b1a05fea5b4eb3eadf52a25}


Convert the top nibble of a btye to ASCII. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hexval}]8 bit value whose top 4 bits are to be converted to ASCII \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]uint8\_\-t Converted value \end{Desc}
\hypertarget{ds1302_8c_68043f0b43c0d8b5f39a370f055e7a74}{
\index{ds1302.c@{ds1302.c}!hex2ascii\_\-l@{hex2ascii\_\-l}}
\index{hex2ascii\_\-l@{hex2ascii\_\-l}!ds1302.c@{ds1302.c}}
\subsubsection[{hex2ascii\_\-l}]{\setlength{\rightskip}{0pt plus 5cm}uint8\_\-t hex2ascii\_\-l (uint8\_\-t {\em hexval})}}
\label{ds1302_8c_68043f0b43c0d8b5f39a370f055e7a74}


Convert the bottom nibble of a btye to ASCII. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hexval}]8 bit value whose bottom 4 bits are to be converted to ASCII \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]uint8\_\-t Converted value \end{Desc}
\hypertarget{ds1302_8c_07630d75a7323cc856505f9013888646}{
\index{ds1302.c@{ds1302.c}!iso\_\-time@{iso\_\-time}}
\index{iso\_\-time@{iso\_\-time}!ds1302.c@{ds1302.c}}
\subsubsection[{iso\_\-time}]{\setlength{\rightskip}{0pt plus 5cm}void iso\_\-time (uint8\_\-t $\ast$ {\em time\_\-store}, \/  uint8\_\-t $\ast$ {\em iso\_\-time\_\-store})}}
\label{ds1302_8c_07630d75a7323cc856505f9013888646}


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

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em $\ast$time\_\-store}]memory location of 7 bytes of the time to be converted. \item[{\em $\ast$iso\_\-time\_\-store}]memory location for the 16 byte result.\end{description}
\end{Desc}
Note that this function only converts. It doesn't update $\ast$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 \char`\"{}20yymmdd hhmmss\char`\"{} 16 bytes including trailing null making it a string. \hypertarget{ds1302_8c_801ff91e864a53bce4df8f3fbe78fc36}{
\index{ds1302.c@{ds1302.c}!read\_\-from\_\-rtc@{read\_\-from\_\-rtc}}
\index{read\_\-from\_\-rtc@{read\_\-from\_\-rtc}!ds1302.c@{ds1302.c}}
\subsubsection[{read\_\-from\_\-rtc}]{\setlength{\rightskip}{0pt plus 5cm}void read\_\-from\_\-rtc (uint8\_\-t {\em command}, \/  uint8\_\-t {\em bytes}, \/  uint8\_\-t $\ast$ {\em address\_\-ptr})}}
\label{ds1302_8c_801ff91e864a53bce4df8f3fbe78fc36}


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

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em command}]Command to send to RTC \item[{\em bytes}]Number of bytes to be returned \item[{\em $\ast$address\_\-ptr}]Pointer to location to store returned values \end{description}
\end{Desc}
\hypertarget{ds1302_8c_1be18823028e4c5e0de6b4a6935a06ef}{
\index{ds1302.c@{ds1302.c}!read\_\-time@{read\_\-time}}
\index{read\_\-time@{read\_\-time}!ds1302.c@{ds1302.c}}
\subsubsection[{read\_\-time}]{\setlength{\rightskip}{0pt plus 5cm}void read\_\-time (uint8\_\-t $\ast$ {\em address\_\-ptr})}}
\label{ds1302_8c_1be18823028e4c5e0de6b4a6935a06ef}


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

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em $\ast$address\_\-ptr}]Pointer to location in memory of first byte of set of data \end{description}
\end{Desc}
\hypertarget{ds1302_8c_bca8f18f7639500cd6a376df5483d96b}{
\index{ds1302.c@{ds1302.c}!rtc\_\-clock\_\-up\_\-down@{rtc\_\-clock\_\-up\_\-down}}
\index{rtc\_\-clock\_\-up\_\-down@{rtc\_\-clock\_\-up\_\-down}!ds1302.c@{ds1302.c}}
\subsubsection[{rtc\_\-clock\_\-up\_\-down}]{\setlength{\rightskip}{0pt plus 5cm}void rtc\_\-clock\_\-up\_\-down (void)\hspace{0.3cm}{\tt  \mbox{[}inline\mbox{]}}}}
\label{ds1302_8c_bca8f18f7639500cd6a376df5483d96b}


\hypertarget{ds1302_8c_973f402d4f02f05fc570d46127ad8f2b}{
\index{ds1302.c@{ds1302.c}!send\_\-time\_\-to\_\-rtc@{send\_\-time\_\-to\_\-rtc}}
\index{send\_\-time\_\-to\_\-rtc@{send\_\-time\_\-to\_\-rtc}!ds1302.c@{ds1302.c}}
\subsubsection[{send\_\-time\_\-to\_\-rtc}]{\setlength{\rightskip}{0pt plus 5cm}void send\_\-time\_\-to\_\-rtc (uint8\_\-t $\ast$ {\em time\_\-ptr})}}
\label{ds1302_8c_973f402d4f02f05fc570d46127ad8f2b}


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

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em $\ast$time\_\-ptr}]Pointer to location where clock set values are stored\end{description}
\end{Desc}
Clock values are 8 bytes: ss,mm,hh,dd,mm,dow,yy,0; \hypertarget{ds1302_8c_4ca5f5739aa4f67dcd686ff9bd18f999}{
\index{ds1302.c@{ds1302.c}!write\_\-to\_\-rtc@{write\_\-to\_\-rtc}}
\index{write\_\-to\_\-rtc@{write\_\-to\_\-rtc}!ds1302.c@{ds1302.c}}
\subsubsection[{write\_\-to\_\-rtc}]{\setlength{\rightskip}{0pt plus 5cm}void write\_\-to\_\-rtc (uint8\_\-t {\em command}, \/  uint8\_\-t {\em bytes}, \/  uint8\_\-t $\ast$ {\em data\_\-ptr})}}
\label{ds1302_8c_4ca5f5739aa4f67dcd686ff9bd18f999}


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

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em command}]Command to send to RTC. See datasheet of the device for details \item[{\em bytes}]Number of bytes to be sent \item[{\em $\ast$data\_\-ptr}]Pointer to location in memory of first byte of set of data\end{description}
\end{Desc}
There is no checking that the data is sensible, or that bytes is the correct number for command 