Use the timer as counter for external pulses

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

Post Reply
PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times
Contact:

Use the timer as counter for external pulses

Post by PicoPuls »

For evaluation I use PIC16F887 at the moment.
It has a 16 bit timer / counter

Is there a general approach in FC7 to pick an input pin for CountUp and an other Pin for Reset ?
That counter should work independent of everything else.
Is there a method to read the counter inside the program execution.

When searching for this I found that FC7 supports "Inputs Encoder Rotary " although PIC16F887
doesn't have any hardware Quadrature Encoder Interface.

If this works it is great and could be a substitute for the counter
The question is how it works. Does an independent counter work
or is it a software counter that will slow down the processor ?
Is it possible to take a look inside the macro and see what it does ?

Best regards
Bengt
Attachments
quad-encode-16F887.jpg
(84.4 KiB) Downloaded 1300 times

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Use the timer as counter for external pulses

Post by LeighM »

Hi,
The Quadrature Encoder is a software component, that maintains a readable counter.
You would need to call CheckForChanges regularly in a loop as this updates the counter on change detection.
So it depends on your pulse rate (and width) how often you would need to call this.
Alternatively you could use an input or timer interrupt and call CheckForChanges in the interrupt routine.

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times
Contact:

Re: Use the timer as counter for external pulses

Post by PicoPuls »

Thanks for reply.
If only count up is needed,
does Fc7 support any of these Peripheral Features ?
(preferred 16 bit )

1. TimerO: 8-bit Timer/Counter with 8-bit

2. Enhanced Timer:
- 16-bit timer/counter with prescaler
- External Gate Input mode
- Dedicated low-power 32 kHz oscillator

3. Timer2: 8-bit Timer/Counter with 8-bit Period Register, Prescaler and Postscaler

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Use the timer as counter for external pulses

Post by LeighM »

Hi
We don't have any counter components.
But the device timer/counters can be setup using a C icon.
For example, the 16F887 datasheet has this note about Timer0..
When used as a counter, the Timer0 module will
increment on every rising or falling edge of the T0CKI
pin. The incrementing edge is determined by the T0SE
bit of the OPTION register. Counter mode is selected by
setting the T0CS bit of the OPTION register to ‘1’.
So this would be the C code to set up TMR0 to count pulses on T0CKI (RA4)

Code: Select all

OPTION_REG |= 0x20;		// Set T0CS
OPTION_REG |= 0x08;		// Set PSA (no prescaler)

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times
Contact:

Re: Use the timer as counter for external pulses

Post by PicoPuls »

Thanks,

How to read inside a the program-loop and reset with a pin ?

I am not a C-programmer.
Are there any working examples
"C in Flowcode"
that can be tested and studied
as a starting point ?

Best regards
Bengt

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Use the timer as counter for external pulses

Post by LeighM »

Hi,
Try the attached.
(Change the target device to your 16F887)
Regards,
Leigh
Attachments
PIC16F1937 COUNTER.fcfx
(5.54 KiB) Downloaded 548 times

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times
Contact:

Re: Use the timer as counter for external pulses

Post by PicoPuls »

Thanks indeed for PIC16F1937 COUNTER.fcfx
It would be great if that can work,

I tested with right circuit
a PIC16F1937
and simplified by removing the reset just to get a first indication.

On my testbench I already have a CMOS COUNTER 74HC4040 that delivers pulses to all Port A inputs
That should give TOCI (RA4) enough pulses to work on

I tested output on port B (as in source) and then changed to port D

Unfortunately I am not getting the expected functionality yet - just hoping that perhaps only a small detail is missing.

Would it also be possible to reset the counter directly on the hardware without using the program flow ?
If yes, it will be possible to use very high count rates and very short reset pulses
Attachments
noreset.jpg
noreset.jpg (27.99 KiB) Viewed 12729 times

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Use the timer as counter for external pulses

Post by QMESAR »

I tested output on port B (as in source) and then changed to port D
Unfortunately I am not getting the expected functionality yet - just hoping that perhaps only a small detail is missing.
I am not sure what you are asking however if you are talking about the Flowchart you posted and you have no output on PORTD
then dont you think you should read the timer and move the value to PORTD to be able to have an output on PORTD as the example code does
Would it also be possible to reset the counter directly on the hardware without using the program flow ?
I would suggest you to make an effort to read the datasheets of the devices you are using as most of your questions are questions that are described in the device datasheet
any case any timer will automatically roll over meaning when it reached it's maximum bit count (8 bit timer will go to zero(0) when 255 is reached and a 16 bit timer will roll over at 65535 ) to answer your question if your application needs to reset the counter before Roll over then there is only one option a SW reset
it will be possible to use very high count rates and very short reset pulses
it depends on what you mean with high count rates
Attachments
1.JPG
1.JPG (17.51 KiB) Viewed 12728 times

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times
Contact:

Re: Use the timer as counter for external pulses

Post by PicoPuls »

Now it works,

This is the true power of Flowcode that beginners and dummies can create great and awesome things,
I am deeply impressed.
Thanks for valuable points of view and all the friendly comments.

This paves the road for much higher count frequency than a software solution.
10 - 50 MHz expected, depending on chip. I guess a 1000 times higher count frequency than software based.
Only my short reset pulse is still a problem.

This can halfway replace my real goal "The Quadrature Encoder" It is in fact a QEI that only counts forwards.

In the beginning of this post I understood that
in PIC16F887 the Quadrature Encoder is a software component, that maintains a readable counter.

What's about dsPIC33EP512MU810. It has two hardware 32-bit Quadrature Encoder Interfaces (QEI)
Flowcode has two such QEI components for that very chip. Are they based on the hardware or are they the same software component as in QEI-less chips ?
That answer determines if Flowcode can make dsPIC33EP512MU810- QEI work with 50 kHz or 50 MHz,

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Use the timer as counter for external pulses

Post by QMESAR »

What's about dsPIC33EP512MU810. It has two hardware 32-bit Quadrature Encoder Interfaces (QEI)
Flowcode has two such QEI components for that very chip. Are they based on the hardware or are they the same software component as in QEI-less chips ?
Too my knowledge the QEI component in FC is a pure software implementation regardles PIC dsPIC ,PIC32, AVR or ARM micro's

PicoPuls
Posts: 111
Joined: Wed Mar 02, 2016 11:26 am
Has thanked: 43 times
Been thanked: 22 times
Contact:

Re: Use the timer as counter for external pulses

Post by PicoPuls »

Just what I guessed. Good to have in confirmed.

Flowcode's awesome focus is to simplify technology.

Maybe it is possible (sometime soon? ) to simplify also the hardware timers and
the Quadrature Encoder Interface to make them real easily accessible.

Without this simplification layer it seems extremely complicated to read the documentation
and use those valuable features quickly and efficiently.
I have even asked a C-compiler vendor. They confirmed that they had no library function for these purposes.

Regards
Bengt

User avatar
AbhijitR
Posts: 299
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 280 times
Been thanked: 79 times
Contact:

Re: Use the timer as counter for external pulses

Post by AbhijitR »

Hello! LeighM

Thanks for your below, previous post and Flow Chart (PIC16F1937 COUNTER.fcfx).

Re: Use the timer as counter for external pulses
Postby LeighM » Fri Nov 18, 2016 3:03 pm

Hi,
Try the attached.
(Change the target device to your 16F887)
Regards,
Leigh
ATTACHMENTS
PIC16F1937 COUNTER.fcfx
(5.54 KiB)


I tried your flow chart to count the external pulses on RA4 pin using PIC16F1938 and it works perfect, I tried to modify your program to use with PIC18F46K22, but unfortunately it dint work, kindly excuse me but i am not at all good with "C"

I will be happy if someone could shed some light on how to configure the PIC18F46K22 to work as a counter. I tried to read few post on this topic but did not find or may be i did not understand, as i said i am not good with "C".

Thanks in advance.

Regards
Abhi
Attachments
Counter_18F46K22_RA4_15-10-2017.fcfx
(4.22 KiB) Downloaded 259 times

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Use the timer as counter for external pulses

Post by LeighM »

Hello Abhi,
Try this ...
Attachments
Counter_18F46K22_RA4_15-10-2017 LM.fcfx
(8.12 KiB) Downloaded 281 times

User avatar
AbhijitR
Posts: 299
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 280 times
Been thanked: 79 times
Contact:

Re: Use the timer as counter for external pulses

Post by AbhijitR »

Hello! LeighM

First of all many thanks for your answer.

I tried to compile the modified FC file but unfortunately it dint compile, i received the below message.

Target folder: E:\FLOWCO~1\ENCODE~1\COUNTE~3
Source name: E:\Flowcode Programs\Encoder_Trials\CounterfromLeigh\Counter_18F46K22_RA4_15-10-2017 LM.fcfx
Title:
Description:
Device: PIC.18F.18F46K22
Generated by: Flowcode v6.1.4.0
Date: Wednesday, October 18, 2017 17:50:24
Users: 1
Registered to: Abhijit_RASHINKAR
Licence key: Y02253
http://www.matrixtsl.com
Launching the compiler...
C:\Program Files\Flowcode 6\compilers\pic\boostc\boostc_18F.exe -v -t PIC18F46K22 "Counter_18F46K22_RA4_15-10-2017 LM.c"
Launching BoostC...
..................................................................
Completed BoostC compilation, return = 1
C:\Program Files\Flowcode 6\compilers\pic\boostc\boostc_18F.exe reported error code 1
FINISHED


Just for your knowledge i am a FC6 user, is it because of that.

Kindly excuse to bother you again, but i hope you wont mind to check if i am making some mistake.

Thank you.

Regards
Abhi

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Use the timer as counter for external pulses

Post by LeighM »

ah, right, sorry.
Yes you need to use t0con and tmr0l

User avatar
AbhijitR
Posts: 299
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 280 times
Been thanked: 79 times
Contact:

Re: Use the timer as counter for external pulses

Post by AbhijitR »

LeighM

My friend, it works exactly the same what i expected, many many thanks, cheers....

Regards
Abhi

Post Reply