Andres Arguello

SPI configuration problem

Discussion created by Andres Arguello on Oct 25, 2008
Latest reply on Oct 26, 2008 by David Payne
Hi, first allow me to apologize if my english is not really good, it is not my native language.
I am trying to use the SPI in my MC68HC908GP32CP, I used the processor expert SPI bean initialization
Here is the init code that it generates:
void SPI1_Init(void)
{
  /* SPCR: SPRIE=0,??=0,SPMSTR=0,CPOL=0,CPHA=0,SPWOM=0,SPE=0,SPTIE=0 */
  setReg8(SPCR, 0x00);                 /* Disable the SPI module */
  (void)getReg8(SPSCR);                /* Dummy read of the SPSCR registr to clear flags */
  (void)getReg8(SPDR);                 /* Dummy read of the SPDR registr to clear flags */
  /* SPSCR: SPRF=0,ERRIE=0,OVRF=0,MODF=0,SPTE=0,MODFEN=0,SPR1=1,SPR0=1 */
  setReg8(SPSCR, 0x03);                
  /* SPCR: SPRIE=0,??=0,SPMSTR=1,CPOL=1,CPHA=1,SPWOM=0,SPE=0,SPTIE=0 */
  setReg8(SPCR, 0x38);                 
}

 My problems start here, when I try to do the simulation, it show me an error in the code line
(void)getReg8(SPDR);                 /* Dummy read of the SPDR registr to clear flags */

The error said :
Error: At location 0012 -
Error: Attempt to use invalid or uninitialized memory, i don't know what to do.

I am posting all the code that I am using to comunicate with the slave device, it is a MMA745xL Digital Accelerometer, almost all the code here I copied from the application note 3468 (AN3468), because this is my first try with SPI and this kind of devices.
Thank you, I will really appreciate any help I can get.
PD. I already try searching on the forum for any problem similar to mine, but didn't find any.
Andres

/** ###################################################################
**     Filename  : Project_9.C
**     Project   : Project_9
**     Processor : MC68HC908GP32CP
**     Version   : Driver 01.15
**     Compiler  : CodeWarrior HC08 C Compiler
**     Date/Time : 25/10/2008, 12:18 a.m.
**     Abstract  :
**         Main module.
**         This module contains user's application code.
**     Settings  :
**     Contents  :
**         No public methods
**
**     (c) Copyright UNIS, spol. s r.o. 1997-2008
**     UNIS, spol. s r.o.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/
/* MODULE Project_9 */


/* Including used modules for compiling procedure */
#include "Cpu.h"
#include "Events.h"
#include "SPI1.h"
#include "AS1.h"
/* Include shared modules, which are used for whole project */
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"
byte Xdata,Ydata,Zdata,CTLReg,X;
void spi_write(byte reg, byte data);
byte spi_read(byte reg);
void main(void)
{
  /* Write your local variable definition here */

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
  /*** End of Processor Expert internal initialization.                    ***/
/*** End of Processor Expert internal initialization.                    ***/
 
 

  PTC_PTC4=1;
  DDRC_DDRC4=1;
  Xdata=0;
  CTLReg=0;
  spi_write(0x16,0x05); //4 Wire Mode Set up in the Accelerometer
  CTLReg=spi_read(0x16);
  while(AS1_SendChar(CTLReg)!=ERR_OK){}

  /* Write your code here */
  /* For example: for(;; ) { } */
 
  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;; ){}
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END Project_9 */
/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 3.03 [04.07]
**     for the Freescale HC08 series of microcontrollers.
**
** ###################################################################
*/

// Function Definitions for Read and Write
byte spi_read(byte reg){
byte x;
PTC_PTC4=0;
x=SPSCR;
x=SPDR;
while (!SPSCR_SPTE);
SPDR= ((reg &0x3F)<<1); // write in the register address with the read command
while(!SPSCR_SPRF){} //wait for transfer
x=SPDR;
SPDR = 0x00; // send 2nd byte
while (!SPSCR_SPRF){} // wait transfer done
x = SPDR;
PTC_PTC4=1;
return (x);
}


//Write function SPI 3 Wire or SPI 4 Wire Mode
void spi_write(byte reg, byte data){
byte x;
PTC_PTC4=0;
SPDR=(((reg &0x3F)<<1)|0x80);
while (!SPSCR_SPRF){} //wait for transmission complete
x=SPDR; //dummy read
SPDR=data;
while (!SPSCR_SPRF){} //wait for transmission complete
x=SPDR;
PTC_PTC4=1;
}
 
  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/



Outcomes