Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 10 months ago.
MCP4151 hookup
I am trying to get an MCP4151 SPI digital resistor working with the mbed 1768.
Here is my code thus far:
Expression
#include "mbed.h" SPI spi(p5, NC, p7); // mosi, miso, sclk DigitalOut cs(p8); int main() { cs = 1; spi.format(8,0); spi.frequency(1000000); for (int level = 0; level < 1; level++) { cs = 0; spi.write(0); spi.write(level); cs = 1; wait(10); } while(1) { } }
I can't seem to make it work! The chip has a combined MISO and MOSI so I have connected the mbed MOSI via a 3.9kOhm resistor to the chips combined data line.
I have the chip working with an Arduino using the following code but clearly the SPI libraries differ and I have no idea why my translation from one to the other is failing. Is there something I am missing? Thanks!
Expression
#include <SPI.h> #include <avr/io.h> const int slaveSelectPin = 10; void setup() { Serial.begin(9600); pinMode (slaveSelectPin, OUTPUT); SPI.begin(); SPI.setClockDivider(SPI_CLOCK_DIV128); //depends on resistors } void loop() { for (int level = 0; level < 256; level++) { digitalWrite(slaveSelectPin,LOW); SPI.transfer(0); SPI.transfer(level); digitalWrite(slaveSelectPin,HIGH); Serial.println(analogRead(A0)); delay(10); } while(1) { } }
EDIT: Just tried with the following simplified code for the mbed. The LED I have hooked up will light up in the range of 200-256 for the write value but only once. The loop function seems to be unable to change the value from the first write call. I have tried doing spi.write with multiple values and it seems to work but just not in a loop or if repeated.
Expression
#include "mbed.h" SPI spi(p5, NC, p7); // mosi, miso, sclk DigitalOut cs(p8); int main() { cs = 1; spi.format(16,3); spi.frequency(1000000); cs = 0; spi.write(255); cs = 1; wait(100); while(1) { } }
This does not work:
Expression
#include "mbed.h" SPI spi(p5, NC, p7); // mosi, miso, sclk DigitalOut cs(p8); int main() { cs = 1; spi.format(16,3); spi.frequency(1000000); while(1) { cs = 0; spi.write(255); cs = 1; wait(100); cs = 0; spi.write(0); cs = 1; } }
1 Answer
9 years, 10 months ago.
Hi Toby,
Haven't used this library yet, however your line 12 looks suspect
you define level as an int (integer) and then step with a 0.01
line 14 of your original code for the arduino looks right.
The SPI.write class expects an integer.
Regards
Martin
It appears, from further testing that the SPI library does not play well with the wait() function
posted by Toby O'Connell 18 Feb 2015