Go Down

Topic: DataFlash AT45DB041D - SPI (Read 1 time) previous topic - next topic

freddd

Oct 01, 2011, 11:55 pm Last Edit: Oct 02, 2011, 12:02 am by freddd Reason: 1
Hi, All!
I am trying to interface with Atmel 4-megabit DataFlash AT45DB041D over the SPI. Already 2 days I'm trying to write a byte to the first available address, and then read it from there. I tried the dataFlash library, don`t work. What am I doing wrong?

Code: [Select]
#include <SPI.h>

#define miso 12
#define mosi 11
#define sck 13
#define cs 10

void setup()
{
 SPI.begin() ;
 Serial.begin(115200);
 pinMode(mosi, OUTPUT);
 pinMode(miso, INPUT);
 pinMode(sck,OUTPUT);
 pinMode(cs,OUTPUT);
 digitalWrite(cs,HIGH);
 SPI.setBitOrder(MSBFIRST);
 SPI.setDataMode(SPI_MODE0);
 SPI.setClockDivider(SPI_CLOCK_DIV4);
 delay(1000);
}

void loop()
{
 BuWrite();
 delay(100);
 BufferToPage();
 delay(100);
 mainMemoryPageRead();
 delay(100);
}

void BuWrite()
{
 boolean t;
 digitalWrite(cs,LOW);
 SPI.transfer(0x84);
 
 SPI.transfer(0x00);
 SPI.transfer(0x00);
 SPI.transfer(0x00);

 if (t==true) SPI.transfer(0xFF);
 if (t==false) SPI.transfer(0xAA);
 digitalWrite(cs,HIGH);
 t=!t;
}

void BufferToPage()
{
 digitalWrite(cs,LOW);
 
 SPI.transfer(0x83);
 
 SPI.transfer(0x00);
 SPI.transfer(0x00);
 SPI.transfer(0x00);
 
 digitalWrite(cs,HIGH);
}

void mainMemoryPageRead()
{
 int res;
 digitalWrite(cs,LOW);
 
 SPI.transfer(0xD2);
 
 SPI.transfer(0x00);
 SPI.transfer(0x00);
 SPI.transfer(0x00);
 
 SPI.transfer(0xFF);
 SPI.transfer(0xFF);
 SPI.transfer(0xFF);
 SPI.transfer(0xFF);
 res=SPI.transfer(0x00);
 
 digitalWrite(cs,HIGH);
 Serial.println(res);
}

CrossRoads

Get rid of these, SPI.begin takes over the pins:

#define miso 12
#define mosi 11
#define sck 13

  pinMode(mosi, OUTPUT);
  pinMode(miso, INPUT);
  pinMode(sck,OUTPUT);

Move this out of here:

void BuWrite()
{
  boolean t;   <<< this effectively resets to 0 every time you enter this function

See if these make a difference.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

freddd

#2
Oct 02, 2011, 02:35 pm Last Edit: Oct 02, 2011, 02:38 pm by freddd Reason: 1
Thank`s! I corrected the code, but for some reason it still does not work. What is interesting here is that I get  :smiley-eek:
Code: [Select]
0
0
0
0
0
0
0
0
0
0
0
255
255
255
255
255
255
255
255
255
255
255
255
255
0
0
0
0
0
0
0
0
0


Here`s the code:
Code: [Select]

#include <SPI.h>

#define cs 10
boolean t=true;

void setup()
{
 SPI.begin() ;
 Serial.begin(115200);
 pinMode(cs,OUTPUT);
 digitalWrite(cs,HIGH);
 SPI.setBitOrder(MSBFIRST);
 SPI.setDataMode(SPI_MODE3);
 SPI.setClockDivider(SPI_CLOCK_DIV4);
 delay(100);
}

void loop()
{
 BufferWrite();
 delay(100);
 BufferToPage();
 delay(100);
 mainMemoryPageRead();
 delay(100);
}

void BufferWrite()
{
 digitalWrite(cs,LOW);
 SPI.transfer(0x84);
 
 SPI.transfer(0x00);
 SPI.transfer(0x00);
 SPI.transfer(0x00);

 if (t==true) SPI.transfer(0xFF);
 if (t==false) SPI.transfer(0xAA);
 digitalWrite(cs,HIGH);
 t=!t;
}


void BufferToPage()
{
 digitalWrite(cs,LOW);
 
 SPI.transfer(0x83);
 
 SPI.transfer(0x00);
 SPI.transfer(0x00);
 SPI.transfer(0x00);
 
 digitalWrite(cs,HIGH);
}

void mainMemoryPageRead()
{
 int res;
 digitalWrite(cs,LOW);
 
 SPI.transfer(0xD2);
 
 SPI.transfer(0x00);
 SPI.transfer(0x00);
 SPI.transfer(0x00);
 
 SPI.transfer(0xFF);
 SPI.transfer(0xFF);
 SPI.transfer(0xFF);
 SPI.transfer(0xFF);
 res=SPI.transfer(0x00);
 
 digitalWrite(cs,HIGH);
 Serial.println(res);
}


So why is this code looks so ugly. I tried 2 libraries - serialFlash and Chinese one. Then i tried to combine them beacause serialFlash works with AT45DB041B but not AT45DB041D. Chinese seems to be ok but it`s not arduino library. When I joined them I still got the wrong result. What else can I try?

CrossRoads

Post a link to the datasheet.
Need to see what's required for setup.
You have three functions with 0x82, 0x83, 0xD2,
No comments at all so hard to tell what the chip wants from all that.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.


Go Up