Timer functions implemented in FLowcode

Moderator: Benj

Post Reply
DonM
Posts: 12
Joined: Fri Jul 20, 2012 12:21 am
Has thanked: 14 times
Been thanked: 3 times
Contact:

Timer functions implemented in FLowcode

Post by DonM »

Are the Timer0, 1 , and 2 functions going to be implemented in Flowcode in the future?

Thank you,
Don

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Timer functions implemented in FLowcode

Post by Enamul »

Hi,
What do you mean by that as Timer0, 1 & 2 already implemented in Flowcode?
Enamul
Enamul
University of Nottingham
enamul4mm@gmail.com

DonM
Posts: 12
Joined: Fri Jul 20, 2012 12:21 am
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Timer functions implemented in FLowcode

Post by DonM »

Some of the PIC's like the PIC16F690 support timer modules (see PDF "PIC16F631/677/685/687/689/690
Data Sheet" - Page 86 (Section 6.0 - TIMER1 MODULE WITH GATE CONTROL). These appear to be Timer\Counters that can be used for timing events or counting external input pulses. I do not see in Flowcode where these timer modules can be setup and used. Have I over looked something or misread the data sheet?

Thank you,

Don

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: Timer functions implemented in FLowcode

Post by medelec35 »

Hi Don,
here is how to select a timer interrupt in Flowcode:
Selecting Timer.png
(133.14 KiB) Downloaded 8049 times
Your best bet would be to look at Flowcode examples and look how interrupts are used.
You can try here:
http://www.matrixmultimedia.com/lc_index.php?p=7

Also take a look here:
http://www.matrixmultimedia.com/mmforum ... 012#p37574

There are numerous examples using timer interrupts.
You could search for key words like timer interrupts etc.

Martin
Martin

DonM
Posts: 12
Joined: Fri Jul 20, 2012 12:21 am
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Timer functions implemented in FLowcode

Post by DonM »

Thank you for the reply.
I have looked at the interrupt using the timer but what I am trying to do is measure the time between input pulses. I was looking at using the interrupt on the input pin to jump to a routine that would start a timer. When the input pin interrupted again on the next pulse I would read the accumulated value of the timer, save it and then clear the timer and start it again for the next pulse. Is there a way to do this in Flowcode? Or do I have to use "C" or ASM to do it?

Don

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Timer functions implemented in FLowcode

Post by Enamul »

hi,
You can easily implement that in Flowcode. The idea can be to use RB0 int to sense the rising edge of the pulse. when the interrupt occurs you can start TMR0 timer while clearing tmr0 register using c code. Wait for another RB0 int to occur. Measure tmr0 value which will give you pulse duration.
I'll post a program for that.
Enamul
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Timer functions implemented in FLowcode

Post by Enamul »

Hi,
Can you please tell what range of pulse duration you want to measure so that I can decide right timer for the program?
Enamul
Enamul
University of Nottingham
enamul4mm@gmail.com

DonM
Posts: 12
Joined: Fri Jul 20, 2012 12:21 am
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Timer functions implemented in FLowcode

Post by DonM »

I am looking at 2 to 100 Hz range (500 to 10 mS). Timer 1, the 16 bit timer is the timer that would be needed to get the resolution I am looking for.

Thank you,
Don

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Timer functions implemented in FLowcode

Post by Enamul »

hi,
Ok.. I am posting a program shortly.
Enamul
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Timer functions implemented in FLowcode

Post by Enamul »

