B4R Question MCP4XXX Wrapped Library ?

KiloBravo

Active Member
Licensed User
I am trying to get my ESP8266 to Set/Read the wipers on BOTH Pots0 and 1 of the MCP4XXX using the wrapped Library here https://www.b4x.com/android/forum/threads/mcp4xxx.101935/

The Header file in mcp4xxx has this line defined in it in the MCP4XXX Class.
MCP4XXX(byte select_pin = SS, Pot pot = pot_0, Resolution resolution = res_7bit, WiperConfiguration config = potentiometer);
That line is not in the rMCP4xxx h file.
In my code I have this line

mcp.Initialize(dp.D8,mcp.POT_0,mcp.RES8BIT,mcp.POTENTIOMETER)

I can read pot 0 OR pot 1 if I set it in the mcp4xxx.h file located in my B4R lib. But when I change it in my B4R program it has no effect, and I can only read one pot. The one I hard coded in that line in the MCP4XXX lib. I tried a bunch of different things and still can't figure out how to switch pots in my code so I can read and set both pots on mcp4251 chip. As you can guess I am pretty weak on the c++ side and wrapping libraries. Any help would be appreciated. Thanks !
 

Siam

Active Member
Licensed User
Longtime User
hello,

the MCP4XXX.cpp and MCP4XXX.h files are the Library on this files you don't need to change anything.
The wrapper is rMCP4XXX.h and MCP4XXX.cpp
did you try
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Public mcp0 As MCP4xxx 'testing for MCP4131
    Public mcp1 As MCP4xxx 'testing for MCP4131
    Public const total=9800 As UInt 'measured from term A to B
    Public const wiperohms=110 As UInt 'measured from term A to wiper when wiper is at 0
    Public ohminc=0 As UInt
End Sub

Private Sub AppStart
    Serial1.Initialize(9600)
    Log("AppStart")

mcp0.Initialize(dp.D8,mcp0.POT_0,mcp0.RES8BIT,mcp0.POTENTIOMETER) ' for pot 0
mcp1.Initialize(dp.D8,mcp1.POT_1,mcp1.RES8BIT,mcp1.POTENTIOMETER) ' for pot 1

...
...
..
end sub
 
Upvote 0

kolbe

Active Member
Licensed User
Longtime User
Siam is right you will need an instance for each potentiometer. Once initialized you can't change the which potentiometer or resolution, etc. Make sure as well that the terminals are connected.
 
Upvote 0

KiloBravo

Active Member
Licensed User
I tried to Init with the above I.E mcp0.Initialize & mcp1.Initialize. It still did not work.

But, after another 8 hours of, trying other libs, digging thru stuff, and then finally comparing it to Erel's example of wrapping a Library. I only had to change one line in rMCP4xxx.cpp and it works !!!
If I Initialize with mcp0.Initialize & mcp1.Initialize.
Old
namespace B4R {
void B4RMCPXXX::Initialize(Byte csPin,Pot pot,Resolution res,WiperConfiguration config) {
mcp = new (sizeMCP) MCP4XXX(csPin);
}
New
namespace B4R {
void B4RMCPXXX::Initialize(Byte csPin,Pot pot,Resolution res,WiperConfiguration config) {
mcp = new (sizeMCP) MCP4XXX(csPin, pot, res, config);
}

Thanks to everyone who offered suggestions, you got me on the right track to find the error and fix it! I ALMOST gave up. I have 5 chips (MCP4251 2 Digital Pots Each) I only needed 3 digital pots.
But, I like a challenge too !!!
 
Upvote 0

kolbe

Active Member
Licensed User
Longtime User
Looked through the code again and you are right. This bit...

B4X:
MCP4XXX(byte select_pin = SS, Pot pot = pot_0, Resolution resolution = res_7bit, WiperConfiguration config = potentiometer);

from mcp4xxx.h sets defaults if nothing is passed which is the behavior you pointed out above. Since I only used the mcp4131, I never saw this and it wouldn't accept pot_1 etc. I'll test this on a mcp4261 and make an update to the original library post. Thanks for posting what you found.
 
Upvote 0
Top