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 for the SPIF flag, 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 as the code gets stuck at while (!(SPCFG & 0x80));
If any one can help me find a solution it will be great
CODE
/************************************************* ****************************************/
//Program: Interfacing AT93C46A serial EEPROM to a P89V51RD2 using the SPI//
/************************************************* ****************************************/
#include <stdio.h>
#include <p89v51rx2.h>
//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 FUNCTIONS
//delay(10) = 200usec.
void delay(unsigned char Delval)
{
unsigned char i=25;
for(;Delval!=0;Delval--)
for(;i!=0;i--);
}
void delay_ms(unsigned int Delval_ms)
{
for(;Delval_ms!=0;Delval_ms--)
{
delay(250);
delay(250);
delay(250);
delay(250);
}
}
//MAIN
void main(void)
{
LED1 = 0;
LED2 = 0;
cs = 1;
di = 1;
dou = 1;
clk = 1;
delay_ms(10);
LED1 = 1; // Start of SPI
SPCTL = 0x73; //initialize SPI
SPDAT = 0x05;
while (!(SPCFG & 0x80));
LED2 = 1; // End of SPI transfer
while(1){}
}
I have stripped down the code to bare basics for troubleshooting and to identify the problem.
I have found that when i am testing for the SPIF flag, 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 as the code gets stuck at while (!(SPCFG & 0x80));
If any one can help me find a solution it will be great
CODE
/************************************************* ****************************************/
//Program: Interfacing AT93C46A serial EEPROM to a P89V51RD2 using the SPI//
/************************************************* ****************************************/
#include <stdio.h>
#include <p89v51rx2.h>
//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 FUNCTIONS
//delay(10) = 200usec.
void delay(unsigned char Delval)
{
unsigned char i=25;
for(;Delval!=0;Delval--)
for(;i!=0;i--);
}
void delay_ms(unsigned int Delval_ms)
{
for(;Delval_ms!=0;Delval_ms--)
{
delay(250);
delay(250);
delay(250);
delay(250);
}
}
//MAIN
void main(void)
{
LED1 = 0;
LED2 = 0;
cs = 1;
di = 1;
dou = 1;
clk = 1;
delay_ms(10);
LED1 = 1; // Start of SPI
SPCTL = 0x73; //initialize SPI
SPDAT = 0x05;
while (!(SPCFG & 0x80));
LED2 = 1; // End of SPI transfer
while(1){}
}