Offline
Newbie
Karma: 0
Posts: 48
|
 |
« on: November 18, 2012, 03:16:04 pm » |
Hi, I'm trying to just toggle the output pin several times to get desired timing signal. However I'm getting some errors in output. Few signals/toggles get missing on random interval. Is this called Signal Tearing? How to fix it?Please advice. #define NOP __asm__ __volatile__("nop\n\t") void setup() { DDRB = DDRB | B00111111; // sets Arduino pins 8 to 13 as outputs, pins GND & AREF are unchanged PORTB = 0; } void loop() { while(1) { for(int j=0;j <5; j++) { for(int i=0;i<24; i++) { PORTB = 1; NOP; NOP; NOP; NOP; PORTB = 0; NOP; NOP; NOP; NOP; } } PORTB = 0; delayMicroseconds(200); } } Output where you can see some signals missing for about 6.4uSec http://i49.tinypic.com/6zwps4.jpgThanks
|
|
|
|
« Last Edit: November 18, 2012, 03:26:05 pm by DirtyBits »
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #1 on: November 18, 2012, 03:28:51 pm » |
The timer interrupt fires every 1.024 mS which would stop your loop temporarily. If you want a proper signal use the hardware timers, not a timed loop. Examples here: http://www.gammon.com.au/forum/?id=11504
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #2 on: November 18, 2012, 03:31:11 pm » |
There is also a small gap just after where you have 40 uS on the screenshot (which is after 24 pulses) which would be where the loop of 24 ends.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 48
|
 |
« Reply #3 on: November 18, 2012, 03:33:19 pm » |
There is also a small gap just after where you have 40 uS on the screenshot (which is after 24 pulses) which would be where the loop of 24 ends.
true ..1st let me refer to the link you send...brb
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 48
|
 |
« Reply #4 on: November 18, 2012, 04:16:34 pm » |
The timer interrupt fires every 1.024 mS which would stop your loop temporarily. If you want a proper signal use the hardware timers, not a timed loop. Examples here: http://www.gammon.com.au/forum/?id=11504Thanks a lott Nick..you are a super star!! I tried to scan through the whole Timer and Interrupt topics...understood few bits and pieces but finally found out that cli (); is needed to Disable Interrupt flag. I put that after "While" in the loop and it works!! while(1) { cli (); for(int j=0;j <5; j++) {
But the question is would that affect any other Interrupt routine in the "While" loop?? i.e. if I'm using Serial.read function then??
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #5 on: November 18, 2012, 04:21:54 pm » |
If you turn off interrupts you won't be getting any serial reads (or writes) and the timer functions will return incorrect time.
You are better off generating pulses in hardware.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6391
-
|
 |
