AnsweredAssumed Answered

SPI Communication - MC9S08QG8 with accelerometer

Question asked by victor carvajal on May 10, 2010
Latest reply on Mar 19, 2018 by Arna Lee

Hello,

I'm using the following demo board, DEMO9S08QG8, that uses the MC9S08QG8 MCU and I'm trying to talk to a ST accelerometer via SPI,  Attached is the datasheet accelerometer.

 

There is a lot of good existing information on the forums regarding SPI and even some application notes, but i would appreciate another set of eyes into what I could be doing wrong.

 

When I try to communicate w/ the device, i keep getting back 255 as the data.

 

Attached are some code snippets:

 

 

#include <hidef.h> /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations */#ifdef __cplusplus extern "C"#endifvoid MCU_init(void); /* Device initialization function declaration */unsigned char memorydata = 0;unsigned char temp = 0;long int address = 0x0F;byte SPI_proc(byte data);void main(void) {  MCU_init(); /* call Device Initialization */  /* include your code here */  for(;;) {    /* __RESET_WATCHDOG(); by default, COP is disabled with device init. When enabling, also reset the watchdog. */        if (PTAD_PTAD2 == 0)  /* read chip */    {      PTBD_PTBD5 = 0;   /* chip select low */      (void)SPI_proc(3);    /* send read instruction to memory */      memorydata = SPI_proc(0x0F);    /* red device id */            PTBD_PTBD5 = 1;   /* chip select high */          } /* loop forever */  /* please make sure that you never leave main */  }}byte SPI_proc(byte data) {    while(!SPIS_SPTEF); /* wait for tX buffer to empty */  SPID = data;        /* send data */  while(!SPIS_SPRF);  /* wait for Rx buffer to empty */  return (SPID);}   

 

 

here's MCU_init()

 

/*** ===================================================================**     Method      :  MCU_init (bean MC9S08QG8_16)****     Description :**         Device initialization code for selected peripherals.** ===================================================================*/void MCU_init(void){  /* ### MC9S08QG8_16 "Cpu" init code ... */  /*  PE initialization code after reset */  /* Common initialization of the write once registers */  /* SOPT1: COPE=0,COPT=1,STOPE=0,BKGDPE=1,RSTPE=0 */  SOPT1 = 0x52;                                        /* SPMSC1: LVDF=0,LVDACK=0,LVDIE=0,LVDRE=1,LVDSE=1,LVDE=1,BGBE=0 */  SPMSC1 = 0x1C;                                        /* SPMSC2: PDF=0,PPDF=0,PPDACK=0,PDC=0,PPDC=0 */  SPMSC2 = 0x00;                                        /* SPMSC3: LVDV=0,LVWV=0 */  SPMSC3 &= (unsigned char)~0x30;                       /*  System clock initialization */  ICSTRM = *(unsigned char*far)0xFFAF; /* Initialize ICSTRM register from a non volatile memory */  ICSSC = *(unsigned char*far)0xFFAE;  /* Initialize ICSSC register from a non volatile memory */  /* ICSC1: CLKS=0,RDIV=0,IREFS=1,IRCLKEN=0,IREFSTEN=0 */  ICSC1 = 0x04;                        /* Initialization of the ICS control register 1 */  /* ICSC2: BDIV=0,RANGE=0,HGO=0,LP=0,EREFS=0,ERCLKEN=0,EREFSTEN=0 */  ICSC2 = 0x00;                        /* Initialization of the ICS control register 2 */  /* Common initialization of the CPU registers */  /* PTASE: PTASE4=1,PTASE3=1,PTASE2=1,PTASE1=1,PTASE0=1 */  PTASE |= (unsigned char)0x1F;                                 /* PTBSE: PTBSE7=1,PTBSE6=1,PTBSE5=1,PTBSE4=1,PTBSE3=1,PTBSE2=1,PTBSE1=1,PTBSE0=1 */  PTBSE = 0xFF;                                        /* PTADS: PTADS5=0,PTADS4=0,PTADS3=0,PTADS2=0,PTADS1=0,PTADS0=0 */  PTADS = 0x00;                                        /* PTBDS: PTBDS7=0,PTBDS6=0,PTBDS5=0,PTBDS4=0,PTBDS3=0,PTBDS2=0,PTBDS1=0,PTBDS0=0 */  PTBDS = 0x00;                                        /* ### Init_SPI init code */  /* SPIC1: SPIE=0,SPE=0,SPTIE=0,MSTR=0,CPOL=0,CPHA=0,SSOE=0,LSBFE=0 */  SPIC1 = 0x00;                        /* The SPRF interrupt flag is cleared when the SPI module is disabled. */  /* SPIC2: MODFEN=0,BIDIROE=0,SPISWAI=0,SPC0=0 */  SPIC2 = 0x00;                                        /* SPIBR: SPPR2=1,SPPR1=1,SPPR0=0,SPR2=1,SPR1=1,SPR0=1 */  SPIBR = 0x67;                                        (void)(SPIS == 0);                   /* Dummy read of the SPIS registr to clear the MODF flag */  /* SPIC1: SPIE=0,SPE=1,SPTIE=0,MSTR=1,CPOL=1,CPHA=1,SSOE=0,LSBFE=0 */  SPIC1 = 0x58;      PTBDD = 0xee;  PTBPE = 0x11;                                        /* ### */  asm CLI;                             /* Enable interrupts */} /*MCU_init*/

 

 

Something that just dawned on me as I write this is that I am not packing the read command, MSB and address field of the register I want into the first byte? Yet I don't see this in other forums, which makes causes me to be suspect.  Figure 7 (attached) from accelerometer datasheet page 24.

 

Any feedback is much appreciated.

Great forums BTW.

 

--Victor

Outcomes