Pages: [1]   Go Down
Author Topic: DataFlash AT45DB041D - SPI  (Read 1323 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}
« Last Edit: October 01, 2011, 05:02:42 pm by freddd » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27372
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
#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?
« Last Edit: October 02, 2011, 07:38:13 am by freddd » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27372
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://atmel.com/dyn/resources/prod_documents/doc3595.pdf
Link to the AT45DB041D Datasheet
Logged

Pages: [1]   Go Up
Jump to: