Page 1 of 1

Delay calculation

Posted: Sun Jul 12, 2020 5:44 pm
by 0101
Hello everyone,
I use 16f877a and a 20 MHz crystal. I have to calculate the delay for 20uS. How do I calculate the delay at 20 MHz. Any calculation method is welcome.
Thank you!

Re: Delay calculation

Posted: Mon Jul 13, 2020 10:21 am
by 0101
Hi,
Here I get 7.9 KHz and I want to get 50 KHz.

Re: Delay calculation

Posted: Mon Jul 13, 2020 10:53 am
by medelec35
Hi 0101,
You will be better off using the PWM (within Mechatronics).
Set Prescaler to 1 & period register to 99.
The output will be from CCP1 (pin17).
I believe the PWM is available in the free version?
Alternatively you can use a timer interrupt.
The delay macro is not recommend for short accurate delays.

Re: Delay calculation

Posted: Mon Jul 13, 2020 11:24 am
by 0101
Hello, Mr. Martin
It works perfectly and thank you for that, the demo version has PWM. I would like to know how to calculate, why in the loop I have a delay of 10 ms. I also want to know how to calculate a timer interrupt.
:D :D :D :D :D

Re: Delay calculation

Posted: Mon Jul 13, 2020 12:02 pm
by medelec35
You're welcome,
Glad it works the way you want it to.
The delay is to prevent Simulation running too fast and locking up.
I also believe it could prevent hardware doing strange things if not terminating with a loop.
The delay value is not important, so I have chosen a random value.
I did not calculate the value, although there is a formula for doing that.
What I did was just selected up/down arrows to change Period register within properties, until required value.
Although I used a duty cycle for 50, to get 50% this value will depend on period register settings.
The period register is the required value to get a duty of 100%
In this case the period register is 99, therefore to get 50% it's 99/(100/50) = 50 to 0 decimal places.
20 % duty = 99/(100/20) = 20 to 0 decimal places.

If the period register is 60 then for a 50% duty = 60/(100/50) = 30
20 % duty = 60/(100/20) = 12 to 0 decimal places etc.

Re: Delay calculation

Posted: Mon Jul 13, 2020 12:24 pm
by 0101
Hello, Mr. Martin
I now understand from the calculation examples. You can also give me an example of calculation for timer interrupt ?
:D :D :D :D :D
Where do I find the option to show appreciation?
If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

Re: Delay calculation

Posted: Mon Jul 13, 2020 1:19 pm
by medelec35
Timer interrupts allow you to perform a task at precise timed interval,
irrespective of what else is going on within the code.
To get a pin to generate a fixed frequency, you need to toggle the pin at twice the desired frequency.
So for 50 KHz, an interrupt frequency of 100KHz is required.
Values for timer 2 to generate 10uS delay can be found by loading spreadsheet found here.
0101 wrote:
Mon Jul 13, 2020 12:24 pm
Where do I find the option to show appreciation?
The Matrix TSL server have undergone a recent upgrade.
It has caused an issue with the thanks.
See here.

Re: Delay calculation

Posted: Mon Jul 13, 2020 2:16 pm
by 0101
Hello, Mr. Martin
Thanks a lot, now I have to study. I want to ask you why under 49 when setting the register period, the frequency drops to about 51 KHz, up to 100 KHz (register period adjustment) from 99 to 49 is ok, at 48 the frequency is 51 KHz.
:D :D :D :D :D

Re: Delay calculation

Posted: Mon Jul 13, 2020 3:56 pm
by medelec35
Not too sure what you are asking?
The higher you set the period register the lower the frequency produced.
Also the higher the period, the better the resolution ,hence more duty control.
If the period is fairly low with 1 prescaler setting, then see if you can get the same frequency with a different prescaler & a higher period.

Re: Delay calculation

Posted: Mon Jul 13, 2020 4:54 pm
by 0101
Hello, Mr. Martin
I don't think I was explicit enough, I'm sorry. I played with the settings from the register period and I discovered that at 49 I got 100 KHz, and at 48 I get 51 KHz. In the flowcode it shows me 102 KHz for 48 set to the register period. The settings are: 1 in prescaler, 49 in period register and
also 1 in prescaler and 48 in period register.
:D :D :D :D :D

Re: Delay calculation

Posted: Mon Jul 13, 2020 7:07 pm
by medelec35
Hmm, That's odd.
Just tried with 16F1937 and with period of 47 to 50, getting around 100KHz frequency.
Bug with silicon?
Unless its a Flowcode version difference. I'm running v5.5.2.1
If its a bug with your version of Flowcode, then sorry v5 is no longer supported.

Re: Delay calculation

Posted: Tue Jul 14, 2020 6:01 pm
by 0101
Hello, Mr. Martin
It really seems strange, I have the 5.1 demo version and I want to ask you if I use a delay of 1 s for a stopwatch or clock, will it delay 1 s? That's what I'd like to know to calculate a delay for a clock or a kitchen timer for example.
:D :D :D :D :D

Re: Delay calculation

Posted: Tue Jul 14, 2020 6:11 pm
by medelec35
The delays are only software driven, so are not recommended for any accurate timing.
For that you will need to use interrupts or a real time clock external component.
As you have a demo version, you won't have access to the communication protocols to drive RTC component.

Re: Delay calculation

Posted: Tue Jul 14, 2020 6:13 pm
by 0101
Does v5.5.2.1 work well? If I set for example 47 register period and 1 prescaler with 20 MHz clock speed, the frequency generated in pin 17 at pic16f877a is 104 KHz?

Re: Delay calculation

Posted: Tue Jul 14, 2020 6:22 pm
by medelec35
Attached so you can check it out.