AnsweredAssumed Answered

[Help HCS12] SPI function

Question asked by bast bare on Feb 13, 2017
Latest reply on Feb 21, 2017 by bast bare

Hi,

I'm a student and I discover the programming on the HCS12.
After many attempts, I come to ask you to help with the SPI of this microcontroller.
I want to establish communication between the HCS12 and the DS1306 RTC. However, I cann't write/read the data.
The code is based on differents examples found.

 

My code :

void SPI0_INIT(void)
{
   WOMS =0;
   SPI0CR1 = 0x50; // 0b01010000
   SPI0CR2 = 0x00;
   SPI0BR = 0x02;
}

void SPI0_WRITE(uchar addr, char data)
{
   PORTS_DONNEES &= ~0x7F;
   while(!(SPI0SR & SPI_SPTEF)); // wait until write is permissible
   SPI0DR = addr; // output the byte to the SPI
   while(!(SPI0SR & SPI_SPIF)); // wait until write operation is complete
   PORTS_DONNEES |= 0x80;
}

uint SPI0_READ(uchar ad)
{
   PORTS_DONNEES &= ~0x7F;
   while(!(SPI0SR & SPI_SPTEF)); // wait until write is permissible
   SPI0DR = ad; // trigger eight SCK pulses to shift in data
   while(!(SPI0SR & SPI_SPIF)); // wait until a byte has been shifted in
   PORTS_DONNEES |= 0x80;
   return SPI0DR;
}

void main()
{
   DDRS |=0xE0;
   PORTS_DONNEES |= 0x80;
   SPI0_INIT();

   SPI0_WRITE(0x8F);
   SPI0_WRITE(0);
   SPI0_WRITE(0x80);
   SPI0_WRITE(0x08);

 for(;;)
  {
   SPI0_WRITE(0x80, 0);
   SPI0_WRITE(0x03, 0);
   a=SPI0_READ(0x00);
  }  
}

Thank,

Best regards

Outcomes