Oroitz Elgezabal

Problems with SPI module

Discussion created by Oroitz Elgezabal on Mar 12, 2007
Latest reply on Mar 12, 2007 by bigmac
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

#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 */
// 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
//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

        //Switch on the RTC

        //Write the adress byte to the RTC

        //Switch off the RTC


         while (1){