Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TimerOne.h>
- /* Arduino parallel 8 bit DAC sine wave output generator using Timer1*/
- /* NOTE: I used MATLAB or Octave to generate the tabulated data below. Here's the code:
- clear
- clc
- f=1000;
- fs=20000;
- M=floor(fs/f);
- disp(M);
- disp(['{(byte)(127.0+127.0*sin(2.0*3.1416*' num2str(f) '*' num2str(0) '/' num2str(fs) ')),']);
- for n=1:(M-2)
- disp(['(byte)(127.0+127.0*sin(2.0*3.1416*' num2str(f) '*' num2str(n) '/' num2str(fs) ')),']);
- end
- disp(['(byte)(127.0+127.0*sin(2.0*3.1416*' num2str(f) '*' num2str(M-1) '/' num2str(fs) '))};']);
- */
- byte table[20]={(byte)(127.0+127.0*sin(2.0*3.1416*1000*0/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*1/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*2/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*3/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*4/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*5/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*6/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*7/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*8/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*9/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*10/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*11/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*12/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*13/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*14/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*15/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*16/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*17/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*18/20000)),
- (byte)(127.0+127.0*sin(2.0*3.1416*1000*19/20000))};
- byte n=0;
- void setup()
- {
- // Initialize the digital pin as an output.
- pinMode(0, OUTPUT);
- pinMode(1, OUTPUT);
- pinMode(2, OUTPUT);
- pinMode(3, OUTPUT);
- pinMode(4, OUTPUT);
- pinMode(5, OUTPUT);
- pinMode(6, OUTPUT);
- pinMode(7, OUTPUT);
- pinMode(8, OUTPUT);
- Timer1.initialize(50);
- Timer1.attachInterrupt( timerIsr ); // attach the service routine here
- }
- void loop()
- {
- // Main code loop
- // Nothing here
- }
- /// --------------------------
- /// Custom ISR Timer Routine
- /// --------------------------
- void timerIsr()
- {
- byte temp;
- temp=table[n];
- PORTD=temp;
- digitalWrite(8,digitalRead(8)^1);
- n++;
- n=(n>19)?0:n; //MODIFY TO MATCH M-1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement