SRF08 + PIC18F4520 + matrix multimedia Board

Discussion in 'Embedded Systems and Microcontrollers' started by afz786miah, Mar 17, 2012.

  1. afz786miah

    Thread Starter New Member

    Mar 16, 2012
    2
    0
    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!!!! :confused:

    MY CODE

    Code ( (Unknown Language)):
    1.  
    2. // BY AFZAL MIAH 2012
    3. // Matrix Multiprogrammer at 19.6608MHz
    4. #include <p18f4520.h>
    5. #include <usart.h>
    6. #include <stdio.h>
    7. #include <delays.h>
    8. #include <I2C.h>
    9.  
    10. // 18f4520 settings
    11. #pragma config OSC=HSPLL,PWRT=ON,WDT=OFF,BOREN=OFF,MCLRE=ON,PBADEN=OFF
    12. #pragma config CCP2MX=PORTC,STVREN=ON,LVP=OFF,XINST=OFF,DEBUG=ON
    13. #define SRF08 0xE2 // factory default
    14. #define   SSPENB       0b00100000   /* Enable serial port and configures SCK, SDO, SDI*/
    15. unsigned int get_range(unsigned int);
    16. void write_I2C (char ,char ,char );
    17.  
    18. void main(void)
    19. {
    20.  
    21.  unsigned int range;
    22. TRISD=0;
    23.  OpenUSART(USART_TX_INT_OFF  &
    24.       USART_RX_INT_OFF  &
    25.       USART_ASYNCH_MODE &
    26.       USART_EIGHT_BIT   &
    27.       USART_CONT_RX &
    28.       USART_BRGH_LOW,15); //19200 from 19.660800MHz
    29.      
    30.    OpenI2C(MASTER,SLEW_OFF);
    31.    SSPADD = 48;      // baud rate in address reg 100k at 19.6608Mhz, clock = Fosc/(4*(SSPADD+1))
    32.    putcUSART(0x0c);
    33.  
    34.    printf((rom char *)"Test Program for SRF10: AJW 30/3/07\r\n\n");
    35.    printf((rom char *)"Front(cm)\r\n");
    36.    
    37.    Delay10KTCYx(25);   // 25ms delay
    38.    write_I2C(SRF08,1,8);  // set max gain to 140
    39.    Delay10KTCYx(25);   // 25ms delay
    40.    write_I2C(SRF08,2,36);  // set range to 1.5m
    41.  Delay10KTCYx(25);   // 25ms delay
    42.  
    43.  while(1) {
    44.                     // loop forever
    45.  // range = get_range(SRF08);       // get range from srf10 (round trip flight time in 1uS units)
    46.         range = get_range(SRF08);
    47.  TRISD=range/255;
    48.   printf((rom char *)"%6d\r\n", range*10/58); // convert to mm
    49.  printf((rom char *)"%6d\r\n", range); // convert to mm
    50.  }
    51. }
    52.  
    53. unsigned int get_range(unsigned int addr)
    54. {
    55. unsigned int range;
    56. // This sends the ranging command to the SRF08 to start it ranging
    57.  write_I2C(addr,0,0x51);
    58.  Delay10KTCYx(50); // at 19.6608MHz, 1 cycle = approx. 200ns, so 10KTCYx(50) = 100ms
    59.      
    60. // next, get the range from the SRF08
    61.  SSPCON2bits.SEN = 1;   // send start bit
    62.  while(SSPCON2bits.SEN);  // and wait for it to clear
    63.  SSPCON2bits.ACKDT = 0;  // acknowledge bit
    64.  PIR1bits.SSPIF = 0;
    65.  
    66.  SSPBUF = addr;     // SRF08 I2C address
    67.  while(!PIR1bits.SSPIF);  // wait for interrupt
    68.  PIR1bits.SSPIF = 0;   // then clear it.
    69.  
    70.  SSPBUF = 2;      // address of register to read from - high byte of result
    71.  while(!PIR1bits.SSPIF);  //
    72.  PIR1bits.SSPIF = 0;   //
    73.  
    74.  SSPCON2bits.RSEN = 1;  // send repeated start bit
    75.  while(SSPCON2bits.RSEN); // and wait for it to clear
    76.  
    77.  PIR1bits.SSPIF = 0;   //
    78.  SSPBUF = addr+1;    // SRF08 I2C address - the read bit is set this time
    79.  while(!PIR1bits.SSPIF);        // wait for interrupt
    80.  PIR1bits.SSPIF = 0;         // then clear it.
    81.  
    82.  while(SSPCON2&0x1f);     // wait for IDLE state  before setting RCEN (work around in errata)
    83.  
    84.  SSPCON2bits.RCEN = 1;  // start receiving
    85.  while(!SSPSTATbits.BF);  // wait for high byte of range
    86.  range = ((int)SSPBUF)<<8; // and get it
    87.  SSPCON2bits.ACKEN = 1;  // start acknowledge sequence
    88.  while(SSPCON2bits.ACKEN); // wait for ack. sequence to end
    89.  
    90.  while(SSPCON2&0x1f);     // wait for IDLE state before setting RCEN (work around in errata)
    91.  
    92.  SSPCON2bits.RCEN = 1;  // start receiving
    93.  while(!SSPSTATbits.BF);  // wait for low byte of range
    94.  range += SSPBUF;    // and get it
    95.  
    96.  SSPCON2bits.ACKDT = 1;  // not acknowledge for last byte
    97.  SSPCON2bits.ACKEN = 1;  // start acknowledge sequence
    98.  while(SSPCON2bits.ACKEN); // wait for ack. sequence to end
    99.  
    100.  SSPCON2bits.PEN = 1;   // send stop bit
    101.  while(SSPCON2bits.PEN);  //
    102.  return range;
    103. }
    104.  
    105. void write_I2C (char addr,char reg,char cmd)
    106. {
    107.  SSPCON2bits.SEN = 1;   // send start bit
    108.  while(SSPCON2bits.SEN);  // and wait for it to clear
    109.  PIR1bits.SSPIF = 0;
    110.  
    111.  SSPBUF = addr;     // SRF08 I2C address
    112.  while(!PIR1bits.SSPIF);  // wait for interrupt
    113.  PIR1bits.SSPIF = 0;   // then clear it.
    114.  
    115.  SSPBUF = reg;     // address of register to write to
    116.  while(!PIR1bits.SSPIF);  //
    117.  PIR1bits.SSPIF = 0;   //
    118.  
    119.  SSPBUF = cmd;     // command
    120.  while(!PIR1bits.SSPIF);  //
    121.  PIR1bits.SSPIF = 0;   //
    122.  
    123.  SSPCON2bits.PEN = 1;   // send stop bit
    124.  while(SSPCON2bits.PEN);  //
    125. }
    126.  
     
    Last edited by a moderator: Mar 17, 2012
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    "FFFF" sounds like a hint. the I2C data line is normally pulled high. If no slaves respond then the master will clock in all 1's for all the data.

    Sounds like you have a basic I2C problem. Start seeing if the slave ACKs the master when it sends the address. Either try sticking a scope on the clock & data lines, or have the master stall out when it is clocking the NAK/ACK bit in so you can read it with a DVM.

    I betcha dollars to donuts there is the problem. (I2C is usually a biatch to ge4t working.)
     
  3. afz786miah

    Thread Starter New Member

    Mar 16, 2012
    2
    0
    Cheer mate. Thanks for the reply. Guess you rite il let you no if i solve the problem.
    Big Thanks to ya again
     
Loading...