This is the program that works with PIC processors. I am not using a PIC processor, im using the mini-max 8051 microcontroller with the DIO Expander from
https://www.bipom.com/minimax51-set.shtm. If anyone has any suggestions on how to modify this program please let me know. Thanks for all your help.
Ben L.
#include <8051io.h>
#include <8051bit.h> /* Bit set/clear macros */
#include <8051reg.h>
#define SCL_IN RB4 //???needs to be adjusted
#define SDA_IN RB1 // ???
#define SCL TRISB4 //I2C Bus
#define SDA TRISB1
//#define SS P1.5
//#define PIC_ADDR 0xB0
void i2c_start(void);
void i2c_dly(void);
void i2c_stop(void);
unsigned char i2c_rx(char ack);
unsigned char i2c_tx(unsigned char d);
extern bit i2cbusy;
//setbit (SDA);
//SCL = 1;
//SCL_IN = SDA_IN = 0;
void main()
{
unsigned char rangehigh, rangelow;
while(1)
{
i2c_start(); // send start sequence
i2c_tx(0xE0); // SRF10 I2C start up address with R/W bit
clear
i2c_tx(0x00); // SRF10 command register address
i2c_tx(0x50); // command to start ranging in inches
i2c_stop(); // send stop sequence
i2c_start(); // send start sequence
i2c_tx(0xE0); // SRF10 I2C address with R/W bit clear
i2c_start(); // send a restart sequence
i2c_tx(0xE1); // SRF10 I2C address with R/W bit set
rangehigh = i2c_rx(1); // get the high byte of the range and send
acknowledge.
rangelow = i2c_rx(0); // get low byte of the range - note we don't
acknowledge the last byte.
i2c_stop(); // send stop sequence
}
}
void i2c_start(void)
{
setbit (SDA); // i2c start bit sequence
i2c_dly(); //
setbit (SCL);
i2c_dly();
clrbit (SDA);
i2c_dly();
clrbit (SCL);
i2c_dly();
}
void i2c_stop(void)
{
clrbit (SDA); // i2c stop bit sequence
i2c_dly();
setbit (SCL);
i2c_dly();
setbit (SDA);
i2c_dly();
}
void i2c_dly(void)
{
}
unsigned char i2c_rx(char ack) //ack = acknowledgement bit
{
char x,d;
d=0;
setbit (SDA);
for(x=0; x<8; x++)
{
d <<= 1;
do {
setbit (SCL);
}
while(clrbit(SCL_IN)); // wait for any SCL clock stretching
i2c_dly();
if(SDA_IN) d |= 1
clrbit (SCL);
}
if(ack) clrbit (SDA);
else setbit (SDA);
setbit (SCL);
i2c_dly(); // send (N)ACK bit
clrbit (SCL);
setbit (SDA);
return d;
}
unsigned char i2c_tx(unsigned char d) //function to receive data
{
char x;
static bit b;
for(x=8; x; x--) {
if(d&0x80) setbit (SDA);
else clrbit (SDA);
sebit (SCL);
d <<= 1;
clrbit (SCL);
}
setbit (SDA);
setbit (SCL);
i2c_dly();
b = SDA_IN; // possible ACK bit
clrbit (SCL);
return b;
}