speed07 1 Posted January 27, 2017 Share Posted January 27, 2017 Hi all, I have recently found a sketch in arduino forum for frequency counting, which I tried running in MSP432 with energia and MSP430 with energia sketch on Code composer studio. The sketch basically counts the frequency using interrupts and prints the frequency on serial monitor but serial monitor is empty while running the program,.The sketch is as shown below, // Frequency counter sketch, for measuring frequencies low enough to execute an interrupt for each cycle // Connect the frequency source to the INT0 pin (digital pin 2 on an Arduino Uno) volatile unsigned long firstPulseTime; volatile unsigned long lastPulseTime; volatile unsigned long numPulses; void isr() { unsigned long now = micros(); if (numPulses == 1) { firstPulseTime = now; } else { lastPulseTime = now; } ++numPulses; } void setup() { Serial.begin(115200); // this is here so that we can print the result pinMode(3, OUTPUT); // put a PWM signal on pin 3, then we can connect pin 3 to pin 2 to test the counter analogWrite(3, 128); } // Measure the frequency over the specified sample time in milliseconds, returning the frequency in Hz float readFrequency(unsigned int sampleTime) { numPulses = 0; // prime the system to start a new reading attachInterrupt(0, isr, RISING); // enable the interrupt delay(sampleTime); detachInterrupt(0); return (numPulses < 3) ? 0 : (1000000.0 * (float)(numPulses - 2))/(float)(lastPulseTime - firstPulseTime); } void loop() { float freq = readFrequency(1000); Serial.println(freq); delay(1000); } Quote Link to post Share on other sites
LiviuM 43 Posted January 27, 2017 Share Posted January 27, 2017 Where are you defining the input (counter) pin? speed07 1 Quote Link to post Share on other sites
LiviuM 43 Posted January 27, 2017 Share Posted January 27, 2017 LE Found it, the pins default to inputs: LaunchPad pins default to inputs, so they don energia 1 Quote Link to post Share on other sites
speed07 1 Posted January 27, 2017 Author Share Posted January 27, 2017 I redefined the interrupt pin and now the serial monitor shows output as 0.00 but it should be showing 490Hz (PWM frequency), where have I gone wrong ? Quote Link to post Share on other sites
NurseBob 111 Posted January 27, 2017 Share Posted January 27, 2017 I don't use Energia all that much, but doesn't delay() put the mcu into LPM4 - no timers, only hdwe interrupts? Since the frequency counter requires the timers, you might consider looking there... Quote Link to post Share on other sites
speed07 1 Posted January 27, 2017 Author Share Posted January 27, 2017 Btw the the serial monitor is outputting 490 after explicitly declaring the interrupt pin as input in void setup().Thanks for the tip, LiviuM. // Frequency counter sketch, for measuring frequencies low enough to execute an interrupt for each cycle // Connect the frequency source to the INT0 pin (digital pin 2 on an Arduino Uno) volatile unsigned long firstPulseTime; volatile unsigned long lastPulseTime; volatile unsigned long numPulses; void isr() { unsigned long now = micros(); if (numPulses == 1) { firstPulseTime = now; } else { lastPulseTime = now; } ++numPulses; } void setup() { Serial.begin(115200); // this is here so that we can print the result pinMode(4, OUTPUT); // put a PWM signal on pin 3, then we can connect pin 3 to pin 2 to test the counter analogWrite(4,128); pinMode(11,INPUT); } // Measure the frequency over the specified sample time in milliseconds, returning the frequency in Hz float readFrequency(unsigned int sampleTime) { numPulses = 0; // prime the system to start a new reading attachInterrupt(11, isr, RISING); // enable the interrupt delay(sampleTime); detachInterrupt(0); return (numPulses < 3) ? 0 : (1000000.0 * (float)(numPulses - 2))/(float)(lastPulseTime - firstPulseTime); } void loop() { float freq = readFrequency(1000); Serial.println(freq); delay(1000); } Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.