Page 1 of 1

TMR1 asynchronous counter...

Posted: Thu Mar 24, 2011 6:13 pm
by saakis
Hi,
Could you help me useing the TMR1 in asynchronous counder, in order to measure a pulse train no more than 2 secs wide and no more than 20 kHz fast? How I can enable it and read the value after the pulses finished?
Thank you!saakis

Posts: 1
Joined: Sat Dec 11, 2010 9:50 pm
Has thanked: 0 time
Have thanks: 0 time
Private message

Re: TMR1 asynchronous counter...

Posted: Fri Mar 25, 2011 12:30 pm
by Benj
Hello,

Would the Digital Frequency counter example be of any use to you?
5. Digital frequency counter.fcf
(13.5 KiB) Downloaded 402 times
Example is part of the main Flowcode v4 examples download.

Re: TMR1 asynchronous counter...

Posted: Fri Mar 25, 2011 5:13 pm
by saakis
No, not realy...I want only the amount of pulses witch it's somthing easier I beleive, but it sould be more acurrate as it happens only once and some times for very short time, maybe 50 msec. That's why I want to work with TMR1 in an asynchronous mode. I was look in the Flowcode4 but in the interrupts there was no mode like that whitch is faster and acurrate( I believe...).
Thank you for your reply!
Regards
Sakis

Re: TMR1 asynchronous counter...

Posted: Fri Mar 25, 2011 6:16 pm
by Benj
I want only the amount of pulses
Do you mean you need to be able to time the duration of an incoming logic 1 input pulse. Yes this should be fairly simple to achieve.

1) reset your count variables
2) wait for the input pulse to go high
3) start a timer interrupt on a fast interrupt frequency
4) inside the interrupt macro increment a count variable
5) in the main program wait for the input to go low
6) stop the timer interrupt and divide the count by the frequency of the interrupt and that should give you the time the pulse was active for. You will probably need a floating point variable to store this result.

Here is a quick program designed for a 16F88 running at 19.6608MHz.
PulseMeasure.fcf
(7.5 KiB) Downloaded 386 times
Maximum resolution with the example is 1 / 9600 = 104.16us
Maximum recordable pulse duration is 32767 / 9600 = 3.4s

Re: TMR1 asynchronous counter...

Posted: Fri Mar 25, 2011 8:49 pm
by saakis
Thank you Benj but I want something more(?) easy. I want the number of pulses. I want the TMR1 as a 16 bit (it's better to run as asyncronous) counter waiting for a string of pulses e.g 2000. After these pulses stops just reading the TMR1 and put the number on the LCD. The flowcode you gave me is also usefull in order to know the lenght of the pulse string... Or if it is not possible to activate the internal counter how can I read the value of an external 16bit counter and convert the binary to decimal? Thank you one more time for your time spenting for me...
Regards
Sakis

Re: TMR1 asynchronous counter...

Posted: Fri Mar 25, 2011 11:15 pm
by medelec35
Hi, Saakis
If it was me I would use a port interrupt to count the number of times port has gone from 0V to 5V, while timer0 or timer1 is used for time out.
I can help you with that.
If the let me know:
Target device & osc type and speed.
Time period in which you want time out to occur if if no more pulses are received.
I will throw something together which may or may not be what your after.

Martin

Re: TMR1 asynchronous counter...

Posted: Sat Mar 26, 2011 1:42 am
by medelec35
Here is my 1st attempt at a no thrills (in case it's not what your after) pulse counter.
I have used a 18F4455 device. So you will need to change the target, config settings etc. to suit your own target device.
LCD plugged into portD. Reset is portb1.
Pulse train is fed into portb0
here's how it works.
When 1st switched on, it is waiting for a pulse train.
As soon as one is detected two things happen.
1) Timer1 is started
2) pulses are counted (up to a maximum of 32766) and display shows total pulses counted so far. when a pulse is detected timer is reset back to 0
As soon as pulses stop, timer is no longer reset.
When timer reaches 2 seconds, no more pulses will be counted, and display shows final amount.
When Switch on portb1 is pressed and then released, all is reset, and again is waiting for first pulse to start the whole process all over again.

Martin

Re: TMR1 asynchronous counter...

Posted: Sat Mar 26, 2011 9:49 am
by saakis
Yes Medelec35 this is what I'm looking after, although it is not working in simulation...Does not reset at all, but I will find out what is the problem. Just a question...the idea with TMR1 in asynchronous mode does not working out? Is that a problem of the flowcode4 or I can't use it at all? Thank you for your help!
Regards
Sakis

Re: TMR1 asynchronous counter...

Posted: Sat Mar 26, 2011 11:05 am
by medelec35
Hi saakis.
It should work with real hardware. Unfortunately simulator does not work with timer1 (sorry forgot about that. I did originally create with timer0, tested with real hardware, and simulation, but changed to timer1 without retesting on smilator). I used timer1 only because that's the timer you mentioned.
Here is a version that will work with Flowcode simulator, It is using timer0 which can be simulated OK.
Reset will not work, until after time out of 2 seconds has occurred. Since Timer1 is not working in the simulator, Time out won't occur, hence reset won't work.
The simulator 2 seconds will not be in real seconds. I will only be in real seconds on your actual hardware.

I have not used timer1 as an asynchronous counter before, just used it to time out of two seconds.

I don't believe TMR1 will work as an asynchronous counter in the way you want it to.

When T1CKI has changed state then Tmr1l will increase by 1, when Tmr1l reaches 255 it rolls over to 0 causing Tmr1h to increase by 1. when Tmr1h reaches 255 then the interrupt is accessed.
Unlike INT interrupt which is accessed each time pulse changes state from 0 to 1. That's why I used port interrupt, instead of timer1 for pulse detection.

Conclusion to use TMR1 as an asynchronous counter you will need to
1)reset timer1.
2)Wait for 1st pulse
3) On receving 1st pulse, start timer0
3)wait for 2 sec time out. whilst pulses are being counted..If pulse detected, reset timer0.
4) After last pulse, if 2 seconds has elapsed, Read value of Tmr1l & Tmr1h.
5) Combined Tmr1l & Tmr1h to a single signed integer.
6) Display results
7) Wait for reset switch to be pressed. if not pressed go to step 7, If pressed go to step 1

Note Steps 1 and 4 will have to be done via a C code block which can't be simulated.

The above can be done if that's really what you want, but both version posted should currently work on real hardware, whereas this attachment as stated should work on simulator and hardware

What would your hardware set up be, so I can adjust flowchart to suit?


Martin

Re: TMR1 asynchronous counter...

Posted: Sat Mar 26, 2011 6:52 pm
by saakis
Hello Martin, I have to thank you one more time for the time to spent for me! I prefer to use the 16f877 as I already have it but any one would be fine. One more question please... how easy is to read an external 16 bit counter as I'm much better in hardware than is software? The only reason to avoid reading these pulses in "software" way and prefer the hard one is that I don't want to loose even one of these pulses
as it is very important for the measurement I want to do...
Best regards
Sakis

Re: TMR1 asynchronous counter...

Posted: Sat Mar 26, 2011 7:13 pm
by medelec35
Hi Sakis,
Your welcome.
Since I have used INT0 interrupt which is hardware based not software. I believe it takes priority, so in theory you should not miss any pulses at all! No software, or no polling method was used at all to detect pulses.
If you have a known amount of pulses you can test the theory. Alternately you could use microchips simulator. That will allow you to produce a stimulus file that will produce a set amount of pulses.
If you want to test all three methods, I will produce the flowchart that is using timer1 in asynchronous mode. Will post it a bit later. Need time to develop and test it.
Just remember it's using C code, and C code does not work in the Flowcode simulator.
Just like timer1, you will need to test in a simulator that works with hex code, or upload code to your target device.
Are you running an ext osc at 19.6MHz e.g using EB006 programmer?

Re: TMR1 asynchronous counter...

Posted: Sat Mar 26, 2011 7:27 pm
by saakis
Hi Martin, I use an HP 488 programmer with 19.66 mHz.
Sakis

Re: TMR1 asynchronous counter...

Posted: Sat Mar 26, 2011 9:20 pm
by medelec35
saakis wrote:I use an HP 488 programmer
Sorry not familiar with that programmer, so will let you sort the configuration of the chip out :p
Here is the version you originally asked for.
A pulse counter using TMR1 asynchronous counter to count pulses.
Tested with 18F4455, and is working with me, manually pressing switch connected to T1CKI pin
Would you mind letting me know out of all three, which one works best.
Thanks.

Martin

Re: TMR1 asynchronous counter...

Posted: Sat Mar 26, 2011 10:55 pm
by saakis
Thank you Martin, I compile the last two but I have the 16F877A righ now available and seems not to work even the LCD display. I will buy on Monday an 18F4455 to work with...
Thank you again for your help!!!
Sakis

Re: TMR1 asynchronous counter...

Posted: Sat Mar 26, 2011 11:13 pm
by saakis
The problem is not in the Flowcods you made for me but something is wrong whith the development board.A simple programm I did, didn't work also and only in RC mode work(?).
The board is like a christmas tree in RC mode, with all this leds blinking around but dark in HS mode...

Re: TMR1 asynchronous counter...

Posted: Sat Mar 26, 2011 11:34 pm
by medelec35
saakis wrote:Thank you Martin, I compile the last two but I have the 16F877A righ now available and seems not to work even the LCD display. I will buy on Monday an 18F4455 to work with...
Thank you again for your help!!!
Sakis
I will covert all flowchart to 16F877A. There should be no need to buy 18F4455. I have only set it at that since that's what is currently connected to my EB006 programmer board. Flowchart should work with most Pic devices ,so long as chip configuration is adjusted accordingly.
Should not be too long as I need to refer to data sheet 1st.

Edit: Here is 1st file for 16F877A. It is the using TMR1 as asynchronous counter.
Try it, and let us know If you still have a problem. If so we should be able to help you get going.
Hardware wise. LCD connected to PortB. Pulse train is connected to port C0, and reset switch is connected to C2 (you can change switch to your desired pin).
Make Sure you don't have the jumpers selected for LCD alternative connections. LVP is not used, so must be in HVP mode
saakis wrote:A simple programm I did, didn't work also and only in RC mode work(?).
The board is like a christmas tree in RC mode, with all this leds blinking around but dark in HS mode...
Make sure SW2 is in XTAL position not RC
If you post the hex file that you created with Flowcode to test board in HS mode, I take a look at it for you.

Re: TMR1 asynchronous counter...

Posted: Sun Mar 27, 2011 8:15 am
by saakis
Good morning Martin! It looks grate! I will play with today and put it in a breadboard to test it, but so far so good...Thank you again Martin!
Best regards
Sakis

Re: TMR1 asynchronous counter...

Posted: Sun Mar 27, 2011 9:32 am
by medelec35
Your welcome.
Sounds like you have got the Development working in XTAL mode?
That's great!
When building on breadboard, don't forget to add a pull up resistor of around 10K between MCLR and VDD. Best not connect directly to VDD

Also when I build prototypes, I Have a 5way header connected to the chip for ICSP (In Circuit Serial Programming)
See:
http://www.matrixmultimedia.com/mmforum ... 05&p=20346
and
http://www.matrixmultimedia.com/mmforum ... 815#p14815

It saves unplugging chip from breadboard, and plugging back into Programmer and back on breadboard each time you want change something on the flowchart.

Re: TMR1 asynchronous counter...

Posted: Sun Mar 27, 2011 10:54 pm
by medelec35
Sakis.

Did you want the other two flowcharts converting for you,
or can you convert them yourself OK?

Re: TMR1 asynchronous counter...

Posted: Mon Mar 28, 2011 1:23 am
by saakis
Hello Martin,
If you can do will be fine, thanks.I will order from farnel the 18f4455 any way because looked faster! I suppose with the ICD you advise me I can make a board with smd and programming this chip on board, right? So in case of bugs I can reprogramming...
Thank you again Martin!!!

Re: TMR1 asynchronous counter...

Posted: Mon Mar 28, 2011 7:30 am
by medelec35
saakis wrote: If you can do will be fine, thanks.
Maybe best if I will wait until your new chip arrives, just in case you choose a different chip.
(See next answer)
saakis wrote: I will order from farnel the 18f4455 any way because looked faster!
In my opinion 18f4455 maybe a bit overkill for what you want.
Before you design your hardware, I would think about not using LCD on portB.
PortB uses hardware interrups, Internal pullups, ADC inputs, ISCP and ICD.
There are smaller devices you could use, which run just as fast.
See:
http://www.matrixmultimedia.com/mmforum ... 40&p=22201
You don't need to use external crystal & caps /resonator if you don't want to.
Internal oscillators work very well these days, and some chips which have PLL can run with an internal osc at 32MHz!
I use internal oscillators just to cut down component count, since they may not be quiet as accurate as crystals, but factory calibrated at about +/-1% accurate have always been good enough for me.
saakis wrote: I suppose with the ICD you advise me I can make a board with smd and programming this chip on board, right? So in case of bugs I can reprogramming...
Thank you again Martin!!!
Sure I can help you with that. Just a small correction. ICD is in circuit debugging, which is a brilliant way to debug your hardware.
This is a quote from Benj
The FlowKit In-Circuit Debugging (ICD) tool is a great new way of debugging your Flowcode v4 embedded programs. It also allows the monitoring / updating of variables in real time which means that communications from other systems such as RS232, CAN, Zigbee etc can be injected and tested without requiring the other communications node to be up and running. The FlowKit comes complete with an E-Blocks adapter and a USB out socket to allow you to connect up an E-Blocks set, or one of our USB reprogrammable devices. As well as these options there are also single pin connections available allowing you to connect just about any 3rd party or custom hardware. Basically if Flowcode v4 can target the hardware then the FlowKit ICD tool can debug it on a Flowchart icon level. As a side note the EB006 v7 can also perform ICD functionality as shown here but is limited to EB006 compatible PIC devices only.
I have Flowkit and EB006V7 and regularly use them both for debugging. Flowkit I use on the Miac.

You are correct. ISCP will allow you to program/reprogram your chip in-situ. Yes very handy for SMD

Martin

Re: TMR1 asynchronous counter...

Posted: Mon Mar 28, 2011 6:11 pm
by saakis
Hi Martin,
I already order some pics from Farnell, 18f4455 included...Yes I know what to mean, it's like to kill a mosquito with a cannon! But this project is made mostly for educational-kill my time-enjoy e.t.c. so some euros more is not big deal having in mind that I just starting with programming and starting the easy way! So for one board or two the cost of acrystal is less than a euro... DHL costs much more!!! It is not so easy to find a larg variety here(Greece) so I'm trying to use what I find and not what I want. Thank you again for your help! In case I need help again can I ask for your help?
Sakis

Re: TMR1 asynchronous counter...

Posted: Mon Mar 28, 2011 8:29 pm
by medelec35
saakis wrote:Hi Martin,
Thank you again for your help! In case I need help again can I ask for your help?
Sakis
Your welcome.
Sure you can ask for help as often as you like.
I will help you where I can.

If I can't help with any advice, there are Forum users with far more knowledge then myself.
So either way help is there for you.

If you want to create a circuit diagram, I can take a look at it for you, if it will help.