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

📄 spieeprom.c

📁 avr cpu 库源代码 对avr单片机编程很有帮助
💻 C
字号:
/*! \file spieeprom.c \brief Interface for standard SPI EEPROM memories. */
//*****************************************************************************
//
// File Name	: 'spieeprom.c'
// Title		: Interface for standard SPI EEPROM memories
// Author		: Pascal Stang - Copyright (C) 2004
// Created		: 2004.10.07
// Revised		: 2004.10.07
// Version		: 0.1
// Target MCU	: Atmel AVR series
// Editor Tabs	: 4
//
// This code is distributed under the GNU Public License
//		which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************

#include <avr/io.h>
#include <avr/interrupt.h>

#include "spi.h"
#include "spieeprom.h"

// functions
void spieepromInit(void)
{
	// although there is no code here
	// don't forget to initialize the SPI interface itself
//	sbi(DDRB, 0);
}

u08 spieepromReadByte(u32 memAddr)
{
	u08 data;
//	cbi(PORTB,0);
	// send command
	spiTransferByte(SPIEEPROM_CMD_READ);
	// send address
	spiTransferByte(memAddr>>8);
	spiTransferByte(memAddr&0x00FF);
	// read contents of memory address
	data = spiTransferByte(0xFF);
	// return data
	return data;
//	sbi(PORTB,0);
}

void spieepromWriteByte(u32 memAddr, u08 data)
{
	// wait for any previous write to complete
	while(spieepromReadStatus() & SPIEEPROM_STATUS_WIP);

//	cbi(PORTB,0);
	// send command
	spiTransferByte(SPIEEPROM_CMD_WRITE);
	// send address
	spiTransferByte(memAddr>>8);
	spiTransferByte(memAddr&0x00FF);
	// send data to be written
	spiTransferByte(data);
//	sbi(PORTB,0);
}

void spieepromWriteEnable(void)
{
//	cbi(PORTB,0);
	// send command
	spiTransferByte(SPIEEPROM_CMD_WREN);
//	sbi(PORTB,0);
}

void spieepromWriteDisable(void)
{
//	cbi(PORTB,0);
	// send command
	spiTransferByte(SPIEEPROM_CMD_WRDI);
//	sbi(PORTB,0);
}

u08 spieepromReadStatus(void)
{
	u08 status;
//	cbi(PORTB,0);
	// send command
	spiTransferByte(SPIEEPROM_CMD_RDSR);
	// get status register value
	status = spiTransferByte(0xFF);
//	sbi(PORTB,0);
	return status;
}

⌨️ 快捷键说明

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