Go Down

Topic: Library for RAM chip (Read 4773 times) previous topic - next topic

farlepet

this is what i have, what should i change?
Code: [Select]
int AM9111::read(unsigned int address)
{
  int rv = 0;
  _read() = rv;
  _setAddress(address);
  return rv;
}

Code: [Select]
int AM9111::_read()
{
  int rv = 0;
  digitalWrite(_odp,0);
  digitalWrite(_wep,1);
  bitWrite(rv,0,digitalRead(_IO[0]));
  bitWrite(rv,1,digitalRead(_IO[1]));
  bitWrite(rv,2,digitalRead(_IO[2]));
  bitWrite(rv,3,digitalRead(_IO[3]));
  _read() = rv;
}



AWOL

#31
Apr 23, 2011, 12:20 am Last Edit: Apr 23, 2011, 12:26 am by AWOL Reason: 1
Code: [Select]
_read() = rv;

I thought we'd already decided that this was a no-no?

Code: [Select]
int AM9111::read(unsigned int address)
{
  int rv = 0;
  _read() = rv;
  _setAddress(address);
  return rv;
}

Is that really the correct order of events?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Nick Gammon



Code: [Select]
int AM9111::_read()
{
 digitalWrite(_odp,0);
 digitalWrite(_wep,1);
 int rv = 0;
 bitWrite(_rbits,0,digitalRead(_IO[0]));
 bitWrite(_rbits,1,digitalRead(_IO[1]));
 bitWrite(_rbits,2,digitalRead(_IO[2]));
 bitWrite(_rbits,3,digitalRead(_IO[3]));
 _read() = _rbits;
 return rv;
}




Someone's been doing too much Visual Basic. ;)

Instead of:

Code: [Select]
 _read() = _rbits;
 return rv;


You want:

Code: [Select]
 return _rbits;

Although I'm not sure what rv does. Did you put it there to make compiler errors go away?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon



Code: [Select]
int AM9111::_read()
{
 digitalWrite(_odp,0);
 digitalWrite(_wep,1);
 int rv = 0;
 bitWrite(_rbits,0,digitalRead(_IO[0]));
 bitWrite(_rbits,1,digitalRead(_IO[1]));
 bitWrite(_rbits,2,digitalRead(_IO[2]));
 bitWrite(_rbits,3,digitalRead(_IO[3]));
 _read() = _rbits;
 return rv;
}




I still don't totally get this. Are you trying to change _rbits? In which case why return anything from the function? In that case you want:

Code: [Select]
void AM9111::_read()
{
 digitalWrite(_odp,0);
 digitalWrite(_wep,1);
 _rbits = 0;
 bitWrite(_rbits,0,digitalRead(_IO[0]));
 bitWrite(_rbits,1,digitalRead(_IO[1]));
 bitWrite(_rbits,2,digitalRead(_IO[2]));
 bitWrite(_rbits,3,digitalRead(_IO[3]));
}


But if you want to return stuff, and not affect things such as _rbits (have side-effects) then you actually want:

Code: [Select]
int AM9111::_read()
{
 digitalWrite(_odp,0);
 digitalWrite(_wep,1);
 int rv = 0;
 bitWrite(rv,0,digitalRead(_IO[0]));
 bitWrite(rv,1,digitalRead(_IO[1]));
 bitWrite(rv,2,digitalRead(_IO[2]));
 bitWrite(rv,3,digitalRead(_IO[3]));
 return rv;
}
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

farlepet


farlepet

Yes! It finally compiled! I will upload it tomorrow morning and try it.

Thanks for all your help :)

And what do you mean by "Someone's been doing too much Visual Basic"?

Nick Gammon

Well, have you done Visual Basic?

In VB, a function returns its results by assigning to the function name. For example:

Code: [Select]
function foo (a, b)
  foo = a + b   ' return result of adding a to b
end function


So when I saw you write:

Code: [Select]
_read() = _rbits;


I thought you might have thought this was how you returned the function value.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

farlepet

Well i have done a little visual basic, but not much, i did more visual c++.

and i have made an arduino sketch thad did, for example:
Code: [Select]
boolean yn(int pin)
{
  yn = digitalRead(pin);
}

Nick Gammon

Not one that compiled, right?

Code: [Select]
sketch_apr23c.cpp: In function 'boolean yn(int)':
sketch_apr23c:2: error: assignment of function 'boolean yn(int)'
sketch_apr23c:2: error: cannot convert 'int' to 'boolean ()(int)' in assignment
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

farlepet

hmm, well i know it was something like that, mabey i used if statements or something, i don't know. it was a while ago. Well ill test the lib tomorrow...

farlepet

Now the _read() always returns 0

here is the code:
Code: [Select]


#include "AM9111.h"
#include "WProgram.h"

/////////////////////////////////////////
//
// PUBLIC
//
AM9111::AM9111(uint8_t outputDisablePin, uint8_t writeEnablePin)
{
  _odp = outputDisablePin;
  _wep = writeEnablePin;
}

int AM9111::setIO(uint8_t IO0, uint8_t IO1, uint8_t IO2, uint8_t IO3)
{
  _IO[0] = IO0;
  _IO[1] = IO1;
  _IO[2] = IO2;
  _IO[3] = IO3;
}
 

int AM9111::read(unsigned int address)
{
  int rv = 0;
  _setAddress(address);
  return rv;
}

int AM9111::write(unsigned int address, uint8_t value)
{
  int rv = 0;
  _setAddress(address);
  rv = _write(value);
  return rv;
}

/////////////////////////////////////////
//
// PRIVATE
//
void AM9111::_setAddress(unsigned int address)
{
for(int p=0; p<8; p++)
{
  pinMode(p, OUTPUT);
  digitalWrite(p, bitRead(address, p));
}
}


int AM9111::_read()
{
  digitalWrite(_odp,0);
  digitalWrite(_wep,1);
  int rv = 0;
  bitWrite(rv,0,digitalRead(_IO[0]));
  bitWrite(rv,1,digitalRead(_IO[1]));
  bitWrite(rv,2,digitalRead(_IO[2]));
  bitWrite(rv,3,digitalRead(_IO[3]));
  return rv;
}


int AM9111::_write(uint8_t value)
{
  digitalWrite(_odp,1);
  digitalWrite(_wep,0);
  int  rv = 0;
  digitalWrite(_IO[0],bitRead(0,value));
  digitalWrite(_IO[1],bitRead(1,value));
  digitalWrite(_IO[2],bitRead(2,value));
  digitalWrite(_IO[3],bitRead(3,value));
  return rv;
}


farlepet

is it because of the first read statement rv = 0?

AWOL

#42
Apr 23, 2011, 09:32 pm Last Edit: Apr 23, 2011, 09:34 pm by AWOL Reason: 1
Yes.
That, and the fact that you don't call _read from read.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

farlepet

how do you call _read from read?

AWOL

In your own reply #30, you very nearly did.
You just got it the wrong way round.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up