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.

pic16f72 as master in SPI

Status
Not open for further replies.

Ram Prasadh

Member level 2
Joined
Feb 16, 2011
Messages
51
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,725
I am new in using SPI. I am interfacing a MAXQ3180 and PIC16F72(master). Can you please tell me whether I am checking the SSPIF and the BF correctly. I am sending 0x00 and expecting 0xc1 from the max.But I am not able to get it. My SDO,SDI,SCK,RC1(reset pin to max) pin are in logic high after connecting with max. I am using a 12Mhz crystall oscillator to the pic and 8Mhz crystall in max. It is stated in the Max data sheet that"the SPI clock frequency used by the master must be less than or equal to the MAXQ3180’s clock frequency divided by 8. For example, when the MAXQ3180 is running at 8MHz, the SPI clock frequency must be 1MHz or less for proper communications operation". Here in pic , my 12 Mhz will get divided by 16,I am getting 750 Khz nearly for the SPI clock.Is there anything other than this I have to do? what may be the problem?

void main()
{
ADCON1=6;
TRISA=0X00;
TRISB=0X00;
TRISC=0X10;
INTCON=0x40; //only peripheral interrupt is enabled
PIE1=0x08; //SSPIE is enabled
SSPIF=0;
SSPCON=0b00110001; //SSPEN=1 CKP=1;
SSPSTAT=0b11000000;
RC1=0; RC1=1; // RESET pin
write_enable();
}
void write_enable()
{
RA5=0; //Slave Select Pin
SSPIF=0;
SSPBUF=0X00;
while(SSPIF==0); SSPIF=0; //transmit
delay_50uS();
while(SSPIF==0 && BF==0);SSPIF=0; //recieve
if(SSPBUF==0XC1)
RB7=1;
else
RB6=1;
RA5=1; //SPI over
for(;;);
}


PS* I dont have a logic analyzer and cannot afford one. I am burning the hex and using it.
 

Hello,

Here is the code I would use for a SPI transaction.

char SPI_BYTE (char shift)
{
sspbuf = shift; //write the Char
while ((sspstat & 0x01) == 0); //wait until sspbuf is empty
shift = sspbuf; //receive the Char
return shift;
}

Also I see you have enabled the SPI interrupt but are not servicing it.

I would recommend you disable the interrupt, use my code from above and see if this helps at all.
 
Hello Ben,

Thank you very much for your reply. I have two questions to ask now.
1. I burned your code and displayed the value I got in SSPBUF in PORTB.But all the LEDs gets on at the same time.ie FF.In the data sheet , for the buffer flag, it is given as "if BF=1,Recieve complete , SSPBUF is full". So my understanding is that, we should check the buffer flag only when we are only recieving any data. Is my understanding correct?


2. "The conditions that will set the SSPIF flag are a Transmission or Reception has taken place".This is from the data sheet . I am just going to check whether that flag is set or not.If it is set,Transmission or Reception has taken place.Thats all I am doing.Why do we need a Interrupt service there.?


I am giving my code below. This code just gives a value of FF in PORTB.Help me to solve this please....

void write_enable()
{
while(1)
{
unsigned char i;
RC1=0; //reset is 0
delay_200mS();
RC1=1;RA5=0;RC3=0;RC3=1; //reset=1;slave select=0;clock=0;clock=1;
i=0X80;
SSPBUF=i;
while((SSPSTAT & 0x01)==0); //checking buffer flag
delay_200mS();
PORTB=SSPBUF;
RA5=1;
}
}


PS*afer sending 0x80 , I am expecting a 0xc1 from the slave
 
Last edited:

Hello,

Have you tried adding the required 8.2ms delay after enabling the reset of the device? Depends on your clock source but worth adding a bit of a delay here.

char spi_byte(char data)
{
SSPBUF=data;
while((SSPSTAT & 0x01)==0); //checking buffer flag
return SSPBUF;
}

void write_enable()
{
while(1)
{
RC1=0; //reset is 0
delay_200mS();
RC1=1;RA5=0; //reset=1;slave select=0;
delay_200mS(); //Wait for device to switch to high frequency mode
PORTB = spi_byte(0X80);
delay_200mS();
RA5=1;
}
}
 
Last edited:

    V

    Points: 2
    Helpful Answer Positive Rating
Hello Ben,
In the last post, I have used a reset source, (ie RC1).But I came to know that this reset source is not needed when we are using a reset source from the master itself. Moreover , I am using a 12Mhz clock and according to the SSPSTAT i am setting the clock Freq as Fosc/16(appr 750Khz) which is Sufficient to drive a MAXQ3180(according to the data sheet).I have added the code which you have posted , but when I read the final value in the SSPBUF, I am getting only 0xFF(As I said I am expecting a 0xc1 from the slave)I have posted my latest code also with my Schematic.

Please help me with what I have to do....
98_1305181514.jpg








void main()
{
delay_15mS();
SSPSTAT=0b11000000;
SSPCON=0b00110001;
TRISC=0X10;
ADCON1=0X06;
TRISA=0X00;
RA5=1;
delay_100uS();
RA5=0;
TRISB=0X00;
PORTB=0X00;
PORTC=0x00;
write_enable();
}
char spi_byte(char data)
{
SSPBUF=data;
while((SSPSTAT & 0x01)==0); //checking buffer flag
delay_25uS();
return SSPBUF;
}

void write_enable()
{
while(1)
{
delay_200mS(); //Wait for device to switch to high frequency mode
PORTB = spi_byte(0X80);
delay_200mS();
RA5=1;
}
}
 

Hello,

Any chance that portC being low is holding the device in reset?

Might also be worth giving a quick pulse on the reset pin. I find that some modules need this to start up correctly even if they don't mention the requirement in the datasheet.

Your schematic looks ok to me so hopefully its not your circuit.
 

I am still getting the same garbage value from the slave even if I give a reset pulse.Is an external pull-up needed for the PIC? I have not used any. Will it have an effect? Should I have to do anything with the clock (SCK) connected to RC3? Part of the code is below....


unsigned char SPI_OUTPUT(unsigned char b)
{
SPI_RESET=0; //connected to RC1
delay_200mS();
SPI_RESET=1;
SSPBUF=b;
while((SSPSTAT & 0X01) == 0);
delay_50uS();
while((SSPSTAT & 0X01) == 0);
return(SSPBUF);
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top