Hey all. i have a question i was hoping to get some insight on. my project is complex and don't really want to type it all out. But basically i have a TL494 IC oscillator and a arduino nano monitoring one of the signals and counting pulses and effecting the other 494 signal to make it oscillate... like a 2.5-10khz base frequency that's oscillating under 60hz. The code i wrote works but it has a quark i can't quite understand.
Here's the code i wrote.
void loop() {
digitalWrite(pwmDriver, LOW); //LOW= base signal on
if (digitalRead(counter) > pulseVar) {
count++;
}
if (count>175){
digitalWrite(pwmDriver, HIGH); //HIGH= base freq off
delay(10);
count=0;
}
The count is supposed to be how many pulses i want to see on my oscilloscope at the base frequency with a pause between them. I started out with a base frequency of 11.3k, and to get 10 pulses, the counter had to be set at 35. when i set the frequency to 5.6k, the count had to be set at 70. And when i set the frequency to 2.3k, the count had to be 170. In every situation the pulses happened at the base frequency, but i couldn't understand why i had to increase the count when i went to a lower frequency. I expected the opposite. I've counted pulses before, but never had to do anything like this and don't understand it.
i've been working on this over a 3 day period trying different things. this has been the most progress i've made, but it's backwards of what i expected. I've tried using an attachinterrupt to the signal, and while i was able to count the pulses. i couldn't do anything when the pulse count got to the desired count.
Yes. I'm counting high pulses which are 5v. on the way home from work i was thinking and figured out what the problem is, but not exactly sure how to fix it yet. The code is looping so fast, each time the base freq is high, the loop is counting it multiple times. like at 11khz it's counting it about 3 times before it goes low again, and at 2.3khz, it's counting it about 17 times before it goes low. Which is a problem because these numbers are at roughly 50% duty cycle on the base freq. But that won't be steady. The duty cycle may be as low as 10% or as high as 70%, Which will effect the count the way it is now. So i need to be counting pulses, and not just if the signal is HIGH or not when the loop runs.
int pwmDriver = 5;
int counter = 9;
int count = 0;
int pulseVar = 0;
void setup() {
pinMode(pwmDriver, OUTPUT); //output to control NPN trans
pinMode(counter, INPUT); //input to monitor base freq from TL494
digitalWrite(pwmDriver, HIGH); //HIGH = base signal off
delay(1000);
void loop() {
digitalWrite(pwmDriver, LOW); //LOW= base signal on
if (digitalRead(counter) > pulseVar) {
count++;
}
if (count>175){
digitalWrite(pwmDriver, HIGH); //HIGH= base freq off
delay(10);
count=0;
}
An earlier attempt I tried counting pulses using the code below. but i couldn't get it to work right.
Here's some code that sets up timer 1 on digital pin D5 to count incoming pulses on the rising edge. It includes a 5kHz test output using timer 2 on D11, with the results sent every second to the console. To test, connect D5 to D11 on your board:
// Set-up timer 1 to count pulses on digital pin D5, (with 5kHz test frequency on D11)
volatile uint32_t count;
void setup() {
Serial.begin(115200); // Initialise the serial port
pinMode(11, OUTPUT); // Generate a 5kHz square wave test frequency on D11
TCCR2A = _BV(COM2A0) | _BV(WGM21); // Set timer 2 for CTC (Clear Timer on Compare Match) mode
OCR2A = 49; // Set the D11 output to toggle at 5kHz
TCCR2B = _BV(CS21) | _BV(CS20); // Enable timer clock with a divide by 32 prescaler
TCCR1A = 0; // Clear the TCCR1A register
TCCR1B = _BV(CS12) | _BV(CS11) | _BV(CS10); // Set T1 (D5) as the timer 1 clock source triggered on the rising edge
TIMSK1 = _BV(TOIE1); // Enable overflow interrupts on timer 1
}
void loop() {
delay(1000); // Wait for 1 second
noInterrupts(); // Disable interrupts
Serial.println(count << 16 | TCNT1); // Print the number of pulses counted
count = 0; // Reset the overflow count to 0
TCNT1 = 0; // Reset timer 1 to 0
interrupts(); // Enable interupts
}
ISR(TIMER1_OVF_vect) { // The timer 1 overflow interrupt service routine
count++; // Increment the overflow count
}
The code includes the optional count variable and timer 1 interrupt service routine to count beyond the 16-bit timer 1's maximum value of 65535 (2^16). Just disable the interrupts, by commenting out the line below, if this isn't required:
//TIMSK1 = _BV(TOIE1); // Enable overflow interrupts on timer 1
Further to my last post, here's the pared down code without the count variable and ISR. Again input on D5, test output on D11:
// Set-up timer 1 to count pulses on digital pin D5, (with 5kHz test frequency on D11)
void setup() {
Serial.begin(115200); // Initialise the serial port
pinMode(11, OUTPUT); // Generate a 5kHz square wave test frequency on D11
TCCR2A = _BV(COM2A0) | _BV(WGM21); // Set timer 2 for CTC (Clear Timer on Compare Match) mode
OCR2A = 49; // Set the D11 output to toggle at 5kHz
TCCR2B = _BV(CS21) | _BV(CS20); // Enable timer clock with a divide by 32 prescaler
TCCR1A = 0; // Clear the TCCR1A register
TCCR1B = _BV(CS12) | _BV(CS11) | _BV(CS10); // Set T1 (D5) as the timer 1 clock source triggered on the rising edge
}
void loop() {
delay(1000); // Wait for 1 second
Serial.println(TCNT1); // Print the number of pulses counted
TCNT1 = 0; // Reset timer 1 to 0
}
Pin 9 is not an external interrupt pin. On an UNO, only Pins 2 and 3. See the documentation for attachInterrupt().
In your earlier attempts, your 'count' was not counting pulses but counting the number of times the pin was reading HIGH. Of course, when your pulses are longer the pin will be HIGH for longer periods and you have to adjust your limit to get 10 pulses.
If you switch "counter" to 2 or 3 the interrupt code should work much better.
For your non-interrupt code you could try looking for rising edges:
if (digitalRead(counter) != pulseVar)
{
// 'counter' pin just changed
if (pulseVar == LOW)
{
// 'counter' pin just changed from LOW to HIGH
count++;
pulseVar = HIGH;
}
else
{
// 'counter' pin just changed from HIGH to LOW
pulseVar = LOW;
}
}
Yes, that was a mistake i made when i did the post. Originally i was using pin 9, but when i used the attachinterrupt i was using pin 3. But i will make the changes and see what i can get it to do.
Thanks for all the info. that last bit of code worked perfectly. i've tried several variations similar to it before but didn't quite have it right. i had a decent grasp a few years ago on code, but lost some of it during the break, so it's taking a little while to get back in the groove.
But i'm gonna stick with the basic code for the moment because i planned on using the interrupt pin for other functions in the future.
"That's something that i'll look into. Right now i'm just working with what i'm halfway familiar with. But this program/circuit/project i'm working on is an experiment and doesn't have to be exact. At the moment, there are no set numbers so it has to be variable in nearly every way. The base freq from the TL494 will be adjusted from 500hz to 10khz, and the duty cycle from 10-70%..... that signal will be divided up into small snippets from 4 to 50 pulses spaced between .5hz to 60hz, and both being adjustable. The outgoing modified signal will be driving mosfets for a HV transformer to power a HHO water cell. but in the future, once i get some numbers i'll be able to fine tune the circuit/program for what i need.
As it is, the duty cycle of the TL494 doesn't effect any of my code, for the moment as i'm only using the arduino to count the pulses at any frequency, and then inserting pauses into the output signal of the TL494 with use of a NPN transistor. The PWM coming from the arduino shuts down the output frequency of the 494.
Right now the duty cycle is manually adjusted on the 494, but in the future, once i do some tests and get some numbers, i plan on using the arduino to automatically modify the duty cycle on the 494 as needed. and to do that. I just have to feed a steady 0-3.3v into one of the pins on the 494 and it changes the duty cycle, and that will come from one of the analog pins on the arduino.
i'm gonna try out your code tomorrow and see if i can make any since of it. I've avoided timer codes because i have had no experience with it yet, and don't quite understand it.
I tried the non-interrupt code that johnwasser suggested, and it worked great at frequencies up to 3khz, and was only off on the count by 3-4 which is usable. But when i up the frequency to 5khz, the count is off at least 8... but still usable, but at 10khz it's off at least 20.... all because the pulses are happening faster than the code can read and then act on. and then any extra commands or delays from printing to the LCD screen or reading a pot really throws it off.
i found out that if i remove all of the extra variables that i planned on using for the gate delays. like this code below. the code and count works perfectly at any frequency up to 11k.... and will probably go higher. but i need a way to adjust the delay manually instead of resetting the program.
// int gateReading = analogRead(gatePot);
// long gateTimer = map(gateReading, 1023, 0, 500, 5000);
TCCR1B = _BV(CS12) | _BV(CS11) | _BV(CS10); // Set T1 (D5) as the timer 1 clock source triggered on the rising edge
...configures the 16-bit timer 1 to accept an external clock on its T1 input pin on D5 and increment on the incoming signal's rising edges.
This method has the advantage that the number of incoming pulses can be counted by the timer without any CPU intervention, leaving it free to perform other tasks. It also makes the system immune to interrupt jitter, caused by an increase in the input frequency.
Furthermore, it's possible to set timer 1's OCR1A and OCR1B registers, to trigger their resepective compare A and B interrupt service routines whenever a particular count number has been reached.
today im gonna do some more research and playing around with it. I don't need a test signal since i already have one. i just need to count and act when needed.
last night i stumbled across how to change the port registries and how to speed up the code. For the past few days i've been bitching it takes 4us for a piece of code to run, so hopefully i can resolve that and get it working better.
I've probably spent 3 hours today doing research on the timers/counters code along with port register controls, and after 2 pages of notes and written abbreviations i'm starting to understand it. I have a lot of questions, but wont ask them yet because i need to play with it first.
But i had one stupid question before i start.
How does 49 in this code equivalent to 5khz? OCR2A = 49;
Is that a count determined on the prescaler? or how does that work? or somewhere i can research more about it? even tho i don't plan on generating a set frequency on a pin, i don't like having code that i don't understand how to change it if needed.
You'll find all the information about the ATmega328P microcontroller in its datasheet:
The ATmega328P's timers can be set to a number of different modes of operation.
In this instance, I set up timer 2 to operate in Clear Timer on Compare match or CTC mode. This is described in section 17.7.2 of the datatsheet. It gets the timer to toggle the output (50% duty-cycle) at a specified frequency.
The CTC mode is selected by configuring the TCCR2A register:
TCCR2A = _BV(COM2A0) | _BV(WGM21); // Set timer 2 for CTC (Clear Timer on Compare Match) mode
The timer's prescaler is set in the TCCR2B register:
TCCR2B = _BV(CS21) | _BV(CS20); // Enable timer clock with a divide by 32 prescaler
In the datasheet, section 17.7.2, it provides a formula for calculating the frequency in CTC mode:
f(out) = f(clk) / (2 * N * (1 + OCR2A))
where:
f(clk) = 16MHz (Nano's processor clock frequency)
N = prescaler value
Therefore, if we require a frequency of 5kHz with the prescaler set to 32:
OCR2A = 16MHz / (2 * 32 * f(out)) - 1
OCR2A = 16MHz / (2 * 32 * 5kHz) - 1 = 49
OCR2A = 49
As for timer 1, this is configured to receive an external clock source. This is described in section 16.3.