Topics

► Games

► Sound & Music

► Watches & Clocks

► GPS

► Power Supplies

► Computers

► Graphics

► Thermometers

► Wearables

► Test Equipment

► Tutorials

► Libraries

► PCB-Based Projects

By processor

AVR ATtiny

► ATtiny10

► ATtiny2313

► ATtiny84

► ATtiny841

► ATtiny85

► ATtiny861

► ATtiny88

AVR ATmega

► ATmega328

► ATmega1284

AVR 0-series and 1-series

► ATmega4809

► ATtiny1604

► ATtiny1614

► ATtiny3216

► ATtiny3227

► ATtiny402

► ATtiny404

► ATtiny414

► ATtiny814

AVR DA/DB-series

► AVR128DA28

► AVR128DA32

► AVR128DA48

► AVR128DB28

ARM

► ATSAMD21

► RP2040

► RA4M1

About me

  • About me
  • Twitter
  • Mastodon

Feeds

RSS feed

Simple DataFlash Program

/* Simple DataFlash - see http://www.technoblogy.com/show?2JMU David Johnson-Davies - www.technoblogy.com - 17th July 2019 CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ */ // Winbond DataFlash commands #define PAGEPROG 0x02 #define READSTATUS 0x05 #define READDATA 0x03 #define WRITEENABLE 0x06 #define CHIPERASE 0x60 #define READID 0x90 // Arduino Uno pins used for dataflash const int sck = 13, miso = 12, mosi = 11, cs = 10, power = 9; class DF { public: boolean Setup(); void BeginRead(unsigned long addr); void BeginWrite(void); uint8_t ReadByte(void); void WriteByte(uint8_t data); void EndRead(void); void EndWrite(void); private: unsigned long addr; uint8_t Read(void); void Write(uint8_t); void Busy(void); void WriteEnable(void); }; boolean DF::Setup () { uint8_t manID, devID; pinMode(power, OUTPUT); digitalWrite(power, HIGH); digitalWrite(cs, HIGH); pinMode(cs, OUTPUT); pinMode(sck, OUTPUT); pinMode(mosi, OUTPUT); pinMode(miso, INPUT); digitalWrite(sck, LOW); digitalWrite(mosi, HIGH); delay(1); digitalWrite(cs, LOW); delay(100); Write(READID); Write(0);Write(0);Write(0); manID = Read(); devID = Read(); digitalWrite(cs, HIGH); return (devID == 0x15); // Found correct device } void DF::Write (uint8_t data) { shiftOut(mosi, sck, MSBFIRST, data); } void DF::Busy () { digitalWrite(cs, 0); Write(READSTATUS); while (Read() & 1 != 0); digitalWrite(cs, 1); } void DF::WriteEnable () { digitalWrite(cs, 0); Write(WRITEENABLE); digitalWrite(cs, 1); } void DF::BeginRead (unsigned long start) { addr = start; Busy(); digitalWrite(cs, 0); Write(READDATA); Write(addr>>16); Write(addr>>8); Write(addr); } uint8_t DF::Read () { return shiftIn(miso, sck, MSBFIRST); } void DF::EndRead(void) { digitalWrite(cs, 1); } void DF::BeginWrite () { addr = 0; Busy(); // Erase DataFlash WriteEnable(); digitalWrite(cs, 0); Write(CHIPERASE); digitalWrite(cs, 1); Busy(); } uint8_t DF::ReadByte () { return Read(); } void DF::WriteByte (uint8_t data) { // New page if ((addr & 0xFF) == 0) { digitalWrite(cs, 1); Busy(); WriteEnable(); digitalWrite(cs, 0); Write(PAGEPROG); Write(addr>>16); Write(addr>>8); Write(0); } Write(data); addr++; } void DF::EndWrite (void) { digitalWrite(cs, 1); } DF DataFlash; // Test program void setup() { Serial.begin(9600); while (!Serial); if (!DataFlash.Setup()) { Serial.println("DataFlash not found"); for(;;); } } void loop() { unsigned long Test = 400000; Serial.println("Erase"); DataFlash.BeginWrite(); randomSeed(12345); Serial.println("Write"); for (unsigned long i=0; i
blog comments powered by Disqus