Hi
In this code input to the DAC is sine wave so output is sine.
For testing quality of the DAC I want to replace input to it by signal applied to analog pin PA7.
This is what I did but it is not working.
#include "i2c_stretch.h"
#define DACDevice I2C1
//#define DACAddress 0x60
#define DACAddress 0x67
//uint16_t sineLookup[20] = {2048, 2680, 3251, 3704, 3995, 4095, 3995, 3704, 3251, 2680, 2048, 1415, 844, 391, 100, 0, 100, 391, 844, 1415};
///////////////////
uint16_t volt = 4096;
///////////////////////
//byte sinePos = 0;
byte voltPos = 0;
bool toggle = 0;
//int volt;
void setup()
{
volt = analogRead(PA7);
pinMode(PA7, INPUT_ANALOG);
pinMode(PA1, PWM);
pinMode(33, OUTPUT);
startCommunication();
initTimer();
}
void loop()
{
///////////////////
float volt = analogRead(PA7);
volt = (volt * 3.3) / 4095.0;
/////////////////////
if (i2c_str_IsError(DACDevice) | !i2c_str_PortEnabled(DACDevice))
{
digitalWrite(33, toggle = !toggle);
startCommunication();
}
delay(100);
}
void nextSample()
{
// i2c_str_sendBytes(DACDevice, sineLookup[sinePos] >> 8, sineLookup[sinePos++] & 255);
//sinePos %= 128;
////////////////////////////////////
i2c_str_sendBytes(DACDevice, volt >> 8,volt & 255);
voltPos %= 128;
////////////////////////////////////
}
void startCommunication()
{
i2c_str_InitPort(DACDevice, I2C_STRETCH_DIV_1200KHZ);
i2c_str_StartSending(DACDevice, DACAddress);
}
void initTimer()
{
nvic_irq_set_priority(NVIC_TIMER2, 0);
Timer2.setChannel1Mode(TIMER_OUTPUTCOMPARE);
Timer2.setPrescaleFactor(3);
Timer2.setOverflow(133); //should be about 60khz
Timer2.setCompare1(1);
Timer2.attachCompare1Interrupt(nextSample);
}