Ive been trying to understand and implement the provided SPI.h library from the Arduino IDE into a program that Ive been working on for too long now, and I cant seem to get it right.. it feels like I've tried so many different things.
My code has no errors, but I dont get any feedback on the LED that I have hooked up to the wiper of my MCP4151 digital potentiometer. Is there somebody who could explain to me what I am missing or doing
incorrectly?
#include <SPI.h>
const int wiper1 = 0000; // volatile wiper0 address is 00h
const int wiper2 = 0001; // volatile wiper1 address is 01h
const int CSpin = 53; // chip select pin variable is set to 53
const int SCKpin = 52; // clock pin variable set to 52
const int MOSIpin = 51; // MasterOutSlaveIn pin set to 51
const int MISOpin = 50; // MasterInSlaveOut pin set to 50
byte sendData; // variable to store the values to transfer to MCP4151
void setup() {
pinMode (CSpin, OUTPUT); // chip select is now set as an output
pinMode (SCKpin, OUTPUT); // clock pin is now set as an output
pinMode (MOSIpin, OUTPUT); // MasterOutSlaveIn is now set as an output
pinMode (MISOpin, INPUT); // MasterInSlaveOut is now set as an input
digitalWrite(CSpin, HIGH); // write the CS pin HIGH to make sure no transmissions happen right away
}
void loop() {
sendData = 150; // variable has a value of 150
SendDataToWiper1(sendData, CSpin); // use SendDataToWiper1() to write sendData values to MCP4151
}
void CS_high(const int CSpin) { // function to write the CS pin HIGH for no bit transfer
digitalWrite(CSpin, HIGH); // writes the CS pin HIGH to disable bit transactions
}
void CS_low(const int CSpin) { // function to write the CS pin LOW to begin bit transfer
digitalWrite(CSpin, LOW); // writes the CS pin LOW to enable bit transactions
}
void SendDataToWiper1(const int sendData, const int CSpin) {
// begins the SPI transaction with the settings for MCP4151
SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
CS_low(CSpin); // write the CS pin LOW to begin bit transaction
delay(250); // a small delay for the clock to remain active until first edge of the clock
SPI.transfer( wiper1); // transfer the wiper address 00h
SPI.transfer( sendData); // transfer the sendData bits
CS_high(CSpin); // writes the CS pin HIGH to end bit transaction
SPI.endTransaction(); // ends the SPI transaction
}
void SendDataToWiper2(const byte sendData, const int CSpin) {
// begins the SPI transaction with the settings for MCP4151
SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
CS_low(CSpin); // write the CS pin LOW to begin bit transaction
delay(250); // a small delay for the clock to remain active until first edge of the clock
SPI.transfer( wiper2); // transfer the wiper address
SPI.transfer( sendData); // transfer the sendData bits
CS_high(CSpin); // writes the CS pin HIGH to end bit transaction
SPI.endTransaction(); // ends the SPI transaction
}