Inquiry about Increasing Interrupt Frequency for PWM Pulse Width Reading and Stepper Motor Control

For general Flowcode discussion that does not belong in the other sections.
Post Reply
bazkhf
Posts: 20
http://meble-kuchenne.info.pl
Joined: Sun Jan 14, 2024 10:15 am
Has thanked: 5 times
Been thanked: 2 times

Inquiry about Increasing Interrupt Frequency for PWM Pulse Width Reading and Stepper Motor Control

Post by bazkhf »

Greetings,

I would like to inquire about how to enable an interrupt at a frequency higher than 62500 Hz on the Arduino Nano, as I need a speed of 1 MHz.

In most software tools like Flowcode, it is not possible to achieve a frequency higher than 62500 Hz due to the limitations imposed by the Prescaler.

However, I need to enable an interrupt to read the PWM pulse width signal so that I can control a stepper motor using this signal.
I was forced to use interrupts to make the stepper motor move forward and backward without waiting, which is essential.
But with an interrupt frequency of 62500 Hz, there will be an error of up to 16 microseconds, as the interrupt will occur every 16 microseconds, which affects the accuracy of the PWM signal reading.

Is there a way to achieve this frequency by modifying the timer settings in the code, or are there any special techniques that can be used to accomplish this?

Any advice or detailed steps would be greatly appreciated.

Thank you.

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: Inquiry about Increasing Interrupt Frequency for PWM Pulse Width Reading and Stepper Motor Control

Post by mnfisher »

No, is the easy answer - at 16mhz there won't be much time to do anything in the interrupt handler..

There are options. One is to use the chip capture module to time pulses. See https://flowcode.co.uk/forums/viewtopic.php?t=2890 for an example using a pic mcu. The Atmega 328p has one capture unit - see the datasheet.
It is also possible to get a more accurate time (increments at 8mhz - but you can just read this)

16us isn't very long - the motor should appear continuous to an observer? Using a faster clocked mcu would be another option

Martin

bazkhf
Posts: 20
Joined: Sun Jan 14, 2024 10:15 am
Has thanked: 5 times
Been thanked: 2 times

Re: Inquiry about Increasing Interrupt Frequency for PWM Pulse Width Reading and Stepper Motor Control

Post by bazkhf »

Hi Martin,

Thank you for your response.

I checked the link you provided, but I couldn't understand how the timestamps for the rising and falling edges are determined to accurately calculate the time difference between them. I will attach the file where I have enabled the interrupt, but I only need to know how to determine the start and end times accurately in order to calculate the pulse width.

Could you clarify how the capture unit records the timestamps for both the rising and falling edges? It would be helpful if you could provide an example or additional explanation on this.

Best regards,
Bilal
Attachments
interrupt_pwm.fcfx
(8.26 KiB) Downloaded 11 times

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: Inquiry about Increasing Interrupt Frequency for PWM Pulse Width Reading and Stepper Motor Control

Post by mnfisher »

Using the capture module is fairly straightforward.
See page 111 of the datasheet.

I wrote a very small program that measures the time between rising pulses (ICES1 = 1) using no prescaler on timer1. The prescaler and edge are set in the interrupt enable code - and it possible to toggle the direction in the interrupt handler to time 'pulses' if required.
Note that the pre-scaler needs to be chosen so that times measured are held in a 16bit value (although this can be altered on the fly if needed)
The signal is connected to pin ICP1 (D8).

As here - 10kHz square wave gives a count of 1546, 20kHz 745 and 40kHz 345 (so not quite 'linear')

Martin

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: Inquiry about Increasing Interrupt Frequency for PWM Pulse Width Reading and Stepper Motor Control

Post by mnfisher »

I think the reason the response is not linear - is that the time spent in the ISR becomes more significant at faster rates.

It might be necessary to disable/re-enable interrupts around the output routines - I haven't teted it though. It managed to time a 50Hz - 80kHz signal, as is.

Martin

bazkhf
Posts: 20
Joined: Sun Jan 14, 2024 10:15 am
Has thanked: 5 times
Been thanked: 2 times

Flowcode v10 Re: Inquiry about Increasing Interrupt Frequency for PWM Pulse Width Reading and Stepper Motor Control

Post by bazkhf »

Hi Martin,

Thank you for your response and for modifying the Flowcode file. I didn’t make any changes to the file; I only tried to convert it to a HEX file to upload it to the Arduino Uno (ATmega328P), but Flowcode gave me an error during compilation, and the process did not complete.

Here is the error message I received:

Code: Select all

Target folder: C:\Users\PC_ 11\Downloads
Source name:   C:\Users\PC_ 11\Downloads\atmega328p capture.fcfx
Title:         
Description:   
Device:        ARD.ATMEGA.ATMEGA328P
Generated by:  Flowcode v10.1.2.40
Date:          Tuesday, February 25, 2025 11:48:42
Users:         1
Registered to: bazkhf (FREE_VERSION)
Licence key: DF7JCQ
   NOT FOR COMMERCIAL USE
https://www.flowcode.co.uk
Using FCD settings for compiler
Launching the compiler...
C:\ProgramData\MatrixTSL\FlowcodeV10\FCD\AVR\batchfiles\avra.bat  "C:\Program Files (x86)\Flowcode\Common\Compilers\avrv5\" atmega328p "C:\Users\PC_ 11\Downloads\atmega328p capture.elf" "C:\Users\PC_ 11\Downloads\atmega328p capture.c" "C:\Users\PC_ 11\Downloads\atmega328p capture.lst"

C:\Users\PC_ 11\Downloads>REM CRC: EA277D0C7EF9B4E537B79187D0BE7A25FA6080B4B01BE48BAF713429E4B690411241251B4A05C2B5828F4452C9F7348705C640EF477B79E0C7881D2E5EEC9BD69A0D46BD3B6AACB92DE4D84759EBB5079CA9DA2D2BBCDAE2A4D1C56CD1580BF5A24465F11D9C3B2DBA3F4E37F13004EE3637760F1F0B7DA0CC623CD30E3C7D6BAB4C3FE6151D92B8EC0ADD55E96639DAC02472BDE3806EF43AE3A028BD32A5DAE68BB191DA97CB6883A66D3C41849952BB2DD264AF32733E2C3050DA273BE4EE1FA30153290F2FAFBD2259D77AD477809E9C4D76400384BA5D2E1919BC6D834D57C406BC4A49D3F1 

C:\Users\PC_ 11\Downloads>REM REVISION: 1.0 

C:\Users\PC_ 11\Downloads>REM GUID: EA0828B2-5C70-41A5-8C54-F359DD5CC88B 

C:\Users\PC_ 11\Downloads>REM DATE: 08\08\2022 

C:\Users\PC_ 11\Downloads>REM DIR: FCD\AVR\batchfiles\avra.bat 

C:\Users\PC_ 11\Downloads>"C:\Program Files (x86)\Flowcode\Common\Compilers\avrv5\bin\avr-gcc.exe" -mmcu=atmega328p -Os -ffunction-sections -fdata-sections -funsigned-char -o "C:\Users\PC_ 11\Downloads\atmega328p capture.elf" "C:\Users\PC_ 11\Downloads\atmega328p capture.c" -lm -Wl,-gc-sections 
C:\Users\PC_ 11\Downloads\atmega328p capture.c: In function 'main':
C:\Users\PC_ 11\Downloads\atmega328p capture.c:2294:3: error: 'TCCR1Bbits' undeclared (first use in this function)
     FCD_047b1_UART1__SendNumber(FCV_CAPTURE_VALUE);
   ^
C:\Users\PC_ 11\Downloads\atmega328p capture.c:2294:3: note: each undeclared identifier is reported only once for each function it appears in

Error returned from [avr-gcc.exe]

C:\ProgramData\MatrixTSL\FlowcodeV10\FCD\AVR\batchfiles\avra.bat reported error code 0x1

Autoclose turned off


FINISHED
I’m not sure what’s causing the issue. Do you have any idea how to fix it?

I appreciate your help, and thanks in advance for your time.

Best regards,
Bilal

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: Inquiry about Increasing Interrupt Frequency for PWM Pulse Width Reading and Stepper Motor Control

Post by mnfisher »

Sorry - was playing around with some ideas at the time:

Try:
Attachments
atmega328p capture.fcfx
(11.63 KiB) Downloaded 9 times

bazkhf
Posts: 20
Joined: Sun Jan 14, 2024 10:15 am
Has thanked: 5 times
Been thanked: 2 times

Re: Inquiry about Increasing Interrupt Frequency for PWM Pulse Width Reading and Stepper Motor Control

Post by bazkhf »

Hi Martin,

Thank you very much for providing me with the file. I tested it, and it works perfectly.

I noticed that the code enables the internal counter in the Arduino Uno and also activates the interrupt on both the rising and falling edges. In this code, the time is measured between the rising edge and the falling edge, and also between the falling edge and the rising edge.

Is there a way to make it measure the time only between the rising edge and the falling edge without measuring the time between the falling edge and the rising edge? I need this measurement to determine the pulse width of the PWM signal.

Thank you again for your support and continuous help.

Best regards,
Bilal

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: Inquiry about Increasing Interrupt Frequency for PWM Pulse Width Reading and Stepper Motor Control

Post by mnfisher »

It is possible to just measure the 'pulse' time.

The code above interrupts on a rising edge (not rising and falling) - but it is possible to toggle the direction (TCCR1B.ICES1) - and measure the time from rising to falling edge (or vice versa)

I added a 'toggle' to the interrupt handler - and also record the time 'low'.

Using a 10kHz PWM signal - with a pulse width of 80uS (& low 20uS) I get 1212. 250. Swapping the pulse width to 20uS I get 251, 1212.


I used a variable (rising) to show current direction - it would probably be slightly better to read the current value of ICES1 (but slightly slower). I also disabled the capture interrupt around the time 'display' code - at 100kHz 2uS pulse I get 59, 123 and 8uS pulse 123, 59 :-)

Martin
Attachments
atmega328p capture.fcfx
(14.38 KiB) Downloaded 9 times

bazkhf
Posts: 20
Joined: Sun Jan 14, 2024 10:15 am
Has thanked: 5 times
Been thanked: 2 times

Re: Inquiry about Increasing Interrupt Frequency for PWM Pulse Width Reading and Stepper Motor Control

Post by bazkhf »

Hi Martin,

Thank you so much for your help and for providing the modified code. I will be testing it soon.

However, I would like to let you know that the code has become more complex for me, as I am a beginner in this field. Nevertheless, I will try to understand it and give it a go.

Thanks again for your continued support, and I truly appreciate your assistance.

Best regards,
Bilal

Post Reply