hi,
can someone help me to change a library that uses shiftOut to use hardware spi. I am fairly new to working with microcontrollers. using a pcf8814 and shiftOut is too slow for what I need. I should only change the function that sends data to the lcd, someone correct me if I'm wrong: P.
i read the reference about spi but nothing seams to work. I use arduino uno, and about the speed, it takes about 200 ms to send data that fill the lcd, I use the lcd and also a wireless serial connection with another arduino and because that 200 ms the communication is slow
Hardware SPI is quite easy to implement. Below is an example to send one data byte to a device at a 8 mhz clock rate on an Uno. This may be too high for some spi devices. To change, you simple change the clock divider value.
You can find the spi.cpp and spi.h files in the arduino_install_dir\hardware\arduino\avr\libraries\SPI folder.
//
#include <SPI.h> // provide prototypes for the SPI library
//
// SPI Ports defines (for Arduino Uno)
//
#define SS 10 // Pin mapping to Arduino = SELECT
#define MOSI 11 // Pin mapping to Arduino = Master Out Slave In
#define SCLK 13 // Pin mapping to Arduino = Serial clock
#define MISO 12 // Pin mapping to Arduino = Master IN slave OUT
//
#define myDevice 0x3f
//
void setup()
{
pinMode (SS, OUTPUT);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
sendSPI(myDevice,0x10);
SPI.end();
}
void sendSPI(int device, int tx_data)
{
PORTB &= 0b11111011; // set CS low with direct write to digital pin 10
SPI.transfer(device); // device address write bit already 1ow
SPI.transfer(tx_data);
PORTB |= 0b00000100; // set CS high with direct write to digital pin 10
}
void loop(){}
the FastShiftOut library is still slow and i modify my send function with spi and still not workin. my connection pin are : sclk = 13, sdin = 11, reset = 7, sce = 10
and the modified cod:
You have initialization code in your send routine. The send routine should be very simple:
PORTB &= 0b11111011; // set the CS line low
SPI.transfer(data);
PORTB |= 0b00000100; // return CS to high
There is some other handling that needs to be done, I think the "type" variable is a flag for a command or data. You'll have to sort that out yourself.
The initialization code gets added to the ::begin subroutine:
The PCF8544 samples the data input on the leading rising edge of the SCK line so your setup of SPI_MODE2 is incorrect which samples on the leading failing edge.
It should be SPI_MODE0 to sample on the leading rising edge.
The SPI:end(); call goes into whatever code gets called when the program is done with the SPI port.
The PCF8814 also needs needs hardware pins PS0 and PS1 set correctly in hardware to function in SPI mode, I do not know if this was done in the display you are using, it may well be setup for I2C mode. You can find the datasheet on SourceForge here: http://mylcd.sourceforge.net/files/pcf8814.pdf