# Hardware SPI not working in PIC18F4550

Status
Not open for further replies.

#### jitun2

##### Member
I am trying to interface the Nokia 6610 color LCD with a pic18F4550. I am using CCS PICC compiler for the programming of the microcontroller. I can do everything with the LCD by using software spi i.e. bit banging but when I try to do the same using hardware SPI nothing happens at all. Here is the code that i am talking about

Code:
void nokia_write_dorc(char bytefornokia)         // serial write data or command subroutine
{
char caa;
for (caa=8;caa>0;caa--) {
output_low(SCK);
//delay_us(2);
if ((bytefornokia&0x80)==0){output_low(SDO);}
else {output_high(SDO);}
output_high(SCK);
bytefornokia=bytefornokia<<1;
}

}
The above code snippet uses software SPI(bit banging SPI) but when I tries to use the following the LCD is not working

Code:
void nokia_write_dorc(char bytefornokia)         // serial write data or command subroutine
{
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_16);
output_low (SDO);
//output_high (SCK);
//delay_cycles(2);
SSPEN = 1;
spi_write(bytefornokia);
//setup_spi(FALSE);
SSPEN = 0;

}
I have tried all the clock frequencies in place of "SPI_CLK_DIV_16". I have even tried using timer2 at the same place but it was all in vain. I even tried using the #use SPI but still nothing happened. I tried to compile the code in two different version of CCS compilers (3.249 and 4.038) but still could not make it work. Can anybody please tell me what am i doing wrong that is preventing me from using the hardware SPI In place of the software(bit banging) SPI mode. I have set

#bit SSPEN = 0xFC6.5

The waveforms looks perfectly nice in Proteus simulation also the SPI debugger shows that the correct SPI data is being sent but when I implement it in the actual hardware it is not working. Please help.

#### Wp100

##### Well-Known Member
Do you have the Pics SDI, serial in, connected ?

An easy thing to miss if driving a spi device which does not return any data is that the master SPI must receive its data back - if you do not have a SDI connection from the Nokia then connect the pics SD0 to the SDI with a 2k2 resistor.

Last edited:

#### jitun2

##### Member
Thanks for the response.I have not connected the PIC's SDI pin to anything.It is floating. Since I just want to write to the LCD and not intent to read anything in return I have left the SDI pin. I will try your suggestion but can you please tell me why the MCU need a SDI when there is no data in being sent to it from the peripheral?

Edit: - I tried doing that I also tried hardwiring DSI pin to ground but it still didnt work. I also added the SPI_XMIT_L_TO_H to the setup_spi but still no success.

Last edited:

#### Wp100

##### Well-Known Member
It is just a feature of the Pic SPI firmware - see -

http://www.electro-tech-online.com/custompdfs/2009/05/spi-1.pdf -
page 28
" Data is sent from the master to the slave on the SPI link. The slave handles the SPI data and sends it to the LEDs. At the same time, the slave will send data back, due to the nature of SPI. The master will discard this data since it is not used, but still must read the data to ensure the link functions normally. "

Afraid I cannot help with the C - not my thing.

#### Pommie

##### Well-Known Member
You seem to be mixing hardware and software and turning the SPI module on and off. Write an init routine which sets the registers and I/O as required and just write the byte to SSPBUF. After writing to SSPBUF you should wait for the BF bit to be set before reading SSPBUF. This is because whenever a byte is transmitted a byte is received and needs to be read.

Mike.

#### jitun2

##### Member
it worked with this setting
Code:
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_XMIT_L_TO_H|SPI_CLK_DIV_4);
But it needs a delay of minimum 25us after each SPI_write()? Why? and how can i make it work without the delay?

#### Pommie

##### Well-Known Member
SPI works by exchanging bytes, when one is transmitted another is received. Try changing your transmit routine to something like,

Code:
char SendSSP(char data){
sspbuf=data;            //send data
while(!sspstat.BF);     //wait for received byte
}
Mike.

#### UTMonkey

##### New Member
Check out the Errata for the 4550, I think I recall some issue in certaing scenarios with the SPI bus that meant you couldnt bit test the BF flag. Rather, copy SSPSTAT into a variable and do the test on that.

Status
Not open for further replies.