Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

problems to debug in mplab a small code with spi mode

Status
Not open for further replies.

eugedebe

Newbie level 2
Joined
Sep 28, 2011
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,302
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:

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:

https://ww1.microchip.com/downloads/en/devicedoc/spi.pdf


By the way, you only do

SPLLEN=0; //PLLX4 DES //SPLLEN=0b0; //PLLX4 DES

as well as the rest having 0bXX,




thanks for the answer, john. I have read the .pdf, and i still don't know where is the mistake or what i 'm not setting in the SPI configuration.

The PDF tells (and the datasheet of my pic16 too) that i have to set the following registers (in the list i didn't write the optional configuration bits like cke, polarity clock, time of sample..., i only wrote the bits that must be config):

SSPCON: SSPEN (done)
SSPM bits 3 though 0 (done).
TrisC: SCK, SDI and SDO are bits 3, 4 and 5 of PORTC respectively. This is why a “1” appears in the binary value sent to TRISC. A “1” will configure that pin as an input, while a 0 forces that signal to be an output. (done)



Then, the page 30 of the pdf describe the steps of an spi master mode transmition. I think that i m doing the same steps, inspite that the debugger doesn't agree...


in page 36: "...The next few lines of code move data from our counter and then send it to SSPBUF. Once SSPBUF is loaded with data, it will begin the exchange of data with the slave. Then, the code checks the BF flag. When it has been determined that the BF flag is set, the data is read from the SSPBUF register. This will clear the BF flag in the process...."


I do that, but bit BF never takes the value 1 and i stay in the loop while(BF!=1);
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top