I am having some issues with integrating my EEPROM and Digital Potentiometer together.
So, I have saved some values in my EEPROM and I have to extract it to control the position of my digital potentiometer. Here's my code:
#include <Wire.h>
#include <SPI.h>
#define disk1 0x50 //Address of 24LC256 eeprom chip
const int slaveSelectPin = 10;
int eeprom_data[6];
int temp=0;
void setup(void)
{
Serial.begin(9600);
Wire.begin();
unsigned int address = 0;
Serial.print("Contents of EEPROM: ");
//EEPROM contains 50,100,150,200,25,130
for(int i=0; i<6; i++)
{
eeprom_data[i]=readEEPROM(disk1,i);
Serial.print(eeprom_data[i]);
Serial.print(" ");
}
for (int j = 0; j < 6; j++)
{
digitalPotWrite(j, eeprom_data[j]);
Serial.print(j);
}
}
void loop(){}
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data )
{
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(data);
Wire.endTransmission();
delay(5);
}
int readEEPROM(int deviceaddress, unsigned int eeaddress )
{
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}
void digitalPotWrite(int address, int value)
{
// take the SS pin low to select the chip:
digitalWrite(slaveSelectPin, LOW);
delay(100);
// send in the address and value via SPI:
SPI.transfer(address);
SPI.transfer(value);
delay(100);
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin, HIGH);
}
I have successfully extracted the data from my EEPROM but when I passed the array (eeprom_data) to the digitalPotWrite function, the code just froze there and it does not run the for (int j = 0; j < 6; j++) loop. (Edit: this loop only runs once. I need it to run 6 times)
I am using a 24LC256 EEPROM and an AD5206 digital potentiometer.
This is my updated code, without the typo. It does not have any compiler errors. When I uploaded it to the board, the loop with the digitalPotWrite function doesn't run.
#include <Wire.h>
#include <SPI.h>
#define disk1 0x50 //Address of 24LC256 eeprom chip
const int slaveSelectPin = 10;
int eeprom_data[6];
int temp=0;
void setup(void)
{
Serial.begin(9600);
Wire.begin();
unsigned int address = 0;
Serial.print("Contents of EEPROM: ");
//EEPROM contains 50,100,150,200,25,130
for(int i=0; i<6; i++)
{
eeprom_data[i]=readEEPROM(disk1,i);
Serial.print(eeprom_data[i]);
Serial.print(" ");
}
for (int j = 0; j < 6; j++)
{
digitalPotWrite(j, eeprom_data[j]);
Serial.print(j);
}
}
void loop(){}
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data )
{
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(data);
Wire.endTransmission();
delay(5);
}
int readEEPROM(int deviceaddress, unsigned int eeaddress )
{
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}
void digitalPotWrite(int address, int value)
{
// take the SS pin low to select the chip:
digitalWrite(slaveSelectPin, LOW);
delay(100);
// send in the address and value via SPI:
SPI.transfer(address);
SPI.transfer(value);
delay(100);
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin, HIGH);
}
It cycles through different channels and changes the position. I am doing the same here. But it doesn't run the mentioned loop. I can't figure out why.
OK so it obviously never comes back from the first call... like I said... use Serial.println... LOTS OF THEM !!!! You are trying to figure out which bit is blocking...
#include <Wire.h>
#include <SPI.h>
#define disk1 0x50 //Address of 24LC256 eeprom chip
const int slaveSelectPin = 10;
int eeprom_data[6];
int temp=0;
void setup(void)
{
Serial.begin(9600);
Wire.begin();
unsigned int address = 0;
Serial.print("Contents of EEPROM: ");
//EEPROM contains 50,100,150,200,25,130
for(int i=0; i<6; i++)
{
eeprom_data[i]=readEEPROM(disk1,i);
Serial.print(eeprom_data[i]);
Serial.print(" ");
}
Serial.print("\n");
Serial.print("Iteration ");
for (int j = 0; j < 6; j++)
{
Serial.print("\n");
Serial.print(j);
digitalPotWrite(j, eeprom_data[j]);
}
}
void loop(){}
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data )
{
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(data);
Wire.endTransmission();
delay(5);
}
int readEEPROM(int deviceaddress, unsigned int eeaddress )
{
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}
void digitalPotWrite(int address, int value)
{
// take the SS pin low to select the chip:
Serial.println("Here 0");
digitalWrite(slaveSelectPin, LOW);
delay(100);
Serial.println("Here 1");
// send in the address and value via SPI:
SPI.transfer(address);
Serial.println("Here 2");
SPI.transfer(value);
Serial.println("Here 3");
delay(100);
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin, HIGH);
Serial.println("Here 4");
}
@red_car thanks for your suggestion. I have added in more stuff similar to what you have said but I still can't solve the problem.
I found out where it stopped (refer to comment in code). I have used your code for easy reference.
void digitalPotWrite(int address, int value)
{
// take the SS pin low to select the chip:
Serial.println("Here 0");
digitalWrite(slaveSelectPin, LOW);
delay(100);
Serial.println("Here 1");
// send in the address and value via SPI:
SPI.transfer(address); //Stops here
Serial.println("Here 2");
SPI.transfer(value);
Serial.println("Here 3");
delay(100);
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin, HIGH);
Serial.println("Here 4");
}
Here's the corresponding output of the serial monitor: