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
Thank you for your help, it works now.
Just one last question. Can I drive the EC (RTC) with the SS (SPI0, μC)?
Have a nice day.
Regards,
Bastien
Hi
The SPI on RTC module is active high, so it needs SS to go high every transmission. But the SPI0 sets SS low during each transmission and it is high when the SPI is in idle state. So you cannot use it in this case.
Regards
Daniel
Hi,
Ok, thanks for the information and your help
Regards,
Bastien
Hi,
Sorry, I don't master export project with Warrior Code.
I start programming under HCS12, hence my lack of knowledge.
Thank you for your SPI example.
However, on pin MISO0, I get 0xFF all the time.
Is it necessary to rerouted the Ports S on Port M? Because I don't understand why you do this.
Here is a diagram with the various connections on the board between HCS12 and RTC.
Thanks
Bastien
Hi
It is not necessary to reroute SPI0. But since you use PM1 as SC, I assumed you use SPI0 on Port M as well instead of Port S.
DS1306 datasheet specifies clock polarity CPOL=0 and clock phase CPHA=1. So please set it accordingly, that means SPI0CR1 = 0x54.
Regards
Daniel
Hi
According to the DS1306 datasheet, the SPI is active high, so make sure CE (SS) is set to 1 during a read or a write.
PORTS_DONNEES |= 0x80; // Set
PORTS_DONNEES &= ~0x80; // Clear
You can also try different setting for CPOL, CPHA.
If it doesn’t help, please attach the whole project with clock configuration etc.
Regards
Daniel
Now my DS1306 is rewired with the following configuration:
MOSI -> SDI
MISO <- SD0
SS -> GND
PM1 -> CE
I still have the same problem to know I cann’t read / write the right values.
Here is the complete project : https://drive.google.com/file/d/0BzdMHBq_IHZwak5RVE15N3YxVkE/view?usp=sharing
Thanks for your help