XDT512: SPI bus & LCD display

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

XDT512: SPI bus & LCD display

4,043 Views
kkarim
Contributor II
Hello,

I am trying to use the SPI on an CSM12DXDT512 on a PBMCUSLK project board to communicate with the board's lcd display.
I have understood the routines of both SPI and LCD, however, i am not able to output anything to the LCD.
I don't think i am actually writing anything on the SPI bus, as I have tried to go trough a step by step run of my code, and nothing is hapening at the output pins (which i connected to LEDs for monitoring).

The XDT512 has 3 SPI channels, and i'm using SPI0, as the board's LCD is connected to it's pins on the MCU PORT.

Here is the piece of code I wrote. Please let me know if there's anything I'm doing wrong.
Labels (1)
0 Kudos
13 Replies

934 Views
kkarim
Contributor II
Forgot to paste the code in the previous post! My bad. :smileyhappy:

#include <hidef.h>      /* common defines and macros */
#include <mc9s12xdt512.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xdt512"



void main(void) {
 
  DDRM = 0x38;
 
  PTM_PTM3= 1;
 
  SPI0CR1= 0x50;
 
  SPI0CR2= 0;
  SPI0BR = 0x02;
 
 
  PTM_PTM3= 0;
 
  SPI0DR= 0xaa;
  while ((SPI0SR_SPIF)==0);
 
  PTM_PTM3= 1;
 
  EnableInterrupts;

  for(;:smileywink: {} /* wait forever */
  /* please make sure that you never leave this function */
}



Message Edited by kkarim on 2007-06-25 10:57 PM
0 Kudos

934 Views
bigmac
Specialist III
Hello,
 
I am not familiar with your board, however, I would assume that your LCD is an alphanumeric type that has a parallel interface.  Therefore, to communicate with the display using SPI will involve additional hardware, such as a shift register, for serial to parallel conversion.  The detail of this interconnection will need to be taken into account.
 
The following post refers to a different board, but may be similar to yours with respect to the LCD connections.  If so, the code presented may give a better idea about the steps required to control the LCD via the shift register.
 
Regards,
Mac
 
0 Kudos

934 Views
kkarim
Contributor II
This thing is driving me crazy.

I am not able to work out how to use SPI, allthough it seems very simple!

I am not even bothering to output to the LCD yet, just trying to write from one XDT512 to the other, and it's not working.

The MOSI0, MISO0, SCK0 and SS0* pins are also used as pins 4,2,5 and 3 of port M (respectively, also pins 17, 19, 21 and 23 of the MCU I/O port). I have been using DDRM to set them as input or output, depending on slave or master.

I read in FreeScale's "SPI Block User Guide" that the pins are set as I/O whenever I enable the SPI system, so I removed the DDRM setting. It's still not working...

Also, i have noticed that most people use PORTS as output for SPI, however, I only have PS0-PS3 on my MCU, and these pins are also used for TXD/RXD (0-1) of the SCI interface.

Can anyone shed some light here? Please?
0 Kudos

934 Views
kef
Specialist I
SPI0 and some other peripherals can be routed to different ports and pins. SPI0 can be routed either to pins PS4-PS7 or to pins PM2-PM5. Check the MODRR register description. Default routing for SPI0 (MODRR=0) is route to pins PS4-PS7. You should set bit4 of MODRR to route SPI0 to port M pins PM2-PM5 ( MODRR|=0x10).
0 Kudos

934 Views
kkarim
Contributor II
Thanks a million kef!!! It's all my code needed.

I can't beleive i found no sign of this whatsoever in the documentation though...
0 Kudos

934 Views
Steve
NXP Employee
NXP Employee
The function is described in the PIM section of the manual and the signal routing is mentioned in the detailed signal description in chapter 1. Maybe we could make the connection a bit clearer so any suggestions will be gratefully received.
In general, chapter 1 of the data book is an essential read because it tells you how the designers "wired-up" the chip. This information will change from chip to chip so you have to check this before reusing code as well.
0 Kudos

934 Views
kkarim
Contributor II
According to the PBMCUSLK schematics, there is a 595 shift register connected to the LCD on one end, and the SPI bus on the other.

Anyways, I still didn't bother to start writing to the LCD, as I can't even see the output on the SPI bus.

Shouldn't I be able to see the pins going High and Low as the master writes on the bus?

Thanks.
0 Kudos

934 Views
kef
Specialist I
kkarim,
 
try to read SPI status register before writing to data register. This is needed to clear SPTEF bit. First read status, then write data. Also if you read MODF bit description, MODF can be cleared by reading status then writing to SPICR1. Of course MODF shouldn't be set after reset, but it won't hurt you if you read status register just before a write to SPICR1.
0 Kudos

934 Views
bigmac
Specialist III
Hello,
 
Whether you can easily capture the transitions on the SPI bus may depend on your instrumentation.  I notice that your SPI clock rate is quite high.  Perhaps you should considerably reduce this so that you can more clearly observe what is happening during your tests.  You might also consider making your test code a repetitive loop to achieve continuous signals on the SPI pins.
 
An additional thought - are there any switches or jumpers that need to be set to route the SPI signals through to the shift register?
 
Regards,
Mac
 


Message Edited by bigmac on 2007-06-26 07:43 PM
0 Kudos

934 Views
kkarim
Contributor II
There are three jumpers to connect, or disconnect the MOSI MISO and CLOCK from the LCD's shift register, and there is one more to select the if we want the SPI selector on the shift register to be taken from SS or from GPIO on the MCU.

There are all properly inserted.

Another question: If i go through a step by step run of my code, shouldn't I be able to see the outputs going high and low at each step?
0 Kudos

934 Views
rocco
Senior Contributor II
Hi Karim:

kkarim wrote:
Another question: If i go through a step by step run of my code, shouldn't I be able to see the outputs going high and low at each step?

Not necessarily. The SPI runs asynchronously from the CPU, and will start shifting as soon as you write to its data register. It can not be "single-stepped". If you have a fast SPI clock rate, it will be done in a couple of microseconds, and you won't be able to see that on an LED.
0 Kudos

934 Views
bigmac
Specialist III
Hello Karim,
 
I missed that you were monitoring using LEDs.  As Rocco indicated, the use of LEDs is quite unsuitable for monitoring the SPI operation.  You will need to use an oscilloscope, and to capture a single SPI transaction at a fast clock rate, it would need to be a storage type.
 
To monitor the correct operation of the SPI and shift register combination, you could connect LEDs at the outputs of the shift register.  Bear in mind that your code will need to "strobe" the output latches of the shift register, after the SPI transaction is completed, to transfer the result of the SPI transaction.
 
The following code is presented as a general purpose function for a single master SPI transaction, for both sending a byte to the slave, and receiving the returned byte from the slave.
 
byte SPI_trans (byte sendval)
{
   while (SPI0SR_SPTEF == 0);
   SPI0DR = sendval;
   while ((SPI0SR_SPIF == 0);
   return SPI0DR
}
 
Note that the final read of SPI0DR is always necessary to clear the SPIF flag, and prevent an overrun error, whether or not you are using the returned data.  Since the function waits for the SPIF flag to become set, the SPTEF flag would always be set whenever the function is re-entered, so the first line of the function is really a formality, and could be omitted in most instances.
 
Regards,
Mac
 
0 Kudos

934 Views
kkarim
Contributor II
Ok thanks a lot for the help guys.
I'll try it on the scope when i go to the university labs tomorrow.

I still hadn't bothered trying to output to the LCD, because i was still trying to write to the SPI bus. Anyways, i am now working on routines for outputting to the LCD.

I'll make sure to let you guys know if i need any more help.

Thanks for the help :smileyhappy:
0 Kudos