Problems with SPI module

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problems with SPI module

1,642 Views
M3H0
Contributor I
Hi,
 
I'm working with a MC9S12C32 microcontroller using the ICC12 compiler and the NoICE12 debugger. Currently I'm trying to comunicate the microcontroller with a Real Time Clock (RTC) via the SPI port. I initialize the SPI port and I send an adress byte and a data byte via the SPI port but when I check the value of the registers in the debugger SPTEF is always set and SPIDR never changes (it's value is FF). Below I post my code, could some check the code and tell me where is the error.
 
NOTE: The functions that are not related with the SPI communication (ledon(), ledoff() and delay_ms()) work
 
thanks in advance
M3H0
 

#include <mc9s12c32.h>
#include <datatypes.h>
#include <chips12_lcd.h>
#include <ofunc.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <afunc.h>

UINT8 data, adress;

void putchar_spi(int cx){ 
         int temp;
        while(!(SPISR & 0b00100000)); /* wait until write is permissible */
         SPIDR = cx;
/* output the byte to the SPI */
         while(!(SPISR & 0b10100000));
/* wait until write operation is complete */
         temp=SPIDR;
// clear the spif flag.
}

void RTC_on(){
        PORTA = 0x00;  // CS pin is driven LOW
}

void RTC_off(){
        PORTA = 0x01; // CS pin is driven HIGH
}

void init_SPI(){
        SPICR1 = 0x50;
        SPICR2 = 0x00;
        DDRM |= 0x38;
//set SS,SCK,MOSI lines of the µC to Output
         SPIBR=0x06;
//Bus clock:8MHz 

}


void main(void){
        //Configuration of the chip select pin
         DDRA = 0x01;
// Port A, Pin 0 controls Shutdown of DS1391

         //Configuration & initialization of the SPI module
         init_SPI();

        //Switch on the RTC
         RTC_on();

        //Write the adress byte to the RTC
         adress=0x82;
        data=0x15;
         putchar_spi(adress);
        putchar_spi(data);

        //Switch off the RTC
         RTC_off();

        ledon(1);
        delay_ms(1000);
        ledoff(1);

         while (1){
                RTC_off();
                 putchar_spi(adress);
                 putchar_spi(data);

                RTC_off();
        }
}

Labels (1)
0 Kudos
2 Replies

340 Views
bigmac
Specialist III
Hello,
 
The problem would appear to be with the putchar_spi() function.
 
void putchar_spi(int cx) { 
   int temp;
   while(!(SPISR & 0b00100000)); /* wait until write is permissible */

   SPIDR = cx; /* output the byte to the SPI */
   while(!(SPISR & 0b10100000)); /* wait until write operation is complete */
   temp=SPIDR; // clear the spif flag.
}
 
In the second while loop, you are waiting until either SPTEF or SPIF becomes set.  However, the write operation will not be complete until SPIF becomes set, whereas SPTEF will become set again a very short while after SPDR is written.  So the function would be prematurely exiting.
 
The second while loop should test only the SPIF flag.
 
Regards,
Mac
 
 
0 Kudos

340 Views
Steve
NXP Employee
NXP Employee
Is the MCU sending anything to the RTC? Is the RTC sending anything to the MCU?
What you read in the SPIDR is what came back from the RTC.
0 Kudos