Advertisement
adria_junyent

Arduino parallel 8 bit DAC sine wave generator sketch

Nov 29th, 2015
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <TimerOne.h>
  2.  
  3. /* Arduino parallel 8 bit DAC sine wave output generator using Timer1*/
  4.  
  5. /* NOTE: I used MATLAB or Octave to generate the tabulated data below. Here's the code:
  6. clear
  7. clc
  8.  
  9. f=1000;
  10. fs=20000;
  11. M=floor(fs/f);
  12.  
  13. disp(M);
  14.  
  15. disp(['{(byte)(127.0+127.0*sin(2.0*3.1416*' num2str(f) '*' num2str(0) '/' num2str(fs) ')),']);
  16. for n=1:(M-2)
  17.     disp(['(byte)(127.0+127.0*sin(2.0*3.1416*' num2str(f) '*' num2str(n) '/' num2str(fs) ')),']);
  18. end
  19. disp(['(byte)(127.0+127.0*sin(2.0*3.1416*' num2str(f) '*' num2str(M-1) '/' num2str(fs) '))};']);
  20. */
  21.  
  22. byte table[20]={(byte)(127.0+127.0*sin(2.0*3.1416*1000*0/20000)),
  23. (byte)(127.0+127.0*sin(2.0*3.1416*1000*1/20000)),
  24. (byte)(127.0+127.0*sin(2.0*3.1416*1000*2/20000)),
  25. (byte)(127.0+127.0*sin(2.0*3.1416*1000*3/20000)),
  26. (byte)(127.0+127.0*sin(2.0*3.1416*1000*4/20000)),
  27. (byte)(127.0+127.0*sin(2.0*3.1416*1000*5/20000)),
  28. (byte)(127.0+127.0*sin(2.0*3.1416*1000*6/20000)),
  29. (byte)(127.0+127.0*sin(2.0*3.1416*1000*7/20000)),
  30. (byte)(127.0+127.0*sin(2.0*3.1416*1000*8/20000)),
  31. (byte)(127.0+127.0*sin(2.0*3.1416*1000*9/20000)),
  32. (byte)(127.0+127.0*sin(2.0*3.1416*1000*10/20000)),
  33. (byte)(127.0+127.0*sin(2.0*3.1416*1000*11/20000)),
  34. (byte)(127.0+127.0*sin(2.0*3.1416*1000*12/20000)),
  35. (byte)(127.0+127.0*sin(2.0*3.1416*1000*13/20000)),
  36. (byte)(127.0+127.0*sin(2.0*3.1416*1000*14/20000)),
  37. (byte)(127.0+127.0*sin(2.0*3.1416*1000*15/20000)),
  38. (byte)(127.0+127.0*sin(2.0*3.1416*1000*16/20000)),
  39. (byte)(127.0+127.0*sin(2.0*3.1416*1000*17/20000)),
  40. (byte)(127.0+127.0*sin(2.0*3.1416*1000*18/20000)),
  41. (byte)(127.0+127.0*sin(2.0*3.1416*1000*19/20000))};
  42.  
  43. byte n=0;
  44.  
  45. void setup()
  46. {
  47.   // Initialize the digital pin as an output.
  48.   pinMode(0, OUTPUT);    
  49.   pinMode(1, OUTPUT);    
  50.   pinMode(2, OUTPUT);    
  51.   pinMode(3, OUTPUT);    
  52.   pinMode(4, OUTPUT);    
  53.   pinMode(5, OUTPUT);    
  54.   pinMode(6, OUTPUT);    
  55.   pinMode(7, OUTPUT);
  56.   pinMode(8, OUTPUT);  
  57.  
  58.   Timer1.initialize(50);
  59.   Timer1.attachInterrupt( timerIsr ); // attach the service routine here
  60. }
  61.  
  62. void loop()
  63. {
  64.   // Main code loop
  65.   // Nothing here
  66. }
  67.  
  68. /// --------------------------
  69. /// Custom ISR Timer Routine
  70. /// --------------------------
  71. void timerIsr()
  72. {    
  73.     byte temp;
  74.     temp=table[n];
  75.     PORTD=temp;
  76.     digitalWrite(8,digitalRead(8)^1);
  77.     n++;
  78.     n=(n>19)?0:n; //MODIFY TO MATCH M-1
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement