Showing posts with label DAC MCP4725. Show all posts
Showing posts with label DAC MCP4725. Show all posts

Wednesday, 29 April 2015

ChipKIT DP32 I2C Bus Speed with a MCP4725 DAC

Both arduino and MPIDE seem to have the I2C bus speed set to 100 kHz by default. So, here it is how to change it to 400kHz.
Inside the MPIDE folder, go to hardware\pic32\libraries\Wire\utility.
Inside file twi.h Find the string #define TWI_FREQ 100000 and change it to:  #defineTWI_FREQ 400000.
In my simple test i went from a 20Hz sinewave to a 72Hz just by effecting this change to the bus speed.
On the DP32 Board, SDA is assigned to Pin RB9 and SCL to Pin RB8.





Saturday, 21 July 2012

MCP4725 DAC part 2


Sinewave with 360 steps wavetable , using the PROGMEM, as configured in the
  pgmspace.h library, variable in time by the analog in of a pot, just as 
the example before.

/* Includes */
#include <Wire.h>
#include <avr/pgmspace.h>
#include <util/delay.h>

/* Wavetables */
#include "sin360.h"

/* setup() */
void setup()
{
  int potRead = 0; //potentiometer read in pin Analog 0
  long potValue ; //value for potentiometer read 
  pinMode (potRead, INPUT);
  /* Enable Internal pull-up resistors through software (not required with a single module on the I2C bus) */
  pinMode(A4, OUTPUT);
  digitalWrite(A4, HIGH);
  pinMode(A5, OUTPUT);
  digitalWrite(A5, HIGH);
  /* Init bus I2C */
  Wire.begin();
}
/* loop() */
void loop(){
  /* Variables  */
  static uint16_t sample, i;
  /* Look-up table (aka wavetable) */
  for(i = 0; i < 360; ++i) {
    /*  Read a sample of the wavetable */
    sample = pgm_read_word(sin360 + i);
    int potRead = 0;     //potentiometer read in pin Analog 0
    long potValue ;     //value for potentiometer read 
    potValue = analogRead(potRead);
    //we map amalog 0-1023/1-4096 for DAC:
    potValue = map(analogRead(potRead), 0, 1023, 1, 4096); 
    /*  Starts communication with the MCP4725 */
    Wire.beginTransmission(B1100000);
    /* Sending the command "DAC Update Register" */
    Wire.write(B1000000);
    /*  Sending the 12-bit data */
    Wire.write((sample & 0xFF0) >> 4); // The 8 bits in first
    Wire.write((sample & 0xF) << 4); // Then the 4 bits of Least Significant Bit- LSB
    _delay_us(potValue); // delay 30 ms, acording to <util/delay.h
    /* Stop  commnication  MCP4725 */
    Wire.endTransmission();
  }
}

"sin360.h"

uint16_t sin360[] PROGMEM = {
  0, 1, 2, 4, 7, 11, 15, 19, 25, 31, 37, 44, 52, 60, 69, 79, 
  89, 100, 111, 123, 135, 149, 162, 176, 191, 207, 223,
  239, 256, 274, 292, 311, 330, 349, 370, 390, 412, 433, 456, 
  478, 502, 525, 549, 574, 599, 625, 650, 677, 704, 731, 758, 
  786, 815, 843, 872, 902, 932, 962, 992, 1023, 1054, 1085, 
  1117, 1149, 1181, 1214, 1247, 1280, 1313, 1346, 1380, 1414, 
  1448, 1482, 1517, 1551, 1586, 1621, 1656, 1691, 1726, 1762, 
  1797, 1833, 1868, 1904, 1939, 1975, 2011, 2046, 2047, 2082, 
  2118, 2154, 2189, 2225, 2260, 2296, 2331, 2367, 2402, 2437, 
  2472, 2507, 2542, 2576, 2611, 2645, 2679, 2713, 2747, 2780, 
  2813, 2846, 2879, 2912, 2944, 2976, 3008, 3039, 
  3070, 3101, 3131, 3161, 3191, 3221, 3250, 3278, 3307, 3335, 
  3362, 3389, 3416, 3443, 3468, 3494, 3519, 3544, 3568, 3591, 
  3615, 3637, 3660, 3681, 3703, 3723, 3744, 3763, 3782, 3801, 
  3819, 3837, 3854, 3870, 3886, 3902, 3917, 3931, 3944, 3958, 
  3970, 3982, 3993, 4004, 4014, 4024, 4033, 4041, 4049, 4056, 
  4062, 4068, 4074, 4078, 4082, 4086, 4089, 4091, 4092, 4093, 
  4094, 4093, 4092, 4091, 4089, 4086, 4082, 4078, 4074, 4068, 
  4062, 4056, 4049, 4041, 4033, 4024, 4014, 4004, 3993, 3982, 
  3970, 3958, 3944, 3931, 3917, 3902, 3886, 3870, 3854, 3837, 
  3819, 3801, 3782, 3763, 3744, 3723, 3703, 3681, 3660, 3637, 
  3615, 3591, 3568, 3544, 3519, 3494, 3468, 3443, 3416, 3389, 
  3362, 3335, 3307, 3278, 3250, 3221, 3191, 3161, 3131, 3101, 
  3070, 3039, 3008, 2976, 2944, 2912, 2879, 2846, 2813, 2780, 
  2747, 2713, 2679, 2645, 2611, 2576, 2542, 2507, 2472, 2437, 
  2402, 2367, 2331, 2296, 2260, 2225, 2189, 2154, 2118, 2082, 
  2047, 2011, 1975, 1939, 1904, 1868, 1833, 1797, 1762, 1726, 
  1691, 1656, 1621, 1586, 1551, 1517, 1482, 1448, 1414, 1380, 
  1346, 1313, 1280, 1247, 1214, 1181, 1149, 1117, 1085, 1054, 
  1023, 992, 962, 932, 902, 872, 843, 815, 786, 758, 731, 704, 
  677, 650, 625, 599, 574, 549, 525, 502, 478, 456, 433, 412, 
  390, 370, 349, 330, 311, 292, 274, 256, 239, 223, 207, 191, 
  176, 162, 149, 135, 123, 111, 100, 89, 79, 69, 60, 52, 44, 
  37, 31, 25, 19, 15, 11, 7, 4, 2, 1, 0
};

