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

📄 spi_eeprom.c

📁 EEPROM driver for SPI EEPROMs. Bitbanged on MSP430 processor. Written in Quadravox compiler.
💻 C
字号:
#include <MSP430Fx.h>
#include "dispport.h"
#include "spi_eeprom.h"

unsigned char EE_JustWritten=0; // Set to TRUE to force WaitRdy check before read.

void EE_Select(void) { P5OUT&=~EE_CS; }
void EE_DeSelect(void) { P5OUT|=EE_CS; }

unsigned char EE_RW(unsigned char txbyte) {
// Writes one byte to bitbang SPI, msb first, while receiving a byte
// EEprom is assumed to be faster than the processor in bitbang mode
  unsigned char rxbyte=0;
  char i; 
  for (i=0;i<=7;i++) {
    if (txbyte & 0x80) P5OUT|=EE_SI; else P5OUT&=~EE_SI; // set data
    P5OUT|=EE_CK;   // Clock data in
    rxbyte<<=1;     // Rotate a nice hole for the data
    if (P5IN & EE_SO) rxbyte++;
    P5OUT&=~EE_CK;  // And get a bit out
    txbyte<<=1;
  }  
  return rxbyte;
} 
  
void EE_WREN(void) { EE_Select(); EE_RW(0x06); EE_DeSelect(); } // WREN command  
void EE_WRDI(void) { EE_Select(); EE_RW(0x04); EE_DeSelect(); } // WRDI command  

unsigned char EE_RDSR(void) { // Read SR
// SR is xxxx BP1 BP0 WEN -RDY
unsigned int res; 
  EE_Select(); EE_RW(0x05); res=EE_RW(0); EE_DeSelect(); 
  return res;
}

void EE_WRSR(unsigned char status) { // Read SR
  EE_Select(); EE_RW(0x01); EE_RW(status); EE_DeSelect(); 
}

void EE_WaitRdy(void) {
  while (EE_RDSR() & 1) {}; // Loop until -RDY drops to 0
  EE_JustWritten=0; 
}

unsigned char EE_WR(unsigned int adr, unsigned char data) {
// Write a byte to the EEPROM. Returns status register on completion.
  if (EE_JustWritten) EE_WaitRdy(); // See if we were finished with the last one 
  EE_WREN(); // Enable Write 
  EE_Select();  EE_RW(0x02); // Write command
  EE_RW(adr >> 8); EE_RW(adr & 0xFF); // send address
  EE_RW(data);
  EE_DeSelect();
  EE_JustWritten=1; 
  return EE_RDSR();
}

unsigned char EE_RD(unsigned int adr) {
// Read a byte from the EEPROM
unsigned int res; 
  if (EE_JustWritten) EE_WaitRdy();  
  EE_Select();  EE_RW(0x03); // Read command
  EE_RW(adr >> 8); EE_RW(adr & 0xFF); // send address
  res=EE_RW(0);
  EE_DeSelect(); 
  return res; 
}

unsigned char EE_Write(unsigned int adr, unsigned char *data, unsigned char datalen) {
// Writes up to 64 bytes to the EEPROM. Returns status register on completion.
  unsigned char i; 
  if (EE_JustWritten) EE_WaitRdy(); // See if we were finished with the last one 
  EE_WREN(); // Enable Write 
  EE_Select();  EE_RW(0x02); // Write command
  EE_RW(adr >> 8); EE_RW(adr & 0xFF); // send address
  for (i=0;i<datalen;i++) EE_RW(*data++);
  EE_DeSelect();
  EE_JustWritten=1; 
  return EE_RDSR();
}

void EE_Read(unsigned int adr, unsigned char *data, unsigned int datalen) {
// Reads any number of bytes from the EEPROM
unsigned int i; 
  if (EE_JustWritten) EE_WaitRdy();  
  EE_Select();  EE_RW(0x03); // Read command
  EE_RW(adr >> 8); EE_RW(adr & 0xFF); // send address
  for (i=0;i<datalen;i++) *data++=EE_RW(0);
  EE_DeSelect(); 
}

⌨️ 快捷键说明

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