Function Generator made by arduino- Mike Robakidze

Discussion in 'Homework Help' started by dr micron, Aug 18, 2013.

  1. dr micron

    Thread Starter New Member

    Jul 18, 2012
    4
    0
    Function Generator made by arduino and DAC ic ( PCF8591)
    Code written by Mike Robakidze.
    Summer 2013

    Code ( (Unknown Language)):
    1.  
    2. #include "Wire.h"
    3. #include <LiquidCrystal.h> // include the library code:
    4. #define PCF8591 (0x90 >> 1)  // I2C bus address
    5. #define ADC0 0x00  // control bytes for reading individual ADCs
    6.  
    7. byte value0, value1, value2, value3;
    8. LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize lib with intf pins
    9. int modulator;    // this changes frequency of a wave
    10.  
    11. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    12. void setup()
    13. {
    14.  Wire.begin();
    15.  Serial.begin(9600);
    16.  lcd.begin(16, 2);  // set up the LCD's columns and rows:
    17.  }
    18. //++++ Sine Wave +++++++++++++++++++++++++++++++++++++++++++++
    19. uint8_t  sine_wave[256] =     // Sine wave *WAVE*
    20. { 0x80, 0x83, 0x86, 0x89, 0x8C, 0x90, 0x93, 0x96,
    21.   0x99, 0x9C, 0x9F, 0xA2, 0xA5, 0xA8, 0xAB, 0xAE,
    22.   0xB1, 0xB3, 0xB6, 0xB9, 0xBC, 0xBF, 0xC1, 0xC4,
    23.   0xC7, 0xC9, 0xCC, 0xCE, 0xD1, 0xD3, 0xD5, 0xD8,
    24.   0xDA, 0xDC, 0xDE, 0xE0, 0xE2, 0xE4, 0xE6, 0xE8,
    25.   0xEA, 0xEB, 0xED, 0xEF, 0xF0, 0xF1, 0xF3, 0xF4,
    26.   0xF5, 0xF6, 0xF8, 0xF9, 0xFA, 0xFA, 0xFB, 0xFC,
    27.   0xFD, 0xFD, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF,
    28.   0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFD,
    29.   0xFD, 0xFC, 0xFB, 0xFA, 0xFA, 0xF9, 0xF8, 0xF6,
    30.   0xF5, 0xF4, 0xF3, 0xF1, 0xF0, 0xEF, 0xED, 0xEB,
    31.   0xEA, 0xE8, 0xE6, 0xE4, 0xE2, 0xE0, 0xDE, 0xDC,
    32.   0xDA, 0xD8, 0xD5, 0xD3, 0xD1, 0xCE, 0xCC, 0xC9,
    33.   0xC7, 0xC4, 0xC1, 0xBF, 0xBC, 0xB9, 0xB6, 0xB3,
    34.   0xB1, 0xAE, 0xAB, 0xA8, 0xA5, 0xA2, 0x9F, 0x9C,
    35.   0x99, 0x96, 0x93, 0x90, 0x8C, 0x89, 0x86, 0x83,
    36.   0x80, 0x7D, 0x7A, 0x77, 0x74, 0x70, 0x6D, 0x6A,
    37.   0x67, 0x64, 0x61, 0x5E, 0x5B, 0x58, 0x55, 0x52,
    38.   0x4F, 0x4D, 0x4A, 0x47, 0x44, 0x41, 0x3F, 0x3C,
    39.   0x39, 0x37, 0x34, 0x32, 0x2F, 0x2D, 0x2B, 0x28,
    40.   0x26, 0x24, 0x22, 0x20, 0x1E, 0x1C, 0x1A, 0x18,
    41.   0x16, 0x15, 0x13, 0x11, 0x10, 0x0F, 0x0D, 0x0C,
    42.   0x0B, 0x0A, 0x08, 0x07, 0x06, 0x06, 0x05, 0x04,
    43.   0x03, 0x03, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
    44.   0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x03,
    45.   0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x0A,
    46.   0x0B, 0x0C, 0x0D, 0x0F, 0x10, 0x11, 0x13, 0x15,
    47.   0x16, 0x18, 0x1A, 0x1C, 0x1E, 0x20, 0x22, 0x24,
    48.   0x26, 0x28, 0x2B, 0x2D, 0x2F, 0x32, 0x34, 0x37,
    49.   0x39, 0x3C, 0x3F, 0x41, 0x44, 0x47, 0x4A, 0x4D,
    50.   0x4F, 0x52, 0x55, 0x58, 0x5B, 0x5E, 0x61, 0x64,
    51.   0x67, 0x6A, 0x6D, 0x70, 0x74, 0x77, 0x7A, 0x7D};
    52. //+++++ Square Wave  ++++++++++++++++++++++++++++++++++++++++++++++
    53. uint8_t  sine_wavee[256] =           // Square wave *WAVEE*
    54. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    55. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    56. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    70. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    71. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    72. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    73. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    74. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    75. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    76. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    77. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    78. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    79. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    80. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    81. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
    82. //++++++ Triangle Wave ++++++++++++++++++++++++++++++
    83. uint8_t  sine_waveee[256] =  // triangle wave *WAVEEE*
    84. {0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,0x10,0x12,0x14,
    85. 0x16,0x18,0x1A,0x1C,0x1E,0x20,0x22,0x24,0x26,0x28,0x2A,
    86. 0x2C,0x2E,0x30,0x32,0x34,0x36,0x38,0x3A,0x3C,0x3E,0x40,
    87. 0x42,0x44,0x46,0x48,0x4A,0x4C,0x4E,0x50,0x52,0x54,0x56,
    88. 0x58,0x5A,0x5C,0x5E,0x60,0x62,0x64,0x66,0x68,0x6A,0x6C,
    89. 0x6E,0x70,0x72,0x74,0x76,0x78,0x7A,0x7C,0x7E,0x80,0x82,
    90. 0x84,0x86,0x88,0x8A,0x8C,0x8E,0x90,0x92,0x94,0x96,0x98,
    91. 0x9A,0x9C,0x9E,0xA0,0xA2,0xA4,0xA6,0xA8,0xAA,0xAC,0xAE,
    92. 0xB0,0xB2,0xB4,0xB6,0xB8,0xBA,0xBC,0xBE,0xC0,0xC2,0xC4,
    93. 0xC6,0xC8,0xCA,0xCC,0xCE,0xD0,0xD2,0xD4,0xD6,0xD8,0xDA,
    94. 0xDC,0xDE,0xE0,0xE2,0xE4,0xE6,0xE8,0xEA,0xEC,0xEE,0xF0,
    95. 0xF2,0xF4,0xF6,0xF8,0xFA,0xFC,0xFE,0xFF, };
    96. //+++ Sawtooth +++++++++++++++++++++++++++
    97. uint8_t  sine_waveeee[256] =  // Sawtooth wave *WAVEEEE*
    98. {
    99. 0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,0x10,0x12,0x14,
    100. 0x16,0x18,0x1A,0x1C,0x1E,0x20,0x22,0x24,0x26,0x28,0x2A,
    101. 0x2C,0x2E,0x30,0x32,0x34,0x36,0x38,0x3A,0x3C,0x3E,0x40,
    102. 0x42,0x44,0x46,0x48,0x4A,0x4C,0x4E,0x50,0x52,0x54,0x56,
    103. 0x58,0x5A,0x5C,0x5E,0x60,0x62,0x64,0x66,0x68,0x6A,0x6C,
    104. 0x6E,0x70,0x72,0x74,0x76,0x78,0x7A,0x7C,0x7E,0x80,0x82,
    105. 0x84,0x86,0x88,0x8A,0x8C,0x8E,0x90,0x92,0x94,0x96,0x98,
    106. 0x9A,0x9C,0x9E,0xA0,0xA2,0xA4,0xA6,0xA8,0xAA,0xAC,0xAE,
    107. 0xB0,0xB2,0xB4,0xB6,0xB8,0xBA,0xBC,0xBE,0xC0,0xC2,0xC4,
    108. 0xC6,0xC8,0xCA,0xCC,0xCE,0xD0,0xD2,0xD4,0xD6,0xD8,0xDA,
    109. 0xDC,0xDE,0xE0,0xE2,0xE4,0xE6,0xE8,0xEA,0xEC,0xEE,0xF0,
    110. 0xF2,0xF4,0xF6,0xF8,0xFA,0xFC,0xFE,//++++++++++++++
    111. 0xFE,0xFC,0xFA,0xF8,0xF6,0xF4,0xF2,0xF0,0xEE,0xEC,0xEA,
    112. 0xE8,0xE6,0xE4,0xE2,0xE0,0xDE,0xDC,0xDA,0xD8,0xD6,0xD4,
    113. 0xD2,0xD0,0xCE,0xCC,0xCA,0xC8,0xC6,0xC4,0xC2,0xC0,0xBE,
    114. 0xBC,0xBA,0xB8,0xB6,0xB4,0xB2,0xB0,0xAE,0xAC,0xAA,0xA8,
    115. 0xA6,0xA4,0xA2,0xA0,0x9E,0x9C,0x9A,0x98,0x96,0x94,0x92,
    116. 0x90,0x8E,0x8C,0x8A,0x88,0x86,0x84,0x82,0x80,0x7E,0x7C,
    117. 0x7A,0x78,0x76,0x74,0x72,0x70,0x6E,0x6C,0x6A,0x68,0x66,
    118. 0x64,0x62,0x60,0x5E,0x5C,0x5A,0x58,0x56,0x54,0x52,0x50,
    119. 0x4E,0x4C,0x4A,0x48,0x46,0x44,0x42,0x40,0x3E,0x3C,0x3A,
    120. 0x38,0x36,0x34,0x32,0x30,0x2E,0x2C,0x2A,0x28,0x26,0x24,
    121. 0x22,0x20,0x1E,0x1C,0x1A,0x18,0x16,0x14,0x12,0x10,0x0E,
    122. 0x0C,0x0A,0x08,0x06,0x04,0x02,0x00};
    123. //+++ Rectified Sine +++++++++++++++++++++++++++
    124. uint8_t  sine_waveeeee[256] =  // Rectified Sine *WAVEEEEE*
    125. {0x80, 0x83, 0x86, 0x89, 0x8C, 0x90, 0x93, 0x96,
    126.  0x99, 0x9C, 0x9F, 0xA2, 0xA5, 0xA8, 0xAB, 0xAE,
    127.  0xB1, 0xB3, 0xB6, 0xB9, 0xBC, 0xBF, 0xC1, 0xC4,
    128.  0xC7, 0xC9, 0xCC, 0xCE, 0xD1, 0xD3, 0xD5, 0xD8,
    129.  0xDA, 0xDC, 0xDE, 0xE0, 0xE2, 0xE4, 0xE6, 0xE8,
    130.  0xEA, 0xEB, 0xED, 0xEF, 0xF0, 0xF1, 0xF3, 0xF4,
    131.  0xF5, 0xF6, 0xF8, 0xF9, 0xFA, 0xFA, 0xFB, 0xFC,
    132.  0xFD, 0xFD, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF,
    133.  0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFD,
    134.  0xFD, 0xFC, 0xFB, 0xFA, 0xFA, 0xF9, 0xF8, 0xF6,
    135.  0xF5, 0xF4, 0xF3, 0xF1, 0xF0, 0xEF, 0xED, 0xEB,
    136.  0xEA, 0xE8, 0xE6, 0xE4, 0xE2, 0xE0, 0xDE, 0xDC,
    137.  0xDA, 0xD8, 0xD5, 0xD3, 0xD1, 0xCE, 0xCC, 0xC9,
    138.  0xC7, 0xC4, 0xC1, 0xBF, 0xBC, 0xB9, 0xB6, 0xB3,
    139.  0xB1, 0xAE, 0xAB, 0xA8, 0xA5, 0xA2, 0x9F, 0x9C,
    140.  0x99, 0x96, 0x93, 0x90, 0x8C, 0x89, 0x86, 0x83,
    141.  0x80
    142.  };
    143. void loop()
    144. {
    145.  Wire.beginTransmission(PCF8591); // wake up PCF8591
    146.  Wire.send(ADC0); // control byte - read ADC0
    147.  Wire.endTransmission(); // end tranmission
    148.  Wire.requestFrom(PCF8591, 2);
    149.  value0=Wire.receive();
    150.  value0=Wire.receive();
    151.  float modulator=value0/60;
    152.  //+++ sine ++++++++++++++++++++++++++++
    153.  if ( digitalRead (13)==HIGH)
    154.  {
    155.  for (int i=0; i<256; i++)
    156.  {
    157.    Wire.beginTransmission(PCF8591); // wake up PCF8591
    158.     Wire.send(0x40);                // control byte - turn on DAC (binary 1000000)
    159.     Wire.send(sine_wave[i]);        // value to send to DAC
    160.     Wire.endTransmission();          // end tranmission
    161.     delay (modulator );
    162.   }
    163.   //Serial.println ( "<<Sine Wave>>");
    164.  }
    165.  //else  { Serial.println ( "<<<Choose wave>>>" );}
    166.  
    167. //+++ Square +++++++++++++++++++++++++++++++++
    168.  if ( digitalRead (10)==HIGH)
    169.  {
    170.  for (int i=0; i<256; i++)
    171.   {
    172.     Wire.beginTransmission(PCF8591); // wake up PCF8591
    173.     Wire.send(0x40);                // control byte - turn on DAC (binary 1000000)
    174.     Wire.send(sine_wavee[i]);       // value to send to DAC
    175.     Wire.endTransmission();         // end tranmission
    176.     delay (modulator ) ;
    177.   }
    178.   //Serial.println ( "<<Square Wave>>");
    179.  }
    180.  //else  { Serial.println ( "<<<Choose wave>>>" );}
    181.  
    182.  //+++ triangle +++++++++++++++++++++++++++++++++++++++++++++++++
    183.  if ( digitalRead (9)==HIGH)
    184.  {
    185.  for (int i=0; i<256; i++)
    186.   {
    187.     Wire.beginTransmission(PCF8591); // wake up PCF8591
    188.     Wire.send(0x40);                // control byte - turn on DAC (binary 1000000)
    189.     Wire.send(sine_waveee[i]);        // value to send to DAC
    190.     Wire.endTransmission();          // end tranmission
    191.     delay (modulator ) ;
    192. }
    193.   //Serial.println ( "<<Triangle Wave>>");
    194. }
    195.  
    196.  //else  { Serial.println ( "<<<Choose wave>>>" ); }
    197.  
    198.  //+++ Sawtooth +++++++++++++++++++++++++++++++++++++++++++++++++
    199. if ( digitalRead (8)==HIGH)
    200. {
    201.  for (int i=0; i<256; i++)
    202.   {
    203.     Wire.beginTransmission(PCF8591); // wake up PCF8591
    204.     Wire.send(0x40);                // control byte - turn on DAC (binary 1000000)
    205.     Wire.send(sine_waveeee[i]);        // value to send to DAC
    206.     Wire.endTransmission();          // end tranmission
    207.     delay (modulator ) ;
    208.  }
    209.   //Serial.println ( "<<Sawtooth Wave>>");
    210.   //else  { Serial.println ( "<<<Choose wave>>>" ); }
    211. }
    212. //+++ Rectivied Sine ++++++++++++++++++++++++++++++++++++++
    213. if ( digitalRead (7)==HIGH)
    214. {
    215.  for (int i=0; i<256; i++)
    216.   {
    217.     Wire.beginTransmission(PCF8591); // wake up PCF8591
    218.     Wire.send(0x40);                // control byte - turn on DAC (binary 1000000)
    219.     Wire.send(sine_waveeeee[i]);    // value to send to DAC
    220.     Wire.endTransmission();         // end tranmission
    221.     delay (modulator ) ;  
    222.   }
    223.   //Serial.println ( "<<Rectified Wave>>");
    224.   //else  { Serial.println ( "<<<Choose wave>>>" ); }
    225. }    
    226. }
    227. [/i][/i][/i][/i][/i]
     
    Last edited by a moderator: Aug 24, 2013
  2. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
  3. dr micron

    Thread Starter New Member

    Jul 18, 2012
    4
    0
    This is a working code and i am not trying to get help from anybody in this case. the reason i posted it here is that anybody who wants to make their own Function Generator, they can have this code.
    Thanks
     
  4. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,644
    759
    Unusual way to show a project.

    And the circuit? And some explanation maybe?
     
  5. dr micron

    Thread Starter New Member

    Jul 18, 2012
    4
    0
    You are right Mr. atferrari. it is unusual.
    I did not know how to upload schematic ( pictures, PDF so on) but as soon as i get a chance to get familiar with this website i ll upload the rest of the stuff.
     
Loading...