HI,
I have implemented the program in PIC 16f877a and LCD is used to display pulse duration. LCD is connected in portd and details about LCD connection is in the link below in my post at the bottom...(plz let me know if you can't see that)
http://www.matrixmultimedia.com/mmforum ... 29&t=10967

Pulse input is RB0/INT..as I have used INT for sensing rising edge of pulse..I have set proper config for you.You have to use 4MHz crystal for proper timing...
TMR1 is used for measuring time duration..prescale 8:1..

so at 4MHz, 1us clock cycle which 65536*8 us to overflow TMR1 as 8 prescalar. Interrupt frequency 1.907 Hz so you can measure maximum 524ms.
Please let me if you are not clear..
Enamul
Attachments
Pulse_duration.fcf
Flowcode v5
(15.54 KiB) Downloaded 349 times
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Timer functions implemented in FLowcode

Post by Enamul »

Hi,
I made a mistake in calculation box in the MY_INT macro..

Code: Select all

TM_L = TEMP_TMR1L
TM_H = TEMP_TMR1H
TMR1 = (TM_H << 8) OR TM_L
D = (TMR1 / 1000)*8
please correct that...
This is because 8 presclar..
Enamul
Enamul
University of Nottingham
enamul4mm@gmail.com

DonM
Posts: 12
Joined: Fri Jul 20, 2012 12:21 am
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Timer functions implemented in FLowcode

Post by DonM »

Hi,
I made the correction plus I had to initialize TEMP_TMR1H as I was getting an "Un-initialized" error for this variable.
So in the initialization cal block I have:
TEMP = 0
D = 7
TEMP_TMR1H = 0
TEMP_TMR1L = 0
In the "MY_INT" routine it appears that there should be a macro "MY_TMR1". That macro is empty. Is this correct?
Also, how does the program know what "tmr1l" and "tmr1h" are in the "C" code blocks:
FCV_TEMP_TMR1L = tmr1l;
FCV_TEMP_TMR1H = tmr1h;
and
tmr1l=0x00;
tmr1h=0x00;
Don

DonM
Posts: 12
Joined: Fri Jul 20, 2012 12:21 am
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Timer functions implemented in FLowcode

Post by DonM »

Please find attached the modified program.

Don
Attachments
Pulse_duration.fcf
(15.54 KiB) Downloaded 243 times

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Timer functions implemented in FLowcode

Post by Enamul »

I made the correction plus I had to initialize TEMP_TMR1H as I was getting an "Un-initialized" error for this variable.
As I am using those variable in C code that's why FC showing the error..but that's not a problem..
So in the initialization cal block I have:
TEMP = 0
D = 7
TEMP_TMR1H = 0
TEMP_TMR1L = 0
Can you post the modified pg so that I can check what you have changed....
In the "MY_INT" routine it appears that there should be a macro "MY_TMR1". That macro is empty. Is this correct?
Actually if pulse width less than 500ms, it will never called that's why I left that blank...
Also, how does the program know what "tmr1l" and "tmr1h" are in the "C" code blocks:
FCV_TEMP_TMR1L = tmr1l;
FCV_TEMP_TMR1H = tmr1h;
tmr1l and tmr1h actual register in pic which can be access through C code..but to link FC variable to C code..you have to use "FCV_" prefix...
tmr1l=0x00;
tmr1h=0x00;
This in c code to clear TMR1 registers..
Enamul
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Timer functions implemented in FLowcode

Post by Enamul »

Hi,
Sorry last-time I have edited your post...It seems issue of valued contributor...I supposed to click quote but i clicked to edit. What you have changed is fine..I am now checking why it's not working and try to test in hardware..let you know..
I have noticed one problem in TMR1 properties...plz change that according to figure..
Enamul
Attachments
TMR1.jpg
TMR1.jpg (30.78 KiB) Viewed 14036 times
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Timer functions implemented in FLowcode

Post by Enamul »

Hi,
I have changed the idea completely..please try this version and let me know...
I guess there is priority problem in using two interrupt in FC...I have to look in depth that later on..now just used switch to sense which should be ok for your case.
I have tested that in Real PIC...it works there should work in hardware..
Enamul
Attachments
TMR1.fcf
(15.54 KiB) Downloaded 222 times
Enamul
University of Nottingham
enamul4mm@gmail.com

DonM
Posts: 12
Joined: Fri Jul 20, 2012 12:21 am
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Timer functions implemented in FLowcode

Post by DonM »

The program compiles without error. When I run in emulator the timer value being displayed stays at 0.
I preset the variable "D" to 7 to track if the display changes, which it does go to 0. I also added an LED to see if the subroutine paths are being executed, which they are.
I assume that in the emulator the display should update with a non-zero counter value if I click the button quick enough.
Any ideas?
I have attached the program for your reference.

Thank you,
Don
Attachments
Pulse_duration v1.fcf
(16.14 KiB) Downloaded 203 times

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Timer functions implemented in FLowcode

Post by Enamul »

Hi,
I have tested earlier program in Real Pic simulator which is almost you can say like real hardware test...there is a problem in last post that the time you keep the switch pressed is not counted as portion of pulse..I have changed that..now the macro is called first and so no portion is escaped..but if you want to simulate you have to press really fast so that it doesn't last more than 500 ms. But in real world when the pulse come to the input..you don't need to worry as its high period will be maximum 262ms as this program can work with 524ms max..And you said you need max 500ms which means 250ms max high period..

Code: Select all

tmr1l=0x00;
tmr1h=0x00;
Due to the above code TMR1 counter resets and starts from beginning in first rising edge and in the next rising edge it will retrieve the TMR1L and TMR1H registers which updates the D value to display in LCD. I put an LED in RA0 to show you when you press first time (i.e., when TMR1 re-starts) LED glows ON and when you press second time (i.e., when D updates) LED turns OFF.
I have tested this also in Real PIC Simulator..I am attaching here the simulation result..and the changed program.
You can download the Real PIC simulator from the following link...this is really good but for 30 days as evaluation version..
http://digitalelectrosoft.com/download-pic-simulator
Hope this will help. :)
Enamul
Attachments
SIM.jpg
SIM.jpg (104.72 KiB) Viewed 14023 times
Pulse_duration_v2.fcf
(16.49 KiB) Downloaded 205 times
Enamul
University of Nottingham
enamul4mm@gmail.com

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: Timer functions implemented in FLowcode

Post by medelec35 »

Real Pic simulator is really handy for testing hex files created with Flowcode.
For instance,rather than clicking switch, there is a function generator to produce a clock pulse from 1us to 80 seconds.
Currently generator is set for 10ms which is minimum resolution, If you want to increase resolution, then just move the slider, and select a different range if necessary ( x100000 drop down)
A scope to view logic levels and duration.
If you would like to check what value a register is for e.g TEMP_TMR1H then in Flowcode select Build, View ASM then look for FCV_TEMP_TMR1
and note hex address is 4C
select value.png
(38.13 KiB) Downloaded 7976 times
Load hex file in Real pic simulator, and with visual tab showing, right click on memory map tab.
Real pic sim.png
(227.42 KiB) Downloaded 7976 times
Every time Real pic sim is stopped, current register value can be noted (this includes all SFR e.g Status, Ports, interrupts etc.)

When I get some more time I will create a tutorial for testing flowcharts in real pic sim, and post in Tips & Tricks section.

Martin
Martin

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Timer functions implemented in FLowcode

Post by Enamul »

HI Martin,
Thanks for the nice post...Please post a tutorial about Real PIC Simulator. That will be really useful.
Enamul
Enamul
University of Nottingham
enamul4mm@gmail.com

DonM
Posts: 12
Joined: Fri Jul 20, 2012 12:21 am
Has thanked: 14 times
Been thanked: 3 times
Contact:

Re: Timer functions implemented in FLowcode

Post by DonM »

Please find attached a program that demonstrates how to use the Timer1 16 bit timer in Flowcode to time the interval between input pulses.
The program is commented so look at the detailed descriptions on each block for more information.
The program uses one input and three outputs. One the rising edge of the input the timer is cleared and starts timing. When the input goes high again the accumulated value is stored in a variable and the timer is cleared and starts timing again.
The three outputs are:
(B4) Button pressed toggle - changes state on every button push.
(B6) Timer timed out - The timer is timing with 8us increments. That means that the duration between input pulses can be no more than 1.90738 seconds or the timer register will overflow. This output indicates a timer overflow.
(B5) Freq in range - The accumulated value is compared against a high and low value. If it is within these two values then this output goes ON.
This was developed on the free version on Flowcode and was compiled and downloaded to the PICDEM LAB board using the PIC16F690 chip. It would require a high speed input simulator (see above posts) to test this in the Flowcode emulator.

My thanks to Enamul for his posts on this question. I helped greatly in understanding using "C" code and Timer functions in Flowcode.

Don
Attachments
Pulse_duration_v3.fcf
Example program timing duration between input pulses
(23.35 KiB) Downloaded 263 times

Post Reply