[SOLVED] Distorted output while trying to interface DAC and ADC with arduino

Status
Not open for further replies.

Chinmaye

Full Member level 3
Joined
Jan 18, 2016
Messages
164
Helped
0
Reputation
0
Reaction score
1
Trophy points
1,298
Activity points
3,145
Hello Experts,
Aa an exercise, I am trying to interface 10 bit ADC MCP3008 wit Arduino-UNO. I am giving a Sine wave input to ADC. I am trying to pass the output of ADC to the DAC and View the same sine wave on the CRO. Sine ADC is 10 Bit and DAC 8 bit, I am trying to Remove the last two bits from the ADC output before giving it to the DAC. Attached is the code. I have also attached the comparison of the original waveform (Green) and the output that i am getting. I am not able to make out what the mistake is ?

Code:
#include <MCP3008.h>
#include <SPI.h>

#define CS_PIN 10
#define CLOCK_PIN 13
#define MOSI_PIN 11
#define MISO_PIN 12
int pins[] = {2, 3, 4, 5, 6, 7, 8, 9};
byte mask;

// put pins inside MCP3008 constructor
MCP3008 adc(CLOCK_PIN, MOSI_PIN, MISO_PIN, CS_PIN);

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
for (int i = 2; i < 10; i++)
  {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
int val = adc.readADC(0);
Serial.println(val);
//delay (500);
 int k =0;
   for (mask = 0000000001; mask>0; mask <<= 1) { //iterate through bit mask
   if (k>1){
    if (val & mask){ // if bitwise AND resolves to true
      digitalWrite(k,HIGH); // send 1
     // Serial.print("1");
     //Serial.println(k);
     
      //delay (200);
    }
    else{ //if bitwise and resolves to false
     digitalWrite(k,LOW); // send 0
     //Serial.print("0");
      //Serial.println(k);
      
    //delay (200);
}
  }
  k++;
   }
}
 

Hi,

From the picture it looks like you loose the 2 MSBs.

Sine ADC is 10 Bit and DAC 8 bit
Here may be the problem.

Recommendation: Shift the 10 bit input value 2 bits to the right.

Klaus
 

No sir. I am trying to knock off the LSB bits.
 

Hi,

This is what the shift to right does.

You can also perform a divide by 4.

Klaus
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…