Hola, ya llevo tiempo sin trabajar con microcontroladores y estoy un poco oxidado y me gustaria un poco de ayuda,
tengo un pic 24FJ64GA006 y una eeprom 25L256 conectada por el spi2 del pic pero no puedo grabar datos en ella.
este es el codigo que estoy usando:
#include <24FJ64GA006.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOJTAG //JTAG disabled
#FUSES CKSFSM //Clock Switching is enabled, fail Safe clock monitor is enabled
#device ICSP=1
#use delay(crystal=20000000)
#use rs232(UART2, baud=9600, stream=UART_PORT1)
#ifndef EEPROM_SELECT
#define EEPROM_SELECT PIN_G9 // Pin CS de la EEPROM
#define EEPROM_DI PIN_G8 // Pin SDI de la EEPROM
#define EEPROM_DO PIN_G7 // Pin SDO de la EEPROM
#define EEPROM_CLK PIN_G6 // Pin SCK de la EEPROM
#endif
/*
#define WREN 6 // Habilitar Escritura
#define WRDI 4 // Deshabilitar Escritura
#define RDSR 5 // Leer Registro Status
#define WRSR 1 // Escribir Registro Status
#define READ 3 // Leer memoria
#define WRITE 2 // Escrbir Memoria
#define RIP 131 // Leer pagina de identificacion (El bit A10 de la direccion debe ser 0, los demas no importan)
#define WIP 130 // Escribir pagina de identiicacion (El bit A10 de la direccion debe ser 0, los demas no importan)
#define RLS 131 // Leer Estado bloqueado de la pagina de identificacion (El bit A10 de la direccion debe ser 1, los demas no importan)
#define LID 130 // Bloquear la pagina de identificacion (El bit A10 de la direccion debe ser 1, los demas no importan)
*/
//#use spi(MASTER, SPI2, MODE=0, BITS=8, ENABLE=PIN_G9, stream=SPI_PORT1)
#use spi(MASTER, SPI2, DI=EEPROM_DO, DO=EEPROM_DI , CLK=EEPROM_CLK, BITS=8,FORCE_HW, MSB_FIRST, MODE=0, stream=SPIEEPROM)
void init_ext_eeprom()
{
output_low(EEPROM_SELECT);
output_low(EEPROM_DI);
output_low(EEPROM_CLK);
output_high(EEPROM_SELECT);
setup_spi2(spi_MASTER | spi_L_to_H | spi_clk_div_16| SPI_XMIT_L_TO_H); // Configuración SPI
}
void write_ext_eeprom(int32 direccion, BYTE data) {
int address[3];
// Construye el paquete de direccion (24 bits)
address[0]= make8(direccion, 2);
address[1]= make8(direccion, 1);
address[2]= make8(direccion, 0);
output_low(EEPROM_SELECT); // Habilita CS
spi_xfer(SPIEEPROM,0x06); // Habilita escritura
output_high(EEPROM_SELECT); // Deshabilita CS
output_low(EEPROM_SELECT); // Habilita de nuevo la EEPROM
spi_xfer(SPIEEPROM,0x02); // Configura la operación de escritura.
spi_xfer(SPIEEPROM,address[0]); // Direcciona la escritura del dato (Byte 2)
spi_xfer(SPIEEPROM,address[1]); // Direcciona la escritura del dato (Byte 1)
spi_xfer(SPIEEPROM,address[2]); // Direcciona la escritura del dato (Byte 0)
spi_xfer(SPIEEPROM,data); // Escribe el dato
output_high(EEPROM_SELECT); // Deshabilita CS
delay_ms(5);
}
BYTE read_ext_eeprom(int32 direccion){//,int cant, BYTE data[]) {
int address[3];
BYTE data;
// Construye el paquete de direccion (24 bits)
address[0]= make8(direccion, 2);
address[1]= make8(direccion, 1);
address[2]= make8(direccion, 0);
output_low(EEPROM_SELECT); // Habilita CS
spi_xfer(SPIEEPROM,0x03); // Configura la operación de escritura.
spi_xfer(SPIEEPROM,address[0]); // Direcciona la lectura del dato (Byte 2)
spi_xfer(SPIEEPROM,address[1]); // Direcciona la lectura del dato (Byte 1)
spi_xfer(SPIEEPROM,address[2]); // Direcciona la lectura del dato (Byte 0)
data=spi_xfer(SPIEEPROM,0xFF); // Lee el dato
output_high(EEPROM_SELECT); // Deshabilita CS
return(data);
me permite leer la eeprom pero no puedo escribir nada en ella.
cualquier ayuda sera muy agradecida.
saludos.