summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Geldenhuis <none@none>2012-02-14 14:09:16 +1300
committerAndre Geldenhuis <none@none>2012-02-14 14:09:16 +1300
commitc029894a5f92c946e8d6abe70a296ed64a27cb2a (patch)
treee4df6e35f9913499e5c620c2f0f6dc6422d433d9
parente81414c9d391099db7e63b203b680e5cb3685bd7 (diff)
downloadDDS-c029894a5f92c946e8d6abe70a296ed64a27cb2a.tar.gz
DDS-c029894a5f92c946e8d6abe70a296ed64a27cb2a.tar.bz2
DDS-c029894a5f92c946e8d6abe70a296ed64a27cb2a.zip
Fixed c4ms not incrementing
-rw-r--r--DDS.cpp47
-rw-r--r--DDS.h7
-rw-r--r--examples/DDS/DDS.pde18
3 files changed, 43 insertions, 29 deletions
diff --git a/DDS.cpp b/DDS.cpp
index b4dc76e..47488e3 100644
--- a/DDS.cpp
+++ b/DDS.cpp
@@ -50,33 +50,22 @@ const double refclk=31376.6; // measured
// variables used inside interrupt service declared as volatile
volatile byte icnt; // var inside interrupt
volatile byte icnt1; // var inside interrupt
-volatile unsigned long c4ms; // counter incremented all 4ms
+extern volatile unsigned long c4ms; // counter incremented all 4ms
volatile unsigned long phaccu; // phase accumulator
-volatile unsigned long tword_m; // dds tuning word m
+volatile unsigned long tword_m; // dds tuning word m
-DDS::DDS(double startfreq=1000)
+DDS::DDS()
{
- // Timer2 Clock Prescaler to : 1
- enableT (TCCR2B, CS20);
- disableT (TCCR2B, CS21);
- disableT (TCCR2B, CS22);
-
- // Timer2 PWM Mode set to Phase Correct PWM
- disableT (TCCR2A, COM2A0); // clear Compare Match
- enableT (TCCR2A, COM2A1);
- enableT (TCCR2A, WGM20); // Mode 1 / Phase Correct PWM
- disableT (TCCR2A, WGM21);
- disableT (TCCR2B, WGM22);
//set timers and interupt
- SetDDSTimers(1);
- SetFreq(startfreq);
+ //SetDDSTimers(1);
+ //SetFreq(startfreq);
//activate outputs on D11
pinMode(11, OUTPUT);
-
+ pinMode(7,OUTPUT);
}
@@ -86,6 +75,20 @@ void DDS::SetFreq(double inputfreq)
}
+void DDS::initTimers(){
+ // Timer2 Clock Prescaler to : 1
+ enableT (TCCR2B, CS20);
+ disableT (TCCR2B, CS21);
+ disableT (TCCR2B, CS22);
+
+ // Timer2 PWM Mode set to Phase Correct PWM
+ disableT (TCCR2A, COM2A0); // clear Compare Match
+ enableT (TCCR2A, COM2A1);
+
+ enableT (TCCR2A, WGM20); // Mode 1 / Phase Correct PWM
+ disableT (TCCR2A, WGM21);
+ disableT (TCCR2B, WGM22);
+}
void DDS::SetDDSTimers(byte enableCom){
if(enableCom==1){
@@ -95,6 +98,8 @@ void DDS::SetDDSTimers(byte enableCom){
else{
disableT(TIMSK2,TOIE2); //disable Timer2 Interupt, stops DDS.
enableT (TIMSK0,TOIE0); //renable Timer0, delay now worls again
+ //test=5;
+ //Serial.println(test,DEC);
}
}
@@ -106,17 +111,19 @@ void DDS::SetDDSTimers(byte enableCom){
// runtime : 8 microseconds ( inclusive push and pop)
ISR(TIMER2_OVF_vect) {
- //sbi(PORTD,7); // Test / set PORTD,7 high to observe timing with a oscope
+ enableT(PORTD,7); // Test / set PORTD,7 high to observe timing with a oscope
phaccu=phaccu+tword_m; // soft DDS, phase accu with 32 bits
icnt=phaccu >> 24; // use upper 8 bits for phase accu as frequency information
// read value fron ROM sine table and send to PWM DAC
OCR2A=pgm_read_byte_near(sine256 + icnt);
-
+
+ //icnt1=icnt1+1;
+
if(icnt1++ >= 125) { // increment variable c4ms all 4 milliseconds
c4ms+=4;
icnt1=0;
}
- //cbi(PORTD,7); // reset PORTD,7
+ disableT(PORTD,7); // reset PORTD,7
}
diff --git a/DDS.h b/DDS.h
index 43919b3..a0f4d20 100644
--- a/DDS.h
+++ b/DDS.h
@@ -35,9 +35,12 @@
class DDS
{
public:
- DDS(double startfreq);
+ DDS();
+ void initTimers();
void SetDDSTimers(byte enableCom);
void SetFreq(double inputfreq);
- volatile unsigned long c4ms;
+ //volatile unsigned long c4ms;
+ volatile byte icnt1;
+ volatile byte test;
};
#endif
diff --git a/examples/DDS/DDS.pde b/examples/DDS/DDS.pde
index 9257c02..32a561b 100644
--- a/examples/DDS/DDS.pde
+++ b/examples/DDS/DDS.pde
@@ -25,16 +25,20 @@
// Academy of Media Arts Cologne
#include <DDS.h>
-
-DDS myDDS(1500);
-
+extern volatile unsigned long c4ms; //kinda gross. increments every 4ms while DDSTimers are on
+DDS myDDS;
void setup()
{
- myDDS.SetFreq(1500); //get the frequncy from the DominoEX object
-
+ myDDS.initTimers();
+ myDDS.SetFreq(1500); //set Freq
+ myDDS.SetDDSTimers(1); //start the timers
+ Serial.begin(38400);
}
void loop()
{
- //do nothing
-
+ myDDS.SetDDSTimers(0); //disable DDS and reenable timer0
+ Serial.println(c4ms,DEC);
+
+ //Do timer related stuff (delay, millis() etc)
+ myDDS.SetDDSTimers(1); //enable DDS whcih disables Timer0 to prevent jitter
}