Hi,
"SCL" usually is an "I2C" pin, not an "SPI" pin. Please check this.
And for the rest: How do you think we can help you without showing your code and schematic?
Klaus
Check your connections ! Obviously some mistake in your wiring.
But also give more details of "problem comes". What problem exactly ?
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#include <xc.h>
#define _XTAL_FREQ 20000000
//SPI lines
#define CS RA5 //Chip select ON RA5
#define MOSI RC5 //Master Out Slave In
#define MISO RC4 //Master in slave out
#define SCK RC3 //Clock
/*SPI_COMMANDS
#define READ 0b00001100;
#define WRITE 0b00001010;
#define EWEN 0b10001001;
#define EWDS 0b00001000;
#define ERASE 0b00001110;*/
unsigned char HiByte; //High Order Byte
unsigned char LoByte; //Low Order Byte
unsigned char HiData; //Hi Data Byte (upper 8 bits of word)
unsigned char ReadData[1]; //Holds Device data being read back
unsigned char Length; //Number of Bytes to read (1 for x8 mode)
unsigned char Result,rxdata;
void SPI_init(void); // Init. function
void Ewen(void); // Erase/Write Enable function
void Ewds(void); // Erase/Write Disable function
void SPI_write_cmd(unsigned char, unsigned char, unsigned char); // 8-bit Write function
unsigned char DataRdyMwire(void );
char SPI_Write(char);
unsigned char SPI_Read (unsigned char, unsigned char, unsigned char , unsigned char);
char getdata( unsigned char, unsigned char );
void main(void)
{
HiByte = 0x0A; //Dummy Zero's, Start Bit and Op Code for Byte Write
LoByte = 0x10; //Address bits for 93LC66C
HiData = 0x5A; //Upper 8 bits of Data word
Length = 0x01; //Number of bytes to read (1 for x8)
while(1){
SPI_init();
Ewen();
// __delay_ms(1);
SPI_write_cmd(HiByte, LoByte, HiData);
// __delay_ms(1);
Ewds();
HiByte = 0x0C; //Changes OpCode from Write to Read for next function
__delay_us(1);
SPI_Read(HiByte, LoByte, ReadData, Length);
}
while(1); //Loop here forever
}
void SPI_init()
{
// ADCON1 = 0X4F;
TRISC5 = 0; // Configure PORTC I/O, MOSI
TRISC3 = 0; // Configure PORTC I/O, CLK
TRISC4 = 1; // Configure PORTC I/O, MISO
PORTA = 0; // Clear all PORTA pins
TRISA = 0;
SSPSTAT = 0b01000000;
SSPCON = 0b00100001;
}
void Ewen(void)
{
CS = 1; //Select Device
SPI_Write(0x09); //EWEN Command
SPI_Write(0x08);
__delay_us(1);
CS = 0; //Deselect Device
}
char SPI_Write(char incoming)
{
SSPBUF = incoming; //Write the user given data into buffer
if (SSPCONbits.WCOL == 1) // test if write collision occurred
{
return(-1);
} // if WCOL bit is set return negative #
else
{
while(!SSPSTATbits.BF ); // wait until bus cycle complete
//rxdata = SSPBUF;
return (0); // if WCOL bit is not set return non-negative#
}
}
void SPI_write_cmd(unsigned char HiByte, unsigned char LoByte, unsigned char HiData)
{
CS = 1; // Select Device
SPI_Write(HiByte); // Send HiByte (Dummy bits, SB, and Op Code)
SPI_Write(LoByte); // Send LoByte (Address for the 93LC66C)
SPI_Write(HiData); // Send HiData to EEPROM
CS = 0; // Deselect Device and initiate write
__delay_us(1);
CS = 1; // Select Device
while (!DataRdyMwire()); // Wait for Write to complete
CS = 0; // Deselect device
// __delay_us(1);
}
unsigned char DataRdyMwire(void)
{
if(PORTCbits.RC4) // test if DO line is high
return (1); // return ready = true
else
return (0); // else ready = false
}
void Ewds(void)
{
CS = 1; //Select Device
SPI_Write(0x08); //EWDS Command to disable write/erase operation
SPI_Write(0x00);
__delay_us(1);
CS = 0; //Deselect Device
}
unsigned char SPI_Read(unsigned char HiByte, unsigned char LoByte, unsigned char ReadData, unsigned char Length )
{
LoByte = LoByte << 1;
CS = 1; // Select Device
SPI_Write( HiByte ); // Send Read OpCode
SPI_Write( LoByte ); // WRITE word address to EEPROM
Result=getdata(ReadData, Length); // Reads 1 byte
__delay_us(1);
CS = 0; // Deselect Device
}
char getdata( unsigned char ReadData, unsigned char length )
{
while ( length ) // stay in loop until length = 0
{
SSPBUF = 0x00; // initiate bus cycle
while(!SSPSTATbits.BF ); // wait until byte has been received
__delay_us(1);
ReadData = SSPBUF; // save byte
length--; // reduce string byte count by 1
}
return(ReadData);
}
I am simply looking at datasheet of 93LC46A, which is what you mentioned. Clearly mentions 9-bit in many places. Also, it's A5.. A0 and not A6.. A0
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?