• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
Resource icon

C18 code for interfacing a PIC to a DS 12885 / DS 12887 2011-02-26

Hi, this is a code snippet for writing to and reading from the registers of a BQ3287(DS12885-DS12C887A) Real Time Clock using INTEL timing, it has been tested using 16Mhz, 4Mhz, 10Mhz XTALs.

Code:
//Cut and paste these register definitions in your main code, 
//add this C file and call the read and write functions
//whenever a Read or Write operation is to be performed.The syntax of the functions is:
//write_RTC(address,data);
//data=read_RTC(address);
// where data and address are 8 bit unsigned characters.
//This snippet has been tested using an 18F4620 and uses PORTD as the Data Bus,
//RE0 for Address strobe,RE1 for RW and RE2 for Data strobe. You can change this by
//changing the Pin Definitions


/****************************Register Definitions**************************************/
#define sec 0
#define sec_alarm 1
#define min 2
#define min_alarm 3
#define hours 4
#define hour_alarm 5
#define day_week 6
#define day_month 7
#define month 8
#define year 9
#define REGA 10
#define REGB 11
#define REGC 12
#define REGD 13 
void write_RTC(unsigned char, unsigned char );
unsigned char read_RTC(unsigned char);	
/*************************************************************************************/
	
/*******************Pin Definitions of the RTC****************************************/
#define ADwritebus LATD           			//Write Bus for the RTC
#define AS LATEbits.LATE0         			//Address Strobe line for the RTC
#define DS LATEbits.LATE2         			//Data Strobe line for the RTC 
#define RW_bar LATEbits.LATE1     			//Read/Write signal for the RTC
#define ADreadbus PORTD           			//Read Bus for the RTC
#define DBus_Direction TRISD      			//Bus direction setter for Data Bus
#define AS_Direction TRISEbits.TRISE0           //Bus direction setter for Data Bus
#define DS_Direction TRISEbits.TRISE2      			//Bus direction setter for Data Bus
#define RW_bar_Direction TRISEbits.TRISE1     	//Bus direction setter for Data Bus
/*************************************************************************************/


void write_RTC(unsigned char RTCwaddress, unsigned char RTCwdata)
{	
	AS_Direction=0;		 //Make all control signal pins as outputs
	DS_Direction=0;
	RW_bar_Direction=0;
	DBus_Direction=0;        //Make data bus an output bus since this is a write operation
	DS=1;
	RW_bar=1;
	AS=1;
	ADwritebus=RTCwaddress;
	_asm nop _endasm
	AS=0;  //Latch address		
	_asm nop _endasm
	ADwritebus=0;		//Make address/data line zero		
	RW_bar=0;               
	_asm nop _endasm
	ADwritebus=RTCwdata;	//write data on specific address	
	_asm nop _endasm
	RW_bar=1;	            //Enable writing the data.
	_asm NOP _endasm
	ADwritebus=0;
	AS=1;
}
unsigned char read_RTC(unsigned char RTCraddress)
{	
	
	unsigned char *pd;
	AS_Direction=0;		 //Make all control signal pins as outputs
	DS_Direction=0;
	RW_bar_Direction=0;
	DBus_Direction=0;       //Make data bus an output bus for address writing
	DS=1;
	RW_bar=1;
	AS=1;
	ADwritebus=RTCraddress;
	_asm nop _endasm
	AS=0;
	DBus_Direction=0xff;   //Make data bus an input bus for data reading
	_asm nop _endasm
	DS=0;
	_asm nop _endasm
	*pd=ADreadbus;
	DS=1;
	_asm NOP _endasm
	AS=1;
	_asm NOP _endasm

	return(*pd);
}
The Read and Write cycles generated by this code are attached.
Author
Wond3rboy
Views
1,483
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from Wond3rboy

Latest threads

EE World Online Articles

Loading
Top