PIC 18f26k20 and Microchip Data FLash - SPI sync problem

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
Posts: 1
Joined: Fri Apr 04, 2014 11:47 am

PIC 18f26k20 and Microchip Data FLash - SPI sync problem

Post by alexvx » Sat Apr 05, 2014 9:27 am

Hi guys :)

I have a random SPI synchronization problem when using a PIC 18f26k20 MCU to write a Microchip SST25VF032B data flash memory. The PIC is initialized in SPI Mode 0 (which is supported according to datasheet of the flash memory). Usually writing works normally and then after some random period of time the bytes sent to the memory start losing their MSb. This problem affects bytes where the MSb is set. For example writing 0xFF results in 0x7F while writing 0x0A gives a correct result.

Here are my initialization and byte send codes. I am using a C18 compiler.

Code: Select all

void initializeSPI() {
  SSPSTAT = 0xC0;
  SSPCON1 = 0x20;

 unsigned char spibyteio(unsigned char byteout) {
   SSPBUF = byteout;
   return SSPBUF;

 void writeByte(unsigned short long address, unsigned char byteout) {
   union converter cnv;
   cnv.l = address;
   waitWhileBusy();  //Polls the status reg of the dataflash to check if it's busy
   writeEnable();  //Sends a write enable sequence as required by the data flash
   chipEnable(); //Pulls low CE pin
   spibyteio(0x02); //Byte rite command
   chipDisable(); //Pulls high CE pin
I use the union to break down three byte addresses (unsigned short long in C18) into separate bytes like this:

Code: Select all

union converter {
   unsigned short long l;
   unsigned char c[3];
Thank you in advance for any assistance :)


Posts: 6
Joined: Mon Sep 15, 2014 2:24 pm

Re: PIC 18f26k20 and Microchip Data FLash - SPI sync problem

Post by okayid » Fri Sep 19, 2014 10:41 am

Instead of using: while(!SSPSTATbits.BF);
Try using: while(!PIR1bits.SSPIF); //PIC18LF14K22 may be different for 18F26K20.
I think this is recommended by Microchip as there may be some timing issues with SSPSTATbits.BF.

Post Reply