I am trying to implement SPI protocol with an EEPROM chip. But I think I am missing something, can anyone give me an opinion on what should I add? Also the EEPROM has 3MHz max input, if I make it on 500Hz, will it be ok?
Code:
void SPI_EEPROM ()
{
PWM_function ();
CHIP_SELECT ();
WREN_INSTRUCTION ();
WRITE_INSTRUCTION ();
CHIP_SELECT ();
READ_INSTRUCTION ();
}
void CHIP_SELECT ()
{
output_high (PIN_B0); /* After the EEPROM is selected by "chip select" going low, on the falling front, a control byte must be sent, A8 bit is the address bit */
output_low (PIN_B0);
}
void WREN_INSTRUCTION ()
{
/* WREN instruction = the first byte transmitted is the control byte, with the MSB A8 being transmitted first, Byte = 0000 X110 */
output_low (PIN_B2); /* "0" */
output_high (PIN_B2); /* "1" */
output_high (PIN_B2); /* "1" */
output_low (PIN_B2); /* A8 */
output_low (PIN_B2); /* "0" */
output_low (PIN_B2); /* "0" */
output_low (PIN_B2); /* "0" */
output_low (PIN_B2); /* "0" */
output_high (PIN_B0); /* The real programming will start after the pin "CS" is turned high */
}
void WRITE_INSTRUCTION ()
{
/* WRITE instruction = 0000 A010, A is the MSB A8 */
output_low (PIN_B2); /* "0" */
output_high (PIN_B2); /* "1" */
output_low (PIN_B2); /* "0" */
output_low (PIN_B2); /* "A8" */
output_low (PIN_B2); /* "0" */
output_low (PIN_B2); /* "0" */
output_low (PIN_B2); /* "0" */
output_low (PIN_B2); /* "0" */
output_high (PIN_B0); /* Pull the chip selet pin high when the operation is done, because after its held high, the information is written to the EEPROM */
}
void READ_INSTRUCTION ()
{
unsigned int8 Data;
/* READ instruction = 0000 A011, A is the MSB A8 */
output_high (PIN_B2); /* "0" */
output_high (PIN_B2); /* "1" */
output_low (PIN_B2); /* "1" */
output_low (PIN_B2); /* "A8" */
output_low (PIN_B2); /* "0" */
output_low (PIN_B2); /* "0" */
output_low (PIN_B2); /* "0" */
output_low (PIN_B2); /* "0" */
Data = input (PIN_B4);
output_d (DATA);
output_high (PIN_B0); /* Pull the chip selet pin high when the operation is done, because after its held high, the information is written to the EEPROM */
}
/* PWM program from PCM programmers's post */
/* Try the following program. Copy and paste it into MPLAB. */
/* Remember that the PWM duty value should not be set greater */
/* than the middle number in the setup_timer_2() function. */
/* If you set it greater than that value, then you will get a constant */
/* high level out. (100% duty cycle) */
void PWM_function ()
{
output_low(PIN_C1); // Set CCP2 output low
output_low(PIN_C2); // Set CCP1 output low
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_ccp2(CCP_PWM); // Configure CCP2 as a PWM
setup_timer_2(T2_DIV_BY_16, 124, 1); // 500 Hz,
set_pwm1_duty(31); // 25% duty cycle on pin C2, 25% of 124
set_pwm2_duty(62); // 50% duty cycle on pin C1, 50% of 124
//while(1); // Prevent PIC from going to sleep (Important !)
}