AT89C51RD2 + MEMORIA EEPROM SPI

Software e Hardware para linha x51

Moderadores: 51, guest2003, Renie, gpenga

AT89C51RD2 + MEMORIA EEPROM SPI

Mensagempor mauricio_lula » 18 Out 2009 20:19

Boa Noite Srs,

Estou com problema de implementar o uso da porta SPI do micro com a memoria ,o programa simplesmente fica travado !
Coloquei algumas escritas no lcd como Debug ,foi constatado que o programa trava logo no write enable da memoria,segue abaixo o codigo.

sbit spi_cs = P1^1;

#define EEPROM_WREN_OPCODE (0x06) // write enable
#define EEPROM_WRDI_OPCODE (0x04) // write disable
#define EEPROM_RDSR_OPCODE (0x05) // read status register
#define EEPROM_WRSR_OPCODE (0x01) // write status register
#define EEPROM_READ_OPCODE(a) ((((a) & 0x0100) >> 0x05) | 0x03)
#define EEPROM_WRITE_OPCODE(a) ((((a) & 0x0100) >> 0x05) | 0x02)

#define EEPROM_ADDRESS_LSB(a) ((a) & 0x00FF)

#define EEPROM_STAT_BUSY 0x01
#define EEPROM_STAT_WREN 0x02


unsigned char read_status (void)
{
unsigned char stat;

spi_cs = 0;

SPDAT = EEPROM_RDSR_OPCODE;
while ((SPSTA & 0x80) == 0);

SPDAT = 0xFF;
while ((SPSTA & 0x80) == 0);

stat = SPDAT;

spi_cs = 1;

return (stat);
}



void write_enable ( bit flag)
{
spi_cs = 0;

if(flag==0)
SPDAT= EEPROM_WRDI_OPCODE;
else
SPDAT= EEPROM_WREN_OPCODE;

gotoxy1(1,2);
wrlcd1('B');

while ((SPSTA & 0x80) == 0); <= nâo passa daqui !!


gotoxy1(1,2);
wrlcd1('C');

spi_cs = 1;
}


void read_spi (unsigned int address)
{
spi_cs = 0;

SPDAT = EEPROM_READ_OPCODE(address);
while ((SPSTA & 0x80) == 0);

SPDAT = EEPROM_ADDRESS_LSB(address);
while ((SPSTA & 0x80) == 0);

SPDAT=0xFF;
while ((SPSTA & 0x80) == 0);
spidata = SPDAT;

spi_cs = 1;
}


void write_spi (unsigned int address,unsigned char dado)
{
gotoxy1(1,2);
wrlcd1('A');

write_enable (1);

gotoxy1(1,2);
wrlcd1('D');

while (read_status () & EEPROM_STAT_BUSY);

gotoxy1(1,2);
wrlcd1('E');

spi_cs = 0;

SPDAT = EEPROM_WRITE_OPCODE(address);
while ((SPSTA & 0x80) == 0);

gotoxy1(1,2);
wrlcd1('F');

SPDAT = EEPROM_ADDRESS_LSB(address);
while ((SPSTA & 0x80) == 0);

gotoxy1(1,2);
wrlcd1('G');

SPDAT = dado;
while ((SPSTA & 0x80) == 0);

spi_cs = 1;

write_enable (0);
}

main;

SPCON=0x53;

write_spi(0x00,'A');


Agradeço pela ajuda !!!
mauricio_lula
Bit
 
Mensagens: 13
Registrado em: 26 Fev 2008 21:33

Voltar para 8051

Quem está online

Usuários navegando neste fórum: Nenhum usuário registrado e 1 visitante

x