altineller 4 Posted August 30, 2015 Share Posted August 30, 2015 Hello, I put two of those light sensors, and wrote the following code to count pulses from them using interrupts. Unfortunately, the sensors output can go upto 1Mhz. So in low light conditions it will work, but in when i turn a light bulb on near, it will just freeze. when i turn of the bulb it will continue, with a high reading, in millions, and then reset to zero again. I am thinking interrupts are coming in too fast for the mcu to count, or they get put in a queue. Any other methods for reading this sensor? Without use of interrupts maybe? One idea I had was to put a divide by 16 counter, and use this output, but it would be nice to handle them with no external parts. Any ideas, recomendations and help greatly appreciated. Best regards, C.A. #define LEFT_FRONT P2_0 #define RIGHT_FRONT P2_3 volatile long ctr_left_front = 0L; volatile long ctr_right_front = 0L; static volatile long second_mark; static unsigned long second_interval = 100000L; void setup() { Serial.begin(9600); Serial.println("hello"); pinMode(LEFT_FRONT, INPUT_PULLUP); pinMode(RIGHT_FRONT, INPUT_PULLUP); attachInterrupt(LEFT_FRONT, intr_left_front, FALLING); attachInterrupt(RIGHT_FRONT, intr_right_front, FALLING); } long micro_mark; void loop() { micro_mark = micros(); // second interval if(micro_mark - second_mark > second_interval) { noInterrupts(); Serial.print(ctr_left_front); Serial.print('\t'); Serial.println(ctr_right_front); ctr_left_front = 0L; ctr_right_front = 0L; second_mark = micro_mark; // critical, time-sensitive code here interrupts(); } } void intr_left_front() { ctr_left_front++; } void intr_right_front() { ctr_right_front++; } Quote Link to post Share on other sites
Fmilburn 445 Posted August 30, 2015 Share Posted August 30, 2015 Hello @@altineller According to the datasheet the maximum frequency without saturation is 500 kHz. Figure 1 in the datasheet shows this occurs at less than than 1000 uW/cm2 irradiance. If this is what is happening, a perhaps less than elegant solution might be to place a filter over it. Quote Link to post Share on other sites
chicken 630 Posted August 30, 2015 Share Posted August 30, 2015 As you basically need a frequency counter, this thread might be relevant: http://forum.43oh.com/topic/4088-how-to-get-signal-frequency/ Quote Link to post Share on other sites
chicken 630 Posted August 30, 2015 Share Posted August 30, 2015 I took the lack of a convenient solution for such a simple problem as a challenge to start a Counter library for Energia. Attached a first version that works for P1.0 on MSP430G2533 and MSP430F5529. This library leverages the Timer_A peripheral and uses P1.0 as external clock input for a 16 bit counter. It has 5 functions: start() initializes the timer peripheral and I/O pin and starts the counter stop() stops the counter read() reads the current value of the counter reset() resets the counter to 0, the counter keeps running readAndReset() reads the current value and resets the counter to 0 Example: #include "CounterLib_t.h" Counter<> MyCounter; // setup a counter void setup() { Serial.begin(9600); MyCounter.start(); // initialize and start counter } void loop() { MyCounter.reset(); // reset counter to 0 delay(1000); Serial.println(MyCounter.read()); // read counter and print out value } Connected to the 1KHz calibration signal output of my oscilloscope (Rigol DS1054Z) I get the following output: 1005 1006 1005 1005 1006 .. I think you should be able to measure 500 KHz by changing the delay between reset and read to 50-100ms. Make sure to add another longer delay after Serial.print to not overwhelm the serial output. Caveats: The counter will restart at 0 after 65536 clock cycles, that's why it's called a 16 bit counter. P1.0 is also used for LED1, you may need to remove the LED1 jumper if the signal source is not strong enough to drive the LED On the MSP430F5529LP, you have to use the upper pin of the LED1 jumper to input the signal Make sure to verify, that the signal source does not exceed 3.3V or you may damage the MSP430 This library may not play nicely with other libraries or functionality that uses Timer A (PWM / analogWrite?) Todo: Configurable /2, /4 and /8 clock dividers, currently the counter is increased on each clock Support for other MCUs and LaunchPads Edit: Updated library with basic support for MSP430F5529 Edit2: Uploaded the correct library file (dooh!) CounterLib_t.h tripwire and Fmilburn 2 Quote Link to post Share on other sites
altineller 4 Posted August 31, 2015 Author Share Posted August 31, 2015 @@chicken I will try your library. Also one more requirement that I have is to be able to read 4 of those sensors at the same time. So, having limited timers, I guess I am out of luck? Quote Link to post Share on other sites
chicken 630 Posted August 31, 2015 Share Posted August 31, 2015 The MSP430F5529 has 4 timer clocks exposed. Though I only implemented it for TimerA0 yet, and one of them (P7.7 IIRC) is not broken out on the LaunchPad. Do you have to measure all sensors all the time? Couldn't you connect them all to the same counter but activate and measure the sensors one at a time, for say 100ms each? Quote Link to post Share on other sites
chicken 630 Posted September 2, 2015 Share Posted September 2, 2015 I did some more work on this library and started a new thread for it. http://forum.43oh.com/topic/8870-hardware-counter-library-for-msp430 altineller 1 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.