hi
i'm trying to interface PIC16F877A to SPI eeprom "25AA010A" from microchip and btw it's my 1st time to interface SPI and i'm using Mikroc in programming, i guess i understand the datasheet and the Mikroc SPI library help well but it seems that iam missing something
i wrote a code to write value in the address 0x00 when switch on RB0 pushed and released and read the address 0x00 then display it's content on LEDs on PORTD when i do the same for the switch on RB1 but the problem is that the value is not correct
here is the code i wrote
and the source and design here
and finally the circuit
i'm trying to interface PIC16F877A to SPI eeprom "25AA010A" from microchip and btw it's my 1st time to interface SPI and i'm using Mikroc in programming, i guess i understand the datasheet and the Mikroc SPI library help well but it seems that iam missing something
i wrote a code to write value in the address 0x00 when switch on RB0 pushed and released and read the address 0x00 then display it's content on LEDs on PORTD when i do the same for the switch on RB1 but the problem is that the value is not correct
here is the code i wrote
Rich (BB code):
/*
buffer : spi buffer
data_in : data to be read from spi
data_out : data to be written to spi
address : eeprom(25AA010A) address pointer
status_reg : eeprom(25AA010A) status register
*/
unsigned short buffer,data_in,status_reg;
//------------------------------------------------------------------------------
//Read data from memory array beginning at selected address
void READ(unsigned short address){
PORTB.f7=0;
Spi_Write(0b00000011);//'0000 x011' READ Instruction
Spi_Write(address);
data_in=Spi_Read(buffer);
PORTB.f7=1;
}
//------------------------------------------------------------------------------
//Reset the write enable latch (disable write operations)
void WRDI(){
Spi_Write(0b00000100);//'0000 x100' WRDI Instruction
}
//------------------------------------------------------------------------------
//Set the write enable latch (enable write operations)
void WRENL(){
Spi_Write(0b00000110);//'0000 x110' WREN Instruction
PORTB.f7=1;
PORTB.f7=0;
}
//------------------------------------------------------------------------------
//Write data to memory array beginning at selected address
void WRITE(unsigned short address, unsigned short data_out){
PORTB.f7=0;
WRENL();
Spi_Write(0b00000010);//'0000 x010' WRITE Instruction
Spi_Write(address);
Spi_Write(data_out);
WRDI();
PORTB.f7=1;
}
//------------------------------------------------------------------------------
//Read STATUS register
void RDSR(){
}
//------------------------------------------------------------------------------
//Write STATUS register
void WRSR(){
}
//------------------------------------------------------------------------------
void inti(){
TRISB = 0x03;
PORTB = 0x80;
TRISD = 0x00;
PORTD = 0x00;
Spi_Init_Advanced(MASTER_OSC_DIV4,DATA_SAMPLE_MIDDLE,CLK_IDLE_LOW,LOW_2_HIGH);
}
//------------------------------------------------------------------------------
void main(){
inti();
while(1){
if(PORTB.f0=1){
while(PORTB.f0=1){}
PORTD=0x00;
WRITE(0x00,0x04);
}
if(PORTB.f1=1){
while(PORTB.f1=1){}
READ(0x00);
PORTD=data_in;
}
}
}
//------------------------------------------------------------------------------
and finally the circuit