CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

sw spi works, HW spi doesn't

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dan king



Joined: 22 Sep 2003
Posts: 119

View user's profile Send private message

sw spi works, HW spi doesn't
PostPosted: Fri Mar 11, 2011 2:51 pm     Reply with quote

I am using a nokia 5510 LCD display and am using the code supplied in the code section of this forum. I set up a project using a 18f2553 connected to the display and after adding some code to display a complete screen successfully I decided to try and take this further by using the project with either a SW SPI or HW SPI.

I modified the function that writes out SPI and added code to select sw or hw functions but the HW version doesn't seem to work. Code below:
Code:

void nokia_init(void)
{

#if !defined (software_spi)
   #use spi(FORCE_HW)
   setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_16);
   sysled = 1;
#endif

  output_high(nok_dc);            // bytes are stored in the display data ram, address counter, incremented automatically
  output_high(nok_cs);            // chip disabled

  output_low(nok_res);            // reset chip during 250ms
  delay_ms(10);         // works with less.....
  output_high(nok_res);

  nokia_write_command(0x21);   // set extins extended instruction set
  nokia_write_command(0xc2);   // Vop  v1: 0xc8 (for 3V)// v2: 0xa0 (for 3V) // v3: 0xc2 (2v6-5v)   ********************************************************************************************************************
  nokia_write_command(0x13);   // bias
  nokia_write_command(0x20);   // horizontal mode from left to right, X axe are incremented automatically , 0x22 for vertical addressing ,back on normal instruction set too
  nokia_write_command(0x09);   // all on

  nokia_clean_ddram();      // reset DDRAM, otherwise the lcd is blurred with random pixels

  nokia_write_command(0x08);   // mod control blank change (all off)

  nokia_write_command(0x0c);   // mod control normal change

}



and the write function:
Code:

void nokia_write_dorc(char bytefornokia)         // serial write data or command subroutine
{

#if defined (software_spi)
  char caa;
  for (caa=8;caa>0;caa--) {
    output_low(nok_sclk);
    // delay_us(2);
    if ((bytefornokia&0x80)==0){output_low(nok_sda);}
    else {output_high(nok_sda);}
    output_high(nok_sclk);
    bytefornokia=bytefornokia<<1;
  }
#endif


#if !defined(software_spi)
   testled = 1;
   spi_write(bytefornokia);

#endif

}


any thoughts on why the HW version won't work?

Thanks in advance,

Dan


Last edited by dan king on Fri Mar 11, 2011 4:02 pm; edited 2 times in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 12, 2011 2:04 am     Reply with quote

Quote:
void nokia_init(void)
{

#if !defined (software_spi)
#use spi(FORCE_HW)
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_16);
sysled = 1;
#endif

Don't mix the two SPI methods. Delete the #use spi(FORCE_HW) line.
The setup_spi() function always uses hardware SPI.

Quote:

setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_16);

In the setup_spi() statement, you have specified mode 2.
I can tell that by looking at these definitions:
Code:

// SPI mode definitions.
#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)


The 5510 apparently uses the same LCD controller as the 3310, which is
the PCD8544:
http://www.sparkfun.com/products/10168

The PCD8544 data sheet shows (in Figure 10 on page 12) that the SPI
clock idles low and samples on the rising edge. This is SPI mode 0.
http://www.nxp.com/acrobat/datasheets/PCD8544_1.pdf.

SPI modes:
http://www.totalphase.com/support/kb/10045/#modes

So the setup_spi() statement should be:
Code:

setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_16);

Put the #define statements for the modes above main(), with the rest
of the #defines.
dan king



Joined: 22 Sep 2003
Posts: 119

View user's profile Send private message

PostPosted: Sat Mar 12, 2011 6:55 am     Reply with quote

Hi PCM,

As usual you hit the nail right on the head, I do have one question though. I used the spi setup lines from a colleagues code that uses the same display and that code works. I'm not questioning you as you obviously got it right and I should have referenced the data sheet for the SPI modes but I assumed since the reference code worked with the wrong SPI setup that it should work for me too. What I'm asking is how was the reference code able to get the display to function with the wrong setup? Any thoughts so I can go back to him and explain the error?

Thanks so much for your help,

Dan
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 13, 2011 2:49 pm     Reply with quote

I don't know. I would need a link to his code, and to the data sheet for
the controller for his LCD.
dan king



Joined: 22 Sep 2003
Posts: 119

View user's profile Send private message

PostPosted: Mon Mar 14, 2011 11:20 am     Reply with quote

I can't provide his code but I can say that he was using the exact same display so it was the same controller. Very odd. I like your links that explain the details of the 4 SPI modes, I'll be referring to that again for sure.

Thanks,

Dan
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group