eugedebe
Newbie level 2
Hi, i m trying to make a small code in mplab with a pic16f1824. The problem that i am having, is that when i write the buffer register of the spi (the SSP1BUF) and then i ask (with a while loop) if the transmition had finished, i stay in an infinite loop.
I think that my problem is that i m not setting all the pic to work with de spi mode. Can you tell me what do you see wrong or what is the correct setup?
here is the code:
I think that my problem is that i m not setting all the pic to work with de spi mode. Can you tell me what do you see wrong or what is the correct setup?
here is the code:
Code:
#include <htc.h>
__CONFIG(FOSC_INTOSC & WDTE_OFF & CP_ON & MCLRE_OFF & BOREN_OFF & PWRTE_ON &
LVP_OFF & BORV_19 & STVREN_ON & PLLEN_OFF & WRT_ALL & CLKOUTEN_OFF);
//functions decl
unsigned char spi_write(int cmd_spi);
void conf(void);
////////////////////////////////////////////
main(){
int cmdword;
conf();
for(;;){
cmdword=0x32;
spi_write(cmdword);
}
}
}
unsigned char spi_write(int cmd_spi){
char dummy;
while (SSP1IF)
dummy=0;
while(BF)
dummy=12;
WCOL=0;
SSP1BUF=0x00;
SSP1BUF = cmd_spi; //(truncated, doesn't matter in the this example)
while(BF!=1); //HERE IS STAY FOR EVER!!!
dummy=0;
return SSP1BUF;
}
void conf(void){
SPLLEN=0b0; //PLLX4 DES
IRCF0=0b1; //4MHZ
IRCF1=0b0;
IRCF2=0b1;
IRCF3=0b1;
//CONFIG OSC
SCS1=0b1; // INTERNAL OSCILLATOR
TMR0CS=0b0; //
PSA=0b0; // habilitaci´n del preescaller
PS0=0b1;//el preescaller en 111
PS1=0b1;
PS2=0b1;
/////////////////////// SPI (PAGINA 248)
//PASO 1: CONF PRIORITY PINS
//SCK (RC0):
// PRIORITY 1 PRIORITY 1 IT IS NOT NECESARRY TO CONFIG
//SD0 (RC2):
SDOSEL=0; // SELECT RC2
//prioridades del pin RC2: SD0, P1D,PIB
//SDI (RC1) PRIORITY 1 IT IS NOT NECESARRY TO CONFIG:
PEIE=1; //ENABLE PERIPH INTERRUPTS (DON'T KNOW IF IT IS NECESARRY)
SSP1IE=1; //ENABLE INT SPI (DON'T KNOW IF IT IS NECESARRY)
//PASO 2: CONFIG PINS
TRISC0=0; //SCK MASTER TRISC0 CLR (OUT)
TRISC1=1; //SDI TRISC1 SET(INPUT)
TRISC2=0;// SDO TRISC2 CLR (OUT)
TRISC3=1; // SS TRISC3 SET (INPUT)
//PASO 3: CONFIG SCK AND MODE
SSPM0=1;
SSPM1=0; //CLOCK IN 4mHZ/4 = 1MHZ IN SPI MASTER MODE
SSPM2=0; //
SSPM3=0;
CKP=0; //CLOCK POL IN 0
SMP=0; //SAMPLE IN THE MIDDLE
CKE=0; //Transmit occurs on transition from Idle to active clock state
SSPEN=1; //Enables serial port and configures SCK(RC0 output cmos), SDO(RC2 outp cmos), SDI(RC1 inp cmos) and SS (RC3 slave select as the source of the serial port pins
////FIN SPI
}
Last edited by a moderator: