uas_arduino

SRAM Working

Feb 15th, 2013
53
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int dataPins[] = {2,3,4,5};
  2. int NUMBER_OF_DATA_PINS = 4;
  3. int addrPins[] = {7,8,9,10};
  4. int NUMBER_OF_ADDR_PINS = 4;
  5. int oe = 11;
  6. int we = 12;
  7.  
  8. void setup(){
  9.   for(int a = 0; a < NUMBER_OF_ADDR_PINS; a++){
  10.     pinMode(addrPins[a], OUTPUT);
  11.   }
  12.  
  13.   pinMode(oe,OUTPUT);
  14.   pinMode(we,OUTPUT);
  15. }
  16.  
  17. void setAddr(byte addr){
  18.   for(int a = 0; a < NUMBER_OF_ADDR_PINS; a++){
  19.     if((addr >> a) & 0x01){
  20.       digitalWrite(addrPins[a],HIGH);
  21.     }else{
  22.       digitalWrite(addrPins[a],LOW);
  23.     }
  24.   }
  25. }
  26.  
  27. void setData(byte data){
  28.   for(int a = 0; a < NUMBER_OF_DATA_PINS; a++){
  29.     pinMode(dataPins[a],OUTPUT);
  30.     if((data >> a) & 0x01){
  31.       digitalWrite(dataPins[a],HIGH);
  32.     }else{
  33.       digitalWrite(dataPins[a],LOW);
  34.     }
  35.   }
  36. }
  37.  
  38. void writeData(byte addr, byte data){
  39.   setAddr(addr);
  40.   digitalWrite(we,LOW);
  41.   setData(data);
  42.   digitalWrite(we,HIGH);
  43. }
  44.  
  45. byte readData(byte addr){
  46.   byte data = 0x00;
  47.   setAddr(addr);
  48.  
  49.   digitalWrite(oe,LOW);
  50.   for(byte a = 0; a < NUMBER_OF_DATA_PINS; a++){
  51.     pinMode(dataPins[a],INPUT);
  52.     if(digitalRead(dataPins[a])){
  53.       data |= (0x01 << a);
  54.     }
  55.   }
  56.   digitalWrite(oe,HIGH);
  57.   return data;
  58. }
  59.  
  60. void loop(){
  61.   for(int a = 0; a < 16; a++){
  62.     writeData(a,a);
  63.   }
  64.  
  65.   for(int a = 0; a < 16; a++){
  66.     Serial.print("Looking at address: ");
  67.     Serial.print(a,DEC);
  68.     Serial.print(" Got back: ");
  69.     Serial.println(readData(a),DEC);
  70.     delay(1000);
  71.   }
  72. }
RAW Paste Data