Last active
July 13, 2019 13:09
-
-
Save surinoel/de5ff4b866a1311805e8a6487785cc15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* eeprom.c | |
* | |
* Created: 2019-07-13 오후 8:59:22 | |
* Author: yeong | |
*/ | |
#include "eeprom.h" | |
#define SPI_SS PB0 | |
#define SPI_MOSI PB2 | |
#define SPI_MISO PB3 | |
#define SPI_SCK PB1 | |
#define EEPROM_Select() PORTB &= ~(1<<SPI_SS) | |
#define EEPROM_DeSelect() PORTB |= (1<<SPI_SS) | |
#define EERPOM_WRITE 0b00000010 | |
#define EEPROM_WREN 0b00000110 | |
#define EEPROM_RDSR 0b00000101 | |
uint8_t EEPROM_readByte(uint8_t address) | |
{ | |
EEPROM_Select(); | |
EEPROM_changeByte(EEPROM_READ); | |
EEPROM_sendAddress(address); | |
EEPROM_changeByte(0); | |
EEPROM_DeSelect(); | |
return SPDR; | |
} | |
void EEPROM_changeByte(uint8_t byte) | |
{ | |
SPDR = byte; | |
while(!(SPSR & (1<<SPIF))); | |
} | |
void EEPROM_sendAddress(uint8_t address) | |
{ | |
EEPROM_changeByte(address); | |
} | |
void EEPROM_writeEnable(void) | |
{ | |
EEPROM_Select(); | |
EEPROM_changeByte(EEPROM_WREN); | |
EEPROM_DeSelect(); | |
} | |
void EEPROM_writeByte(uint8_t address, uint8_t data) | |
{ | |
EEPROM_writeEnable(); | |
EEPROM_Select(); | |
EEPROM_changeByte(EERPOM_WRITE); | |
EEPROM_sendAddress(address); | |
EEPROM_changeByte(data); | |
EEPROM_DeSelect(); | |
while(EEPROM_readStatus() & _BV(0)); | |
} | |
uint8_t EEPROM_readStatus(void) | |
{ | |
EEPROM_Select(); | |
EEPROM_changeByte(EEPROM_RDSR); | |
EEPROM_changeByte(0); | |
EEPROM_DeSelect(); | |
return SPDR; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment