/* Delay functions taken from PICC examples I2C functions assume use of onboard i2c */ /* * Delay functions for HI-TECH C on the PIC * * Functions available: * DelayUs(x) Delay specified number of microseconds * DelayMs(x) Delay specified number of milliseconds * * Note that there are range limits: x must not exceed 255 - for xtal * frequencies > 12MHz the range for DelayUs is even smaller. * To use DelayUs it is only necessary to include this file; to use * DelayMs you must include delay.c in your project. * */ /* I2C Functions i2c_idle(); -- waits for idle condition on I2C interface i2c_Start(); -- sends start and waits for finish of start extern i2c_Stop(); -- sends stop and waits for finish of stop i2c_doAck(); -- send Ack and waits for finish of Ack i2c_doNak(); -- send Nck and waits for finish of Ack i2c_WriteTo(unsigned char addr); -- initiates i2c cycle to device at addr i2c_PutByte(unsigned char byte); -- sends byte on i2c bus, check idle first i2c_FastPutByte(unsigned char byte); -- same as previous, do not check idle unsigned char i2c_GetByte(); -- read byte from i2c bus */ /* Microchip 24LC515 Functions Random Write - writes one byte (val) to RAM designated by 'cmd' to 'addr'. Waits for write to finish via 5 ms delay void mem_write(unsigned char cmd,unsigned char val, int addr); Block Write - writes one block of bytes (64 byte buffer pointed to by 'buf' ) to RAM designated by 'cmd' starting at 'addr'. DOES NOT WAIT FOR WRITE TO FINISH upon return, so you either have to check for end-of-write or delay for 5 ms before calling this again. void block_mem_write(unsigned char cmd,int addr, char *buf); Random Read - writes one byte from RAM designated by 'cmd' from 'addr'. unsigned char mem_read(unsigned char cmd,int addr); Block Read - reads one block of bytes from RAM designated by 'cmd' starting at 'addr'; 64 bytes returns in 'buf'. extern void block_mem_read(unsigned char cmd,int addr,char *buf); MISSING FUNCTIONS There should be some functions that check for end of write... */ #if 0 #define XTAL_FREQ 3686L /* Crystal frequency in KHZ*/ #endif #define XTAL_FREQ 14745L /* Crystal frequency in KHZ*/ #define MHZ *1000L /* number of kHz in a MHz */ #define KHZ *1 /* number of kHz in a kHz */ #if XTAL_FREQ >= 12MHZ #define DelayUs(x) { unsigned char _dcnt; \ _dcnt = (x)*((XTAL_FREQ)/(12MHZ)); \ while(--_dcnt != 0) \ continue; } #else #define DelayUs(x) { unsigned char _dcnt; \ _dcnt = (x)/((12MHZ)/(XTAL_FREQ))|1; \ while(--_dcnt != 0) \ continue; } #endif extern void DelayMs(unsigned char); extern i2c_idle(); extern i2c_Start(); extern i2c_Stop(); extern i2c_doAck(); extern i2c_doNak(); extern i2c_WriteTo(unsigned char addr); extern i2c_PutByte(unsigned char byte); extern i2c_FastPutByte(unsigned char byte); extern unsigned char i2c_GetByte(); extern void mem_write(unsigned char cmd,unsigned char val, int addr); extern void block_mem_write(unsigned char cmd,int addr, char *buf); extern unsigned char mem_read(unsigned char cmd,int addr); extern void block_mem_read(unsigned char cmd,int addr,char *buf);