SPI write MOSI ilde low?

Started by Mongkol sent by Nabble.com December 17, 2005

Hello all,

I am using the 9S12DP256 to the output signal
of an SPI Analog Devices 16bit SPI ADC (AD7708)
void init_spi(){
IO_PORTS_8(DDRS)=0x60; //MOSI ,CLK ouput
IO_PORTS_8(SPI0BR)=0x47; //clk 20.1 kHz
IO_PORTS_8(SPI0CR1)=0x58; //MSTR=1 ,CPOL =1,CPHA =0 ,SPE =1
IO_PORTS_8(SPI0CR2)=0x00;
}
void spi_wr(unsigned int code){char q;
q=IO_PORTS_8(SPI0SR); //clear SPIF
q=IO_PORTS_8(SPI0DR);
IO_PORTS_8(SPI0DR)=code;
while((IO_PORTS_8(SPI0SR)&0x80)==0);
}
unsigned int spi_re(void){char q;
IO_PORTS_8(SPI0DR)=0;
while((IO_PORTS_8(SPI0SR)&0x80)==0);
return (IO_PORTS_8(SPI0DR));
}

i need MOSI idle low CLK idle high..

__ _ ___ ___ ___
clk |___| |___| |___|
__ _ _______
MOSI __ __| |_____ __ |

Any idea on why my setup and interface isn't working?
Thanks you.
--
Sent from the MicroControllers - hc12 forum at Nabble.com:
http://www.nabble.com/SPI-write-MOSI-ilde-low--t760608.html#a1986192


Hi, rather than debug your code, here is working spi code:

// initialization of spi
SPI0CR1 = 0x1C; // master mode , polarity high, phase even
SPI0BR = 0x00; // as fast as possible
SPI0CR1_SPE = 1; // spi on

// routine sends the 8 bit byte cSend out the SPI port
// and returns the received character
// an SPI operation both reads and writes at the same time (full
duplex)
// if you just wish to receive, have cSend be any byte
// if you just wish to send, ignore the returned byte
char SpiZeroByteExchange(char cSend)
{
// make sure we are ready to tx
while (SPI0SR_SPTEF == 0);

// start data exchange by writing to the data register
SPI0DR = cSend;

// wait for the data to be ready to be read
while (SPI0SR_SPIF == 0);

// read the data
return SPI0DR;
}

// do not forget to select the chip before doing the byte exchange

Hope this helps,
Adi