AnsweredAssumed Answered

IIC help please...MC9S08SH

Question asked by Rocky Weru on Aug 5, 2009
Latest reply on Aug 6, 2009 by Rocky Weru

Hello,

        to put it simply, i have given it my all and failed trying to make an IR sensor work with the IIC module on my board. I have read all the IIC app notes, search this forum back and forth...read HCS08 unleashed and now i simply have to ask for help.

My dilemma:

-I am new to programming so thats a handicap right there

-my code only seems to WORK only when i step line by line while in debug mode

-i have scoped my signals and they appear to be functioning (only in debug mode)

-Once i hit the run buttonthe code completes one loop then freezes at

      while (!(IICS_IICIF));  //  <---  stops here during "free run"

When this occurs, my SDA line is held low by the sensor. It stops at the same place even when i disable the watchdog.

The sensor http://www.robotshop.us/solarbotics-i2c-it-ir-rangefinder-1.html

 comes with code for PICS and other processors, and it appears to be very easy to work with, but i cant get it to work with what i have (MC9S08SH

 

MY CODE:

-------------------------------------------------------------------------------- 

#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */

#define trimvalue 0xFFAE      //debugger calculated trim value
#define LED3  PTCD_PTCD1
#define LED4  PTCD_PTCD0


#define IRaddress   0x20
#define IRreceive   0
#define IRtransmit  1
#define inch        1
#define cm          2
#define raw         3

unsigned int data1,data2,data3,data4;        //for data and testing

 /*SCI setup*/
void SCI_init(void){
 
  SCIBD = 26;      //set baud rate to 19200= 8MHz/(16*(SCIBD))
  SCIC2 =0b00001100;  //enable receive and transmit
 
}


 /* simple software delay */
 
 void delay (unsigned int long value){
  for (;value<=0;value--)__RESET_WATCHDOG();
 }

/*MCU setup*/

void MCU_init (void){
 ICSTRM  =  trimvalue;         //set to calcuated trim value
 PTCDD = 0b00000011;          // make PTC0,1 output for leds
 SPMSC1_BGBE =1;              //enable bandgap buffer.
}

/*IIC setup*/
  void IIC_init(void){
  IICC1_IICEN = 1;  //enable module IIC module
  IICF_MULT= 0x01;  //divide by 2
  IICF_ICR0x14;  //divide by 80;       (50khz)
 
   } 
/*master send function call*/ 
  void master_send (void){
  IICC1= 0b10110000;     //iic enable, no iterrupt,master,transmit,ack on,)
  return;
  }
/*master receive function call*/
  void master_receive (void){
  IICC1= 0b10101000;
  return;
  }
/*iic disable*/
void iic_disable(void){
  IICC1_IICEN = 0;     //disable iic module
  return;            
}
void main(void) {

  EnableInterrupts; /* enable interrupts */
  MCU_init();
  SCI_init();
  IIC_init();
  SCID=0x22;   // test to check reset
 

for(;:smileywink:
 
   
   LED3=1;
  
   master_send();
   IICD=0x20<<1|0;  //write address, put in receive mode
   while (!(IICS_IICIF));
   IICS_IICIF = 1
   while(IICS_RXAK); // check for RX ack;
   //IICC_RSTA=1;      //restart send
   IICD=2;           // ask sensor to send in CM
   while (!(IICS_IICIF));  //  <---  stops here during "free run"
   IICS_IICIF = 1
   while(IICS_RXAK); // check for RX ack;
   IICC_RSTA=1;      //restart send
   IICD=0x20<<1|1;  //write address, put in transmit mode
   delay(10000);
  while (!(IICS_IICIF));
   IICS_IICIF = 1
   while(IICS_RXAK); // check for RX ack;
   //--------------------------------
    IICC_TX=0; //switch to receive mode
    data1=IICD;   //dummy read!!       <--reads sensor value!!
    IICC_TXAK = 1; // acknowledge disable;      <-- have tried swapping bits and places
    IICC_MST = 0; // generate stop signal;
    data2 = IICD; // read correct data;
   
    if(SCIS1_TDRE ==1){       //serial for debug
    SCID=data2;
  }
  
   __RESET_WATCHDOG(); /* feeds the dog */
 
 
  } /* loop forever */
  /* please make sure that you never leave main */
}

 

 

thanks in advance.

Outcomes