Hello,
I am trying to control the frequency of a wave output through the DAC using a potentiometer, but am getting no changes from the potentiometer.
Complete program code is below, followed by further details as to what I've tried.
//pottest01.txt
//Testing pot to control frequency of a wavetable wave
#include <avr/io.h>
#define F_CPU 4000000UL
#include <util/delay.h>
#include <avr/interrupt.h>
#define TIMER_PERIOD 0x2000
/* VREF start-up time */
#define VREF_STARTUP_TIME (50)
/* Mask needed to get the 2 LSb for DAC Data Register */
#define LSB_MASK (0x03)
static void VREF_init(void);
static void DAC0_init(void);
static void DAC0_setVal(uint16_t value);
const byte saw[256] PROGMEM = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
};
const byte triangle[256] PROGMEM = {
0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 254, 255, 253, 251, 249, 247, 245, 243, 241, 239, 237, 235, 233, 231, 229, 227, 225, 223, 221, 219, 217, 215, 213, 211, 209, 207, 205, 203, 201, 199, 197, 195, 193, 191, 189, 187, 185, 183, 181, 179, 177, 175, 173, 171, 169, 167, 165, 163, 161, 159, 157, 155, 153, 151, 149, 147, 145, 143, 141, 139, 137, 135, 133, 131, 129, 127, 125, 123, 121, 119, 117, 115, 113, 111, 109, 107, 105, 103, 101, 99, 97, 95, 93, 91, 89, 87, 85, 83, 81, 79, 77, 75, 73, 71, 69, 67, 65, 63, 61, 59, 57, 55, 53, 51, 49, 47, 45, 43, 41, 39, 37, 35, 33, 31, 29, 27, 25, 23, 21, 19, 17, 15, 13, 11, 9, 7, 5, 3, 1,
};
int scale[15] = {65, 73, 82, 87, 98, 110, 123, 131, 147, 165, 175, 196, 220, 247, 262};
int potValue = 0;
int mappedPotValue = 0;
unsigned int accumulator;
volatile int note = 65; // Middle C
static void VREF_init(void)
{
VREF.DAC0REF = VREF_REFSEL_2V048_gc /* Select the 2.048V Internal Voltage Reference for DAC */
| VREF_ALWAYSON_bm; /* Set the Voltage Reference in Always On mode */
/* Wait VREF start-up time */
_delay_us(VREF_STARTUP_TIME);
}
static void DAC0_init(void)
{
/* Disable digital input buffer */
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
/* Disable pull-up resistor */
PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;
DAC0.CTRLA = DAC_ENABLE_bm /* Enable DAC */
| DAC_OUTEN_bm /* Enable output buffer */
| DAC_RUNSTDBY_bm; /* Enable Run in Standby mode */
}
static void DAC0_setVal(uint16_t value)
{
/* Store the two LSbs in DAC0.DATAL */
DAC0.DATAL = (value & LSB_MASK) << 6;
/* Store the eight MSbs in DAC0.DATAH */
DAC0.DATAH = value >> 2;
}
ISR(TCA0_OVF_vect) {
int16_t total = 0;
accumulator = accumulator + note;
total += pgm_read_byte(&saw[accumulator >> 8]);
DAC0_setVal(total);
}
void setup()
{
ADC0.CTRLA &= ~(ADC_ENABLE_bm);
TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm;
TCA0.SINGLE.PER = TIMER_PERIOD;
TCA0.SINGLE.CTRLA = (TCA_SINGLE_CLKSEL1_bm | TCA_SINGLE_CLKSEL2_bm);
TCA0.SINGLE.CTRLA |= TCA_SINGLE_ENABLE_bm;
VREF_init();
DAC0_init();
sei();
}
void loop() {
potValue = analogRead(PIN_PD1); //Read the potentiometer
mappedPotValue = map(potValue, 0, 1047, 0, 15);
note = scale[mappedPotValue];
}
The chip is being programmed via UDPI via jtag2UPDI.
I am using DxCore by SpenceKonde. At the Github page for the project,
I found this description as to how to reference and analog pins:
This is the recommended way to refer to pins. Defines are provided of form PIN_Pxn, where x is the letter of the port (A through G), and n is a number 0 ~ 7
Thusly, I am referencing the potentiometer pin as such:
potValue = analogRead(PIN_PD1); //Read the potentiometer
mappedPotValue = map(potValue, 0, 1047, 0, 15);
note = scale[mappedPotValue];
PIN_PD1, According to the data sheet is a pin with analog function. I did also try the PIN_PA(0-6) pins, just in case there was something I didn't understand.
I can get sound out of the DAC, and change the note value in the variable declaration to get a different pitch. I can also hardcode the mappedPotValue
the get a different frequency.
The part that isn't working seems to be the potentiometer/analog input itself.
I have 4 pots on the bread board and tried connecting to each, with the same failure to get a change in values.
In case it helps, I've also attached a breadboard pic.
Would greatly appreciate any help with this issue.
Tom