www.pudn.com > iccavr_lib.rar > at25.c, change:2002-02-05,size:2541b


/* 
**	Purpose:    Support for AT25080, AT25160, AT25320 & AT25640 SPI EEPROM from Atmel 
**              This LIB needs the "SPI.C" & "SPI.H" files. 
** 
**  Version:    1.0.0, 24:th of August 1999 
** 
**  Author:     Lars Wictorsson 
**              LAWICEL / SWEDEN 
**              http://www.lawicel.com   lars@lawicel.com 
** 
**  History:    1999-08-24  Created 
*/ 
 
#include <avr.h> 
#include "spi.h" 
#include "at25.h" 
 
void AT25Init(void) 
{ 
    DDRB |= 0x10;           // PB4 as output "Chip Select for EEPROM, active low" 
    AT25Disable;            // Turn off /CS for SPI EEPROM 
} 
 
void AT25WriteEnable(void) 
{ 
    AT25Enable;             // CS LOW 
    SpiWriteByte(0x06);     // Write command 
    AT25Disable;            // CS HIGH 
} 
 
void AT25WriteDisable(void) 
{ 
    AT25Enable;             // CS LOW 
    SpiWriteByte(0x04);     // Write command 
    AT25Disable;            // CS HIGH 
} 
 
unsigned char AT25ReadStatusReg(void) 
{ 
    unsigned char byte; 
    AT25Enable;             // CS LOW 
    SpiWriteByte(0x05);     // Write command 
    byte = SpiReadByte();   // Read back a byte from status register 
    AT25Disable;            // CS HIGH 
    return byte; 
} 
 
void AT25WriteStatusReg(unsigned char byte) 
{ 
    AT25Enable;             // CS LOW 
    SpiWriteByte(0x01);     // Write command 
    SpiWriteByte(byte);     // Write byte to status register 
    AT25Disable;            // CS HIGH 
} 
 
unsigned char AT25Read(unsigned int address) 
{ 
    unsigned char byte; 
    AT25Enable;             // CS LOW 
    SpiWriteByte(0x03);     // Write command 
    SpiWriteByte((unsigned char) ((address >> 8) & 0xFF));  // Write High byte of address 
    SpiWriteByte((unsigned char) (address & 0xFF));         // Write Low byte of address 
    byte = SpiReadByte();   // Read back a byte 
    AT25Disable;            // CS HIGH 
    return byte; 
} 
 
void AT25Write(unsigned int address, unsigned char byte) 
{ 
    AT25WriteEnable();      // Each write must set the Write Enable Flag 
    AT25Enable;             // CS LOW 
    SpiWriteByte(0x02);     // Write command 
    SpiWriteByte((unsigned char) ((address >> 8) & 0xFF));  // Write High byte of address 
    SpiWriteByte((unsigned char) (address & 0xFF));         // Write Low byte of address 
    SpiWriteByte(byte);     // Write byte to specified address 
    AT25Disable;            // CS HIGH 
    while (AT25ReadStatusReg() & WRITE_CYCLE_IN_PROGRESS);  //Wait until the byte is programmed by reading back status byte 
}