View previous topic :: View next topic |
Author |
Message |
dan king
Joined: 22 Sep 2003 Posts: 119
|
sw spi works, HW spi doesn't |
Posted: Fri Mar 11, 2011 2:51 pm |
|
|
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
|
|
Posted: Sat Mar 12, 2011 2:04 am |
|
|
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
|
|
Posted: Sat Mar 12, 2011 6:55 am |
|
|
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
|
|
Posted: Sun Mar 13, 2011 2:49 pm |
|
|
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
|
|
Posted: Mon Mar 14, 2011 11:20 am |
|
|
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 |
|
|
|