Hi,
I want to connect two HCS12 MCU's using the SPI communication.
one of them is the MASTER and the other is the SLAVE.
the SS pin for the MASTER is always pulled up and the SLAVE's is always pulled down.
I am using general I/O pin to inform the slave that the communication has been initiated, the slave will wait till the 8 pulses arrived and then read the SPI0DR register.
for both cases (master and slave), the SPI0DR register (data register) is always 0, meaning, it doesn't accept the data that is written to it although the SPETF flag (data register empty flag) is set when I write something to it.
here is some my line for both the master and the slave, and I appriciate any help, thank you.
SLAVE:
1. wait till the MASTER initiate the communication.
2. wait till the communication is done.
3. read the data register.
4. send the some data to the master.
void main()
{
if (!(PTP.b.b7)) // step 1.
{
while(!(SPI0SR.byte & SPIF)); // wait until a byte has been shifted in // sept 2.
test1=SPI0SR.byte; // clear the SPIF flag
test1=SPI0DR;
if (test1==1) // if one received, then send 222 (some test value).
{while(!(SPI0SR.byte&SPTEF)); // wait till the transmit register is empty.
SPI0DR=222;
while(!(SPI0SR.byte & SPIF)); // wait till the communication is over. and repeat step 4.
test1=SPI0SR.byte;
test1=SPI0DR;
while(!(SPI0SR.byte & SPTEF));// wait till the transmit register is empty.
SPI0DR=111;
while(!(SPI0SR.byte & SPIF));
test1=SPI0SR.byte;
test1=SPI0DR;
while(!(SPI0SR.byte & SPTEF));// wait till the transmit register is empty.
SPI0DR=123;
while(!(SPI0SR.byte & SPIF));
test1=SPI0SR.byte;
test1=SPI0DR;
}
}
}
for the MASTER:
void putchar_spi0 (char cx)
{ char temp;
while(!(SPI0SR.byte & SPTEF)); /* wait until write is permissible */
SPI0DR = cx; /* output the byte to the SPI */
while(!(SPI0SR.byte & SPIF)); /* wait until write operation is complete */
temp=SPI0DR; // clear the spif flag.
char getchar_spi0(void)
{
unsigned int te;
while(!(SPI0SR.byte & SPTEF)); /* wait until write is permissible */
SPI0DR = 0x00; /* trigger 8 SCK pulses to shift in data */
while(!(SPI0SR.byte & SPIF)); /* wait until a byte has been shifted in */
te=SPI0DR; // clear the spif flag.
return te; /* return the character */
void main()
{
PORTA.b.b0=0; // 1. pull down the general i/o pin to inform the slave of the beginning of the transmission.
putchar_spi0(1); // send the data one byte (test value 1).
time[0]=getchar_spi0(); // receive 3 bytes.
time[1]=getchar_spi0();
time[2]=getchar_spi0();
PORTA.b.b0=1; // pull up the i/o pin to end the transmission.
}