« Reply #6 on: November 18, 2012, 04:34:10 pm » |
would that affect any other Interrupt routine ...
If you disable interrupts then you disable all interrupts. Nick's suggestion to use a hardware timer to do the fixed frequency output did not rely on disabling interrupts and seems like a much better approach to take.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #7 on: November 18, 2012, 04:41:26 pm » |
cli (); is needed to Disable Interrupt flag. It doesn't disable interrupt flags: it disable interrupt execution. The flags will be set as they otherwise would be, except that they don't trigger isrs anymore. Before you disable it, think hard if you have to. If your pulses needs to be generated absolutely as you had written, without interruption, you then have no choice but to disable interrupts.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #8 on: November 18, 2012, 05:07:11 pm » |
Before you disable it, think hard if you have to. If your pulses needs to be generated absolutely as you had written, without interruption, you then have no choice but to disable interrupts.
Even then there will be glitches at the end of loops, as I pointed out above. He has "no choice" but to use the hardware timers if he wants glitch-free pulses. What frequency are you after? 1 MHz? This sketch does that, nice and simple: const byte LED = 3; // Timer 2 "B" output: OC2B
const long frequency = 1000000; // 1 MHz
void setup() { pinMode (LED, OUTPUT);
TCCR2A = _BV (WGM20) | _BV (WGM21) | _BV (COM2B1); // fast PWM, clear OC2A on compare TCCR2B = _BV (WGM22) | _BV (CS20); // fast PWM, no prescaler OCR2A = (F_CPU / frequency) - 1; // zero relative OCR2B = ((OCR2A + 1) / 2) - 1; // 50% duty cycle } // end of setup
void loop() { // do other stuff here }
Glitch-free output:  Plus you are free to do whatever you want in the main loop, interrupts, serial send/receive, whatever.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #9 on: November 18, 2012, 05:22:57 pm » |
If you have to protect the transmission, you may want to think what it is that you are trying to protect: you can protect the pulse width (high or low, or high and low); you can protect every 8 pulses; or you can protect all 24 pulses.
That will determine where / when you disable / re-enable the interrupts.
The key is to minimize the duration of disabling them.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6391
-
|
 |
« Reply #10 on: November 18, 2012, 07:23:18 pm » |
That will determine where / when you disable / re-enable the interrupts.
I disagree. Nick Gammon has already demonstrated a far better approach which does not require or rely on disabling interrupts. A general rule I try to follow is to use interrupts only where necessary, and when using interrupts disable them only where necessary. In this case, neither the use of interrupt nor the disabling of interrupts are necessary.
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15314
Measurement changes behavior
|
 |
« Reply #11 on: November 18, 2012, 07:39:43 pm » |
Nick;
That's a very impressive application of a timer. I have a few questions:
1. Can the OCR2A&B registers be changed 'on the fly' to other values without having to do anything else? 2. What would the max and min frequencies be on a standard 16 Mhz board with retaining @ 50% duty cycle? 3. What would be the commands to stop the timer at any time, and to restart it at any time.
Sure seems like this could be made into a nice simple general purpose function clock generator?
Lefty
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #12 on: November 18, 2012, 09:40:03 pm » |
Can the OCR2A&B registers be changed 'on the fly' to other values without having to do anything else? Yes they can. They would take effect next cycle. If you are going smaller it might be best to stop the clock in case it overshoots the new limit, and then start it again. What would the max and min frequencies be on a standard 16 Mhz board with retaining @ 50% duty cycle? With no prescaler: 62.5 KHz to 4 MHz. With a prescaler of 8: 7813 Hz to 1 MHz. And so on. (With no prescaler it should go to 8 MHz but I got artifacts doing that). What would be the commands to stop the timer at any time, and to restart it at any time. You would turn off the clock source, eg. Off: TCCR2B = 0; // clock stopped
On: TCCR2B = _BV (WGM22) | _BV (CS20); // fast PWM, no prescaler
If you just want a 50% duty cycle some of the other modes (like CTC) might be better. This one uses both "sides" of the timer. I did a bit of copy/paste on that one. <grin>
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 48
|
 |
« Reply #13 on: November 18, 2012, 11:57:28 pm » |
Before you disable it, think hard if you have to. If your pulses needs to be generated absolutely as you had written, without interruption, you then have no choice but to disable interrupts.
Even then there will be glitches at the end of loops, as I pointed out above. He has "no choice" but to use the hardware timers if he wants glitch-free pulses. What frequency are you after? 1 MHz? This sketch does that, nice and simple: const byte LED = 3; // Timer 2 "B" output: OC2B
const long frequency = 1000000; // 1 MHz
void setup() { pinMode (LED, OUTPUT);
TCCR2A = _BV (WGM20) | _BV (WGM21) | _BV (COM2B1); // fast PWM, clear OC2A on compare TCCR2B = _BV (WGM22) | _BV (CS20); // fast PWM, no prescaler OCR2A = (F_CPU / frequency) - 1; // zero relative OCR2B = ((OCR2A + 1) / 2) - 1; // 50% duty cycle } // end of setup
void loop() { // do other stuff here }
Glitch-free output:  Plus you are free to do whatever you want in the main loop, interrupts, serial send/receive, whatever. Thanks Nick! Just woke up  I need pulses of min 250nS or max 400ns. I need to learn how to use your approach though.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 48
|
 |
« Reply #14 on: November 18, 2012, 11:58:21 pm » |
cli (); is needed to Disable Interrupt flag. It doesn't disable interrupt flags: it disable interrupt execution. The flags will be set as they otherwise would be, except that they don't trigger isrs anymore. Before you disable it, think hard if you have to. If your pulses needs to be generated absolutely as you had written, without interruption, you then have no choice but to disable interrupts. yes it disables all Interrupts...
|
|
|
|
|
Logged
|
|
|
|
|
|