Reply by leon...@yahoo.com.br March 30, 20102010-03-30
When you put while (!IBSR_IBIF); try while (;!IBSR_IBIF;);
see if that works.
Reply by "nielsen.daniel" December 6, 20092009-12-06
Hello, I am trying to interface an SRF08 Ultrasonic Sensor to my Dragon-12 board (I2C) via CodeWarrior and C language. I have tried and tried again but no succes...I am pretty sure I am writing to the SRF08 but I dont think I am reading correctly. Any help or suggestions or working code would be greatly appreciated. Thanks.

This is my code.

#include /* common defines and macros */
#include /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg256b"

#include "I2C.h"
#include "main_asm.h" /* interface to the assembly module */

void main(void) {
byte echo1;
PLL_init();
lcd_init();
clear_lcd();
Init_I2C();
I2C_write_byte(0xE0,0x00,0x50); // 99% sure it does what its suppposed to

ms_delay(65); //wait 65ms
set_lcd_addr(0x00);
type_lcd("Testing"); // lcd DOES display "Testing" so i know we are atleast getting here

echo1 = I2C_read_byte(); // attmempts to read register 0x02, eventually want to read 0x02 and 0x03

set_lcd_addr(0x40);
type_lcd("distance"); //lcd DOES NOT display "distance" so we dont even get to here

data8(echo1); //displays echo1 high byte

}
//Initialize I2C
void Init_I2C(void)
{
IBFD = 0x04; // frequency divider register: Bus = 24MHz => SCL = 40kHz;
IBAD = 0xE0;
IBCR |= 0x80; // enable I2C module

}
//Initiate a ranging command by writing 0x50 to register 0x00 in SRF08(default addr = 0xE0)
void I2C_write_byte(char srf08, char addr, byte data)
{
IBCR_TXAK = 0; // TXAK = 0;
IBCR |= 0x30; // RX/TX = 1; MS/SL = 1; TXAK = 0;
// And generate START condition;
//-------start of transmit first byte to IIC bus-----
IBDR = srf08; // Address the slave and set up for master transmit;
while (!IBSR_IBIF); // wait until IBIF;
IBSR_IBIF=1; // clear the interrupt event flag;
while(IBSR_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IBDR = addr; // Send high byte of the word address;
while (IBSR_IBIF); // wait until IBIF;
IBSR_IBIF=1; // clear the interrupt event flag;
while(IBSR_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IBDR = data;
while (!IBSR_IBIF); // wait until IBIF;
IBSR_IBIF=1; // clear the interrupt event flag;
while(IBSR_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IBCR_MS_SL = 0; // generate STOP condition;
}

//Return the echo high byte of register 0x02 on the SRF08
char I2C_read_byte(void)
{
char dummy;
char echo_high;
IBCR_TXAK = 0; // TXAK = 0;
IBCR |= 0x30; // RX/TX = 1; MS/SL = 1; TXAK = 0;
// And generate START condition;
IBDR = 0xE0; // Address the slave and set up for master transmit;
while (!IBSR_IBIF); // wait until IBIF;
IBSR_IBIF=1; // clear the interrupt event flag;
while(IBSR_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IBDR = 0x02; // Send high byte of word address;
while (!IBSR_IBIF); // wait until IBIF;
IBSR_IBIF=1; // clear the interrupt event flag;
while(IBSR_RXAK); // check for RXAK;
//-----Slave ACK occurred------------
IBCR_RSTA = 1; // set up repeated start;
IBDR = 0xE1; // (slave_address) | (RW = 1);
while (!IBSR_IBIF); // wait until IBIF;
IBSR_IBIF=1; // clear the interrupt event flag;
while (IBSR_RXAK); // check for RXAK;
IBCR_TX_RX = 0; // set up to receive;
IBCR_TXAK = 1; // acknowledge disable;
dummy = IBDR; // dummy read;
while (!IBSR_IBIF); // wait until IBIF;
IBSR_IBIF=1; // clear the interrupt event flag;
IBCR_MS_SL = 0; // generate stop signal;
echo_high = IBDR; // read right data;
return echo_high;
}