AnsweredAssumed Answered

HCS12 SPI to Digital Compass HM55B

Question asked by Pedro Duarte on May 6, 2009
Latest reply on May 8, 2009 by Pedro Duarte

Hello everyone!

 

I am trying to communicate to a digital compass HM55B using SPI. My microcontroller is a CMS12C32. I am very confused because the way of reading back the bits from the compass.

I have written several times the code below and no results yet. I even replaced the compass for a new one.

Sent to Compass:

0b0000 to reset 

0b1000 to start meausurement

0b1100 to check status flag

 

Receive from Compass:

0b1100 everything is ok

After 0b1100, it receives 11-bits x component followed by 11-bit y component.

 

I have attached a graphic showing the compass information.

 


#define EN_N PTT = 0x00 //Enable LOW
#define EN_Y PTT = 0x01 //Enable HIGH

int x_angle = 0;
int y_angle = 0;
unsigned char buf_reset[4] = {0,0,0,0};
unsigned char buf_start[4] = {1,0,0,0};
unsigned char buf_check[4] = {1,1,0,0};

unsigned char staus_flag;

void main(void) {
  int i; 

  //setup PPT
  DDRT_DDRT0=1; //configure PORT T[0] for output

  //Init SPI
  SPICR1= 0x5e; //0x22 low clk
  SPICR2= 0x10;
  SPIBR = 0x70; //1MHz

  for(;;){
  
    do{
    
      //Reset Compass
      EN_Y;
      waitms(10);
      EN_N;
      for(i=0; i<4; i++) {
        while ((SPISR & 0x20) == 0);
        SPIDR = buf_reset[i];
      }
      EN_Y;
      waitms(50);
    
      //Start Compass
      EN_N;
      for(i=0; i<4; i++){
        while ((SPISR & 0x20) == 0);
        SPIDR = buf_start[i];
      }

      //Check Measurement
      waitms(50);
      EN_Y;
      waitms(10);
      EN_N;
      for(i=0; i<4; i++){
        while ((SPISR & 0x20) == 0);
        SPIDR = buf_check[i];
      }

      //Get Status Flag
      staus_flag = getcspi();
    
    } while (staus_flag != 0b1100); //exit if status is 0b1100


    x_angle = getcspi(); //I never get to this point
    y_angle = getcspi();

  }
}
/**********************************
*  Subroutines         
***********************************/
int getcspi(void)
{
  while(!SPISR_SPTEF);// wait until write is permissible
  SPIDR = 0x00;       // trigger 8 SCK pulses to shift in data
  while(!SPISR_SPIF); // wait until a byte has been shifted in
  return SPIDR;       // return the character
}

 

 

Thanks for you ay help!

 

Pedro

Outcomes