Code: Select all
// RTCcustom.c <stanigator@gmail.com>
// Created: Dec 24, 2007
// Modified: Dec 26, 2007
// This contains function definitions of Real-Time Clock handling
// routines on DS1302 chip
#include "typedefs.h"
#include <built_in.h>
#include "RTCcustom.h"
static unsigned short *port; // port that contains PIC port controlling RTC
static unsigned short pinIO = 0; // pin on port controlling data pin of RTC
static unsigned short pinCE = 0; // pin on port controlling clock enable pin of RTC
static unsigned short pinSCLK = 0; // pin on port controlling SPI clock of RTC
void setRTCpin( unsigned short *RTCport,
unsigned short IOpin,
unsigned short CEpin,
unsigned short SCLKpin)
{
port = RTCport;
pinIO = IOpin;
pinCE = CEpin;
pinSCLK = SCLKpin;
}
unsigned int MPORTreturn(void)
{
return (int) &port;
}
unsigned short MIOreturn(void)
{
return pinIO;
}
unsigned short MCEreturn(void)
{
return pinCE;
}
unsigned short MSCLKreturn(void)
{
return pinSCLK;
}
I have a main driver source code called dls.c calling setRTCpin() from RTCcustom.c file as shown below:
Code: Select all
// Data Logging Scale <stanigator@gmail.com>
// Created: Nov 3, 2007
// Revised: Dec 27, 2007
#include <built_in.h>
#include "typedefs.h"
#include "LCD4.h"
#include "RTCcustom.h"
#define BUFFER_SIZE 10
void delay(uint number)
{
while(--number > 0);
}
void main() {
char text_array[BUFFER_SIZE]; // array carrying text
int n; // counter for data to text conversion
OSCCON = 0x70; /* internal oscillator at 8 Mhz, system clock = internal oscillator*/
OSCTUNE = 0x00; /* oscillator tuning register at max frequency */
CMCON = 7; // turn off comparators
Delay_ms(20); // system wide delay of 20 ms
ADCON1 = 0x0e; // Configure Vref and AN0
TRISA = 0x01; // PORTA is output except RA0 (AN0)
TRISB = 0x00; // all PORTB pins are output
TRISC = 0x00; // PORTC is outputs
LCD4_INIT(); // initialize the LCD
setRTCpin(&PORTA, 1, 2, 3);
while(1)
{
// Convert Real-time Clock Data Pin from data to text
n = sprintf(text_array, "%u", MIOreturn());
// exit execution if the conversion contains an error
// making n == NULL
if (n < 0 || n >= BUFFER_SIZE)
{
break;
}
LCD4_CMD(128);
LCD4_OUT(text_array);
LCD4_OUT(" ");
// Convert Real-time Clock chip enable Pin from data to text
n = sprintf(text_array, "%u", MCEreturn());
// exit execution if the conversion contains an error
// making n == NULL
if (n < 0 || n >= BUFFER_SIZE)
{
break;
}
LCD4_CMD(134);
LCD4_OUT(text_array);
LCD4_OUT(" ");
// Convert Real-time Clock SPI Clock Pin from data to text
n = sprintf(text_array, "%u", MSCLKreturn());
// exit execution if the conversion contains an error
// making n == NULL
if (n < 0 || n >= BUFFER_SIZE)
{
break;
}
LCD4_CMD(140);
LCD4_OUT(text_array);
LCD4_OUT(" ");
/*// Convert Real-time Clock Port address from data to text
n = sprintf(text_array, "%u", MPORTreturn());
// exit execution if the conversion contains an error
// making n == NULL
if (n < 0 || n >= BUFFER_SIZE)
{
break;
}
LCD4_CMD(192);
LCD4_OUT(text_array);
LCD4_OUT(" ");*/
}
}