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
}





No comments:

Post a Comment

Feel free to contact me with any suggestions, doubts or requests.

Bless