Problem with SPI implementation - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Problem with SPI implementation

aquibaquib Posts: 1Member
I am using a P89V51RD2, which is a controller from NXP. I am having problems using the SPI protocol to communicate with a serial EEPROM.
I have stripped down the code to bare basics for troubleshooting and to identify the problem.
I have found that when i am testing if the SPIF flag is set which indicates a successful data transfer. The program execution gets stuck at this point and does not proceed further.
I have used Leds for troubleshooting. The code which i am working with is as below. LED2 never glows.

If any one can help me find a solution it will be great

//Program: Interfacing AT93C46A serial EEPROM to a P89V51RD2 using the SPI//


//SPI Pin Defination
sbit cs = P1^4;
sbit di = P1^5;
sbit dou = P1^6;
sbit clk = P1^7;

//Debugging LEDs
sbit LED1 = P2^0;
sbit LED2 = P2^1;

//delay(10) = 200usec.

void delay(unsigned char Delval)
unsigned char i=25;

void delay_ms(unsigned int Delval_ms)
void main(void)

LED1 = 0;
LED2 = 0;

cs = 1;
di = 1;
dou = 1;
clk = 1;

LED1 = 1; // Start of SPI
SPCTL = 0x73; //initialize SPI
SPDAT = 0x05;
[color=Red]while (!(SPCFG & 0x80));[/color]
LED2 = 1; // End of SPI transfer




  • LundinLundin Posts: 3,711Member
    while (!(SPCFG & 0x80));

    The ! operator is reserved for boolean logic. You shouldn't use it on bitwise operations. The correct code should be like this:

    [code]while ( (SPCFC & 0x80) == 0)

    Also, it is quite possible that the flag needs to be cleared. On Freescale micros, the SPI flags are cleared in quite odd ways. No idea how NXP controllers works though.
Sign In or Register to comment.