Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 27th May 2009, 08:07 PM   #1
Question Hardware SPI not working in PIC18F4550

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.
jitun2 is offline  
Old 27th May 2009, 08:52 PM   #2
Default

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 by Wp100; 27th May 2009 at 08:52 PM.
Wp100 is offline  
Old 28th May 2009, 04:37 AM   #3
Default

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?

Please do reply.


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 by jitun2; 28th May 2009 at 05:37 AM.
jitun2 is offline  
Old 28th May 2009, 09:17 AM   #4
Default Hardware SPI not working in PIC18F4550

It is just a feature of the Pic SPI firmware - see -

http://ww1.microchip.com/downloads/en/devicedoc/spi.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.
Wp100 is offline  
Old 28th May 2009, 09:31 AM   #5
Default

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.
Pommie is online now  
Old 28th May 2009, 06:18 PM   #6
Default

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?
jitun2 is offline  
Old 28th May 2009, 10:45 PM   #7
Default

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
    return(sspbuf);         //read it
}
Mike.
Pommie is online now  
Old 31st May 2009, 07:33 PM   #8
Default

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.
UTMonkey is offline  
Reply

Tags
hardware, pic, spi, working

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
pic18f4550 oussamasgh Micro Controllers 10 4th April 2009 05:53 PM
pic18f4550 and lcd neelam29 Micro Controllers 16 1st March 2009 11:23 AM
Programming PIC18F4550 (not working properly) Kryten Micro Controllers 0 16th May 2008 12:04 PM
Pic18f4550!! watzmann Micro Controllers 1 20th March 2008 11:59 AM
PIC18F4550 and USB max2005 Micro Controllers 3 5th July 2005 06:39 PM



All times are GMT. The time now is 04:59 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker