spi clock

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

spi clock

Post by brandonb »

in flowcode i can communicate with external eeprom and sram chips with spi clock set to idle low which would be mode 0,0... in c this works as well with the exception that the master can't read the slaves sent data correctly..... the only way using spi in c that i can receive information from external chips is if i set clock to idle high.... here is the code that works

Code: Select all

void spi_init(){
  clearbit(trisc,3);
  setbit(trisc,4);
  clearbit(trisc,5);
  smp_bit=0;//data sampled at end
  cke_bit=0;//SPI MODE 1,1
  ckp_bit=1;// SPI MODE 1,1
  clearmask(sspcon1,15);// 8Mhz bitrate
  sspie_bit=1;// enable spi done interrupt
  sspif_bit=0;//clear done flag
  sspen_bit=1;//sets up serial port
  peie_bit=1;// enable peripherial interrupts
  gie_bit=1;// enable global interrupts
}
char spi_get(void){
  sspbuf=0;// send dummy
  while(sspif_bit==0);// wait for done
  sspif_bit=0;// clear done flag
  return sspbuf;// return read

}
void spi_send(char dip){
   char dummy;
   sspbuf=dip;
   while(sspif_bit==0);// wait for done
   sspif_bit=0;// clear done flag
   dummy=sspbuf;
}
here is the code that does work as viewed with logic16 but master pic can't read incoming data correctly

Code: Select all

  
smp_bit=0;//data sampled at end
cke_bit=1;//SPI MODE 0,0
ckp_bit=0;// SPI MODE 0,0
i don't see anything in the datasheet that would explain that this won't work with 23lc512 or 25lc512?

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: spi clock

Post by Spanish_dude »

Have you checked the errata for either the eeprom/sram and the microcontroller ?

Never really played with spi before so I can't help you on this.

- Nicolas

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: spi clock

Post by brandonb »

i don't know it all works great now when i rewrote it in old school fashion
void spi_init(char _ckp, char _cke, char _smp, char _sspm){

clearbit(intcon,7);// disable interrupts while setup
clearbit(trisc,3);// set SCK pin as output
setbit(trisc,4);// set DSI pin as input
clearbit(trisc,5);// set SDO pin as output

if(_smp){setbit(sspstat,7);}
else{clearbit(sspstat,7);}
if(_cke){setbit(sspstat,6);}
else{clearbit(sspstat,6);}
if(_ckp){setbit(sspcon1,4);}
else{clearbit(sspcon1,4);}
portmask(sspcon1,3,_sspm);// masking bits 0-1 with _sspm value

setbit(pie1,3);// sspie=1
clearbit(pir1,3);// sspif=0
setbit(sspcon1,5);// sspen=1
setbit(intcon,6);// enable peripherial interrupts peie
setbit(intcon,7);// enable global interrupts gie
}

char spi_get(void){
sspbuf=0;// send dummy
while((pir1&8)==0);// wait for done (sspif)
clearbit(pir1,3);// clear done flag (sspif)
return sspbuf;// return read

}
void spi_send(char send_char){
sspbuf=send_char;
while((pir1&8)==0);// wait for done (sspif)
clearbit(pir1,3);// clear done flag (sspif)
send_char=sspbuf;
}
here are defines to make sense of it
#define setbit(port,bit) port|=1<<bit
#define clearbit(port,bit) port&=~(1<<bit)
#define togglebit(port,bit) port^=1<<bit
#define setmask(port,mask) port|=mask
#define clearmask(port,mask) port&=~(mask)
#define testbit(port,bit) port&1<<bit
#define readpin(port,bit) (port&1<<bit)>>bit
#define portmask(port,mask,value) port=(port & ~(mask)) | ((value) & mask)
if anyone is curious about libraries, i wrote a bunch in mikroc for pic for the 16f1939 chip

Post Reply