Delay calculation

Moderator: Benj

Post Reply
0101
Posts: 50
Joined: Thu Dec 12, 2019 3:36 pm
Been thanked: 1 time
Contact:

Delay calculation

Post 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!

0101
Posts: 50
Joined: Thu Dec 12, 2019 3:36 pm
Been thanked: 1 time
Contact:

Re: Delay calculation

Post by 0101 »

Hi,
Here I get 7.9 KHz and I want to get 50 KHz.
Attachments
delay.fcf
(5.5 KiB) Downloaded 199 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Delay calculation

Post 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.
Attachments
delay PWM.fcf
(6.5 KiB) Downloaded 199 times
Martin

0101
Posts: 50
Joined: Thu Dec 12, 2019 3:36 pm
Been thanked: 1 time
Contact:

Re: Delay calculation

Post 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

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Delay calculation

Post 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.
Martin

0101
Posts: 50
Joined: Thu Dec 12, 2019 3:36 pm
Been thanked: 1 time
Contact:

Re: Delay calculation

Post 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.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Delay calculation

Post 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.
Attachments
delay Timer Interrupt.fcf
(12 KiB) Downloaded 220 times
Martin

0101
Posts: 50
Joined: Thu Dec 12, 2019 3:36 pm
Been thanked: 1 time
Contact:

Re: Delay calculation

Post 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

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Delay calculation

Post 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.
Martin

0101
Posts: 50
Joined: Thu Dec 12, 2019 3:36 pm
Been thanked: 1 time
Contact:

Re: Delay calculation

Post 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

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Delay calculation

Post 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.
Martin

0101
Posts: 50
Joined: Thu Dec 12, 2019 3:36 pm
Been thanked: 1 time
Contact:

Re: Delay calculation

Post 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
Attachments
fc v5.1.png
fc v5.1.png (65.62 KiB) Viewed 6621 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Delay calculation

Post 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.
Martin

0101
Posts: 50
Joined: Thu Dec 12, 2019 3:36 pm
Been thanked: 1 time
Contact:

Re: Delay calculation

Post 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?

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Delay calculation

Post by medelec35 »

Attached so you can check it out.
Attachments
delay PWM Period 47 16F877.hex
(1.03 KiB) Downloaded 146 times
Martin

Post Reply