Hey guys i'm wondering if someone would be able to help me out at the moment i am successfully using a rotary encoder to drive the output of the DAC (MCP4821). Which gives me a voltage out from 0-2.048v which is spot i'm using the 1x gain.
Now i want to implement the MCP4822 with dual channels using the same rotory encoder for both outputs , anyway this is my requirements-
- Have a push button switch between the two DAC channels
- Use the same rotory encoder to output to the DAC
- Keep using the 1xGain
My Codes Listed Below
// MCPDAC relies on SPI.
#include <SPI.h>
#include <MCPDAC.h>
int brightness = 0; // how bright the LED is, start at half brightness
int fadeAmount = 10; // how many points to fade the LED by
unsigned long currentTime;
unsigned long loopTime;
const int pin_A = 7;
const int pin_B = 6;
unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev=0;
void setup() {
// CS on pin 10, no LDAC pin (tie it to ground).
MCPDAC.begin(10);
// Set the gain to "HIGH" mode - 0 to 4096mV.
MCPDAC.setGain(CHANNEL_A,GAIN_LOW);
// Do not shut down channel A, but shut down channel B.
MCPDAC.shutdown(CHANNEL_A,false);
MCPDAC.shutdown(CHANNEL_B,true);
pinMode(pin_A, INPUT);
pinMode(pin_B, INPUT);
currentTime = millis();
loopTime = currentTime;
}
void loop() {
// get the current elapsed time
currentTime = millis();
if(currentTime >= (loopTime + 1)){
// 5ms since last check of encoder = 200Hz
encoder_A = digitalRead(pin_A); // Read encoder pins
encoder_B = digitalRead(pin_B);
if((!encoder_A) && (encoder_A_prev)){
// A has gone from high to low
if(encoder_B) {
// B is high so clockwise
// increase the brightness, dont go over 4095
if(brightness + fadeAmount <= 0x0FFF) brightness += fadeAmount;
}
else {
// B is low so counter-clockwise
// decrease the brightness, dont go below 0
if(brightness - fadeAmount >= 0) brightness -= fadeAmount;
}
}
encoder_A_prev = encoder_A; // Store value of A for next time
MCPDAC.setVoltage(CHANNEL_A,brightness&0x0fff);
loopTime = currentTime; // Updates loopTime
}
// Other processing can be done here
}