Electronic – Arduino SPI Clock Speed Configuration

arduinoatmegaavrspi

I am trying to communicate with a Nokia 1202 LCD with my Arduino using SPI.

The LCD using the STE2007 Controller. As per the datasheet here @ page 12, you find that the min time for SCLK is 250ns => 4MHz and no maximum value listed. So what i understand is that this driver can communicate using SPI with SCLK being max. of 4MHz.

Since my arduino board is running at 16MHz, and I will be using the 'spi' library this is what I think i should do:

  1. set bits SPR0 and SPR1 in the SPCR register to set the spi clock speed @ 4MHz
  2. do spi.begin()
  3. execute spi commands as normal

Is this correct ?

Something like …

void setup()
{
  pinMode(SPI_SS, OUTPUT);
  pinMode(SPI_DC_SEL, OUTPUT);
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  //set spi register to set SCLK @ 4Mhz
  //SPI2X = SPR1 = SPR0 = 0 
  SPCR &= ~(1<<SPR1);
  SPCR &= ~(1<<SPR0);
  SPSR &= ~(1<<SPIX2);
}

Best Answer

You can set the SPI speed (and many other things!) using the Arduino SPI library without specifying any register values. You already did it with MSBFIRST. Like so:

  SPI.setDataMode(SPI_MODE3);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV4); // 16/4 MHz
  SPI.begin(); 

Also, SPI.begin sets all the pin modes for you, so no need to do that.