Autor Tema: problema 25L256 y pic24FJ64GA006  (Leído 1496 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado NarKo

  • PIC10
  • *
  • Mensajes: 12
problema 25L256 y pic24FJ64GA006
« en: 03 de Octubre de 2013, 20:09:30 »
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.

Desconectado PCCM

  • PIC16
  • ***
  • Mensajes: 109
Re: problema 25L256 y pic24FJ64GA006
« Respuesta #1 en: 04 de Octubre de 2013, 00:37:49 »
Supongo que es una memoria de 256KB por lo del 25L256.
Si pudieras enlazar su datasheet seria mejor.

Memorias de este tamaño solo tienen 2 bytes de dirección y tu estas colocando 3 bytes(estos son para memorias de 1MB).

Además en mi caso tengo que colocar un retardo de 10ms después de habilitar escritura  osea después de esto:

output_low(EEPROM_SELECT); // Habilita CS
spi_xfer(SPIEEPROM,0x06); // Habilita escritura
output_high(EEPROM_SELECT); // Deshabilita CS

Aunque esto tómalo como ultima medida.

Desconectado NarKo

  • PIC10
  • *
  • Mensajes: 12
Re: problema 25L256 y pic24FJ64GA006
« Respuesta #2 en: 04 de Octubre de 2013, 14:33:05 »
adjunto Datasheet de la memoria, tenias razon son 16 bit de direccion, ya solucione el tema de la escritura de datos.

/////////////////////////////////////////////////////////////
//            Driver 25L256
//            Autor: Sebastia Olivares
//            configuracion para spi2 del pic24fj64ga006
/////////////////////////////////////////////////////////////

#define EEPROM_ADDRESS long int
#define EEPROM_SIZE    32768  //32Kb x 8
#define READ           0b00000011
#define WRITE          0b00000010
#define WRDI           0b00000100
#define WREN           0b00000110
#define RDSR           0b00000101
#define WRSR           0b00000001


#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

// SPI mode definitions.
#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)


#use spi(spi2,MASTER, DI=EEPROM_DO, DO=EEPROM_DI , CLK=EEPROM_CLK, BITS=8,FORCE_HW, MSB_FIRST, MODE=0, stream=SPIEEPROM)

void init_ext_eeprom()
{
   spi_init(SPIEEPROM,20000000);
   output_low(EEPROM_SELECT);
   output_low(EEPROM_DI);
   output_low(EEPROM_CLK);
   setup_spi2(spi_MASTER | SPI_MODE_0 | spi_clk_div_16); // Configuración SPI
   output_high(EEPROM_SELECT);
}


void write_ext_eeprom(int16 direccion, BYTE data)
{

   int address[2];
   // Construye el paquete de direccion (16 bits)
   address[0]= make8(direccion, 1);
   address[1]= 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); // Deshabilita CS
   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,data); // Escribe el dato
   output_high(EEPROM_SELECT); // Deshabilita CS
}


BYTE read_ext_eeprom(int16 direccion)
{
   BYTE data;
   int address[2];

   // Construye el paquete de direccion (16 bits)
   address[0]= make8(direccion, 1);
   address[1]= 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)
   data=spi_xfer(SPIEEPROM,0xFF); // Lee el dato(s)
   output_high(EEPROM_SELECT); // Deshabilita CS
   return(data);
}

//antes de escribir se tiene que llamar esta rutina
void write_ext_eeprom_status()
{
   output_low(EEPROM_SELECT); // Habilita CS
   spi_xfer(SPIEEPROM,0x06); // Habilita escritura
   output_high(EEPROM_SELECT); // Deshabilita CS
   output_low(EEPROM_SELECT); // Habilita CS
   spi_xfer(SPIEEPROM,0x01); //Comando escribir bit de status
   spi_xfer(SPIEEPROM,0x02); //bit de status bp0 y bp1 a 0
   output_high(EEPROM_SELECT); // Deshabilita CS
   printf("ok");
}

BYTE read_ext_eeprom_status()
{
   BYTE data;

   output_low(EEPROM_SELECT); // Habilita CS
   spi_xfer(SPIEEPROM,0x05); // Leer estado de status
   data=spi_xfer(SPIEEPROM,0xFF); // Lee el dato(s)
   output_high(EEPROM_SELECT); // Deshabilita CS
   return(data);
}


 

anything