Friday, 20 July 2012

12 bits Digital to analog (DAC) MCP4725: variable (basic)sample


A ramp up                      
A round sine                          


#include <Wire.h>
#include <util/delay.h>
// Device = 96;  This hardwired into the IC and the BoB, in other words, it is a given.
#define MCP4725_DID 96 // define device id - see datasheet
int sample = 0 ; //sample variable
int OOMPH[64] = {
  2, 2, 22, 61, 119, 196, 291, 402, 530, 672, 827, 994, 1172,
  1357, 1549, 1747, 1947, 2147, 2347, 2545, 2737, 2922, 3100, 3267, 3422, 3564, 3692, 3803,
  3898, 3975, 4033, 4072, 4092, 4092, 4072, 4033, 3975, 3898, 3803,
  3692, 3564, 3422, 3267, 3100, 2922, 2737, 2545, 2347, 2147, 1947,
  1747, 1549, 1357, 1172, 994, 827, 672, 530, 402, 291, 196, 119, 61,
  22, } 
;
/* uncomment to use */
/* int sintab [64] = {
 2147 , 2347, 2545, 2737, 2922, 3100, 3267, 3422, 3564, 3692, 3803,
 3898, 3975, 4033, 4072, 4092, 4092, 4072, 4033, 3975, 3898, 3803,
 3692, 3564, 3422, 3267, 3100, 2922, 2737, 2545, 2347, 2147, 1947,
 1747, 1549, 1357, 1172, 994, 827, 672, 530, 402, 291, 196, 119, 61,
 22, 2, 2, 22, 61, 119, 196, 291, 402, 530, 672, 827, 994, 1172,
 1357, 1549, 1747, 1947} ;*/

/* int ramp_up [64] = { 
 2347, 2545, 2737, 2922, 3100, 3267, 3422, 3564, 3692, 3803,
 3898, 3975, 4033, 4072, 4092, 2, 22, 61, 119, 196, 
 291, 402, 530, 672, 827, 994, 1172, 1357, 1549, 1747, 
 1947, 2347, 2545, 2737, 2922, 3100, 3267, 3422, 3564, 3692, 
 3803, 3898, 3975, 4033, 4072, 4092, 2, 22, 61, 119, 
 196, 291, 402, 530, 672, 827, 994, 1172, 1357, 1549, 
 1747, 1947, 2347, 2545 } ; */
void setup()
{
  int potRead = 0; //potentiometer read in pin Analog 0
  long potValue ; //value for potentiometer read 
  pinMode (potRead, INPUT);
  Wire.begin() ;
} 


void loop()
{
  int potRead = 0;     //potentiometer read in pin Analog 0
  long potValue ;     //value for potentiometer read 
  potValue = analogRead(potRead);
  //we map amalog 0-1023/1-19000 for DAC:
  potValue = map(analogRead(potRead), 0, 1023, 1, 19000); 
  // Start transmission
  Wire.beginTransmission(MCP4725_DID); //device adress
  Wire.write(64);                     // cmd to update the DAC
  Wire.write(OOMPH[sample] >> 4);        // the 8 most significant bits...
  Wire.write((OOMPH[sample] & 15) << 4); /* the 4 least significant bis...*/
  Wire.endTransmission();
  sample = (sample + 1) &63 ;
  _delay_us(potValue); // delay 30 ms, acording to <util/delay.h
}