HI folks,
I have been trying to connect a SRF08 Ultra sonic sensor to the PIC18F4520 using the i2c bus.
I have been trying for a long time however i keep getting a result of FFFF. I have attached the c code and would apperciate it if someone could let me know where i am going wrong.
I am developing my code under mplab software. using the matrix multimedia board. Which i am trying to output it on the leds attachment of the board.
The only thing works
i got led flashing on the srf08 sensor but dont no what does that mean!!! which is connected to port c on the matrix multimedia board and i am trying to use port d to output the vaule which i am stuck on!!!!
MY CODE
I have been trying to connect a SRF08 Ultra sonic sensor to the PIC18F4520 using the i2c bus.
I have been trying for a long time however i keep getting a result of FFFF. I have attached the c code and would apperciate it if someone could let me know where i am going wrong.
I am developing my code under mplab software. using the matrix multimedia board. Which i am trying to output it on the leds attachment of the board.
The only thing works
i got led flashing on the srf08 sensor but dont no what does that mean!!! which is connected to port c on the matrix multimedia board and i am trying to use port d to output the vaule which i am stuck on!!!!
MY CODE
Rich (BB code):
// BY AFZAL MIAH 2012
// Matrix Multiprogrammer at 19.6608MHz
#include <p18f4520.h>
#include <usart.h>
#include <stdio.h>
#include <delays.h>
#include <I2C.h>
// 18f4520 settings
#pragma config OSC=HSPLL,PWRT=ON,WDT=OFF,BOREN=OFF,MCLRE=ON,PBADEN=OFF
#pragma config CCP2MX=PORTC,STVREN=ON,LVP=OFF,XINST=OFF,DEBUG=ON
#define SRF08 0xE2 // factory default
#define SSPENB 0b00100000 /* Enable serial port and configures SCK, SDO, SDI*/
unsigned int get_range(unsigned int);
void write_I2C (char ,char ,char );
void main(void)
{
unsigned int range;
TRISD=0;
OpenUSART(USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_LOW,15); //19200 from 19.660800MHz
OpenI2C(MASTER,SLEW_OFF);
SSPADD = 48; // baud rate in address reg 100k at 19.6608Mhz, clock = Fosc/(4*(SSPADD+1))
putcUSART(0x0c);
printf((rom char *)"Test Program for SRF10: AJW 30/3/07\r\n\n");
printf((rom char *)"Front(cm)\r\n");
Delay10KTCYx(25); // 25ms delay
write_I2C(SRF08,1,8); // set max gain to 140
Delay10KTCYx(25); // 25ms delay
write_I2C(SRF08,2,36); // set range to 1.5m
Delay10KTCYx(25); // 25ms delay
while(1) {
// loop forever
// range = get_range(SRF08); // get range from srf10 (round trip flight time in 1uS units)
range = get_range(SRF08);
TRISD=range/255;
printf((rom char *)"%6d\r\n", range*10/58); // convert to mm
printf((rom char *)"%6d\r\n", range); // convert to mm
}
}
unsigned int get_range(unsigned int addr)
{
unsigned int range;
// This sends the ranging command to the SRF08 to start it ranging
write_I2C(addr,0,0x51);
Delay10KTCYx(50); // at 19.6608MHz, 1 cycle = approx. 200ns, so 10KTCYx(50) = 100ms
// next, get the range from the SRF08
SSPCON2bits.SEN = 1; // send start bit
while(SSPCON2bits.SEN); // and wait for it to clear
SSPCON2bits.ACKDT = 0; // acknowledge bit
PIR1bits.SSPIF = 0;
SSPBUF = addr; // SRF08 I2C address
while(!PIR1bits.SSPIF); // wait for interrupt
PIR1bits.SSPIF = 0; // then clear it.
SSPBUF = 2; // address of register to read from - high byte of result
while(!PIR1bits.SSPIF); //
PIR1bits.SSPIF = 0; //
SSPCON2bits.RSEN = 1; // send repeated start bit
while(SSPCON2bits.RSEN); // and wait for it to clear
PIR1bits.SSPIF = 0; //
SSPBUF = addr+1; // SRF08 I2C address - the read bit is set this time
while(!PIR1bits.SSPIF); // wait for interrupt
PIR1bits.SSPIF = 0; // then clear it.
while(SSPCON2&0x1f); // wait for IDLE state before setting RCEN (work around in errata)
SSPCON2bits.RCEN = 1; // start receiving
while(!SSPSTATbits.BF); // wait for high byte of range
range = ((int)SSPBUF)<<8; // and get it
SSPCON2bits.ACKEN = 1; // start acknowledge sequence
while(SSPCON2bits.ACKEN); // wait for ack. sequence to end
while(SSPCON2&0x1f); // wait for IDLE state before setting RCEN (work around in errata)
SSPCON2bits.RCEN = 1; // start receiving
while(!SSPSTATbits.BF); // wait for low byte of range
range += SSPBUF; // and get it
SSPCON2bits.ACKDT = 1; // not acknowledge for last byte
SSPCON2bits.ACKEN = 1; // start acknowledge sequence
while(SSPCON2bits.ACKEN); // wait for ack. sequence to end
SSPCON2bits.PEN = 1; // send stop bit
while(SSPCON2bits.PEN); //
return range;
}
void write_I2C (char addr,char reg,char cmd)
{
SSPCON2bits.SEN = 1; // send start bit
while(SSPCON2bits.SEN); // and wait for it to clear
PIR1bits.SSPIF = 0;
SSPBUF = addr; // SRF08 I2C address
while(!PIR1bits.SSPIF); // wait for interrupt
PIR1bits.SSPIF = 0; // then clear it.
SSPBUF = reg; // address of register to write to
while(!PIR1bits.SSPIF); //
PIR1bits.SSPIF = 0; //
SSPBUF = cmd; // command
while(!PIR1bits.SSPIF); //
PIR1bits.SSPIF = 0; //
SSPCON2bits.PEN = 1; // send stop bit
while(SSPCON2bits.PEN); //
}
Last edited by a moderator: