⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 at25.c

📁 ICCAVR中所有的库函数源码
💻 C
字号:
/*
**	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
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -