CiTRuS
Posts: 1
Joined: Sun Jul 21, 2019 5:49 pm

mcp4725 to mcp3008

Sun Jul 21, 2019 5:56 pm

hello everybody,

i'm trying to write voltage from mcp4725 and reading in mcp3008.

i'm trying to use wiringPI library for better sample rate than python.
i've manage to read all the information from the mcp3008 successfuly,
but for some reason i can't write to the mcp4725.

there's my code for the all system:

Code: Select all

#include <wiringPi.h>
#include <wiringPiSPI.h>
#include <stdio.h>  
#include <stdlib.h>  
#include <stdint.h>
#include <string.h>
#include <time.h>
 #include <unistd.h>
 #include <errno.h>
#include <wiringPiI2C.h>
 #define SAMPLE_SIZE 1000
#define WRITEDAC 0x40
#define WRITEDACEEPROM 0x60
#define MCP4725 0x62
#define CHANNEL 1
#define SPI_CHANNEL 0



int main(void)
{
	//setting up 4725
	int mcp4725 = wiringPiI2CSetup(MCP4725);
	if(mcp4725<0){exit(-1);}	
	int value = 4095; // supposed to output max voltage
 	int data[2];
	data[0] = (value >> 8) & 0xFF; // [0 0 0 0 D12 D11 D10 D9 D8] (first bits are modes for our use 0 is fine)
	data[1] = value ; // [D7 D6 D5 D4 D3 D2 D1 D0]
	
	//setting up 3008
	uint8_t adcnum = CHANNEL;
	float v[SAMPLE_SIZE];
	float k = 3.3/1024;
	FILE *fd=fopen("log.txt","w+" );
	unsigned int commandout = 0;
    	unsigned int adcout = 0;

    	commandout = adcnum & 0x3;  // only 0-7
    	commandout |= 0x18;     // start bit + single-ended bit
	
 
    	uint8_t spibuf[3];

    	spibuf[0] = commandout;
    	spibuf[1] = 0;
    	spibuf[2] = 0;
	clock_t start1, end;
	double cpu_time_used;
	
    	// setup GPIO, this uses actual BCM pin numbers 
    	wiringPiSetupGpio();
    	wiringPiSPISetup(SPI_CHANNEL,6*1000*1000);
    	delay(50);
	start1 = clock();
	
	
	//write and read
	for (int i=0;i<SAMPLE_SIZE;i++){
		spibuf[0] = commandout;
   		spibuf[1] = 0;
   		spibuf[2] = 0;
   		//write to 4725
		wiringPiI2CWriteReg8(mcp4725, data[0], data[1]);
		//read from 3008
	 	wiringPiSPIDataRW(SPI_CHANNEL, spibuf, 3);    

   	 	adcout = ((spibuf[1] << 8) | (spibuf[2])) >> 4;
        	v[i] =k * (float)(adcout);
    }
    end = clock();
	cpu_time_used = ((double) (end - start1)) / CLOCKS_PER_SEC;
	fprintf(stdout,"time taken %f\n",cpu_time_used);
for(int k=0;k<SAMPLE_SIZE ;k++){
		
	//fprintf(fd,"AD vd: = %f\n",	v[k]);
	fprintf(fd,"%2.7f\n",	v[k]);
}

    return 0;
}


hoping i'll find the answer here,
thanks in advance!

Return to “C/C++”