Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Use of the the Count feature

Status
Not open for further replies.
If Count is used in a main loop to continuously update number of incoming pulses, the iterations of the loop are held up for the duration the "pulse-collecting " interval, typically 1000 mSec. This suggests that any operation within the same loop , for example a multiplexing loop, is also held up for that interval with undesirable results. Is there any way in which these secondary operations can be continued without interruption by the count operation? I believe, for example, that there is a feature in Microsoft's Visual Basic called Do Events which facilitates this. Help here will be much appreciated.
 
The use of interrupts, which is what something like Visual Basic basic uses - as does Windows, for it's pseudo multi-tasking.

It's a very common operation with microcontrollers, which have specific implementations of it - these vary considerably though from device to device.
 
Hi Nigel. Thanks for the tip. I've never used interrupts, but note that Oshonsoft's Reference Manal has a briefish section on interrupts with a couple of examples which I can't begin to understand --yet. I'll study these to see if I can make any sense of them, but I'll probably be back I'm afraid.
 
You don't need interrupts for counting.... Most Pic micros will allow you to gate the timer to an incoming train of pulses ..

There is normally a T0CK and a T1CK pin so the count is done in the background.... The downside is the counter only ever counts up!!
 
I'm sorry, I'm not well versed in micros so I don't understand your drift. I'm wondering if maybe I didn't make the problem clear. Specifically I'm trying to produce a frequency counter, the frequency being displayed on a SMA420560 LED display module.
The counter, under "Count," samples the incoming pulses for a second and dumps the number received into a variable. This gives me a direct reading of the source frequency. The value is then processed and for display.

The counter operates in a main loop in which there is a second loop which effectively multiplexes the digits of the display module. Unfortunately, at the beginning of the main loop, the counter is refreshed for a second, during which time the loop is held up, which holds up the multiplexing loop, which extinguishes the display module for that second in every iteration of the main loop. I need to prevent this constant periodic extinguishment. So the loop in which the display code resides needs to continue unhampered by the Count refreshing episodes. My code as it stands is, I'm afraid, a bit lengthy and convoluted, so I'm reticent about inflicting it on you patient guys.
 
Steve, you may have to look at Interrupts to prevent main code "delays". Look into using Timer0 or Timer1 as an interrupt that is PIC clock based, and set it up to count to 1000mS. Most PIC clocks run too fast to make it 1 second with one timer interrupt setup. Set up a interrupt routine, perhaps triggering every 100mS, that counts 10 times to reach 1000mS. Then, look into a pin to interrupt on change (IOC pin), and in that interrupt routine, increment a counter, whether it be a byte, word, or long word. That increment is what you are counting of course. When timer0 interrupts for the 10th time, stop all interrupts, read the value you incremented in the other interrupt routine, and set a simply bit/byte flag for the main routine to check if "time is up". Your main routine can then process the value, then clear the timers and interrupt stored value and re-enable interrupts.
This way, your main routine runs forever with no pauses.
I use the following utility to calculate PIC timer values, including which registers to set. It is not exactly the same register names as Oshonsoft, but very close, you can easily set the equivalent registers. This calculator has an option to show settings in C, Basic or Pascal. It is from another compiler vendor, but seems open to the public:
https://libstock.mikroe.com/projects/view/398/timer-calculator

Next time, it would help if you mentioned what PIC you are using...
 
Thats exactly what I was saying...... The "count" is fed into a timer... Lets say timer 1... The other timer, timer 0 will count seconds... When Timer 0 reaches a certain time, timer1 is read and the count per second is displayed..

There is an example of this in my articles... But as this is in the Oshonsoft area, I take it you want to use Basic!!
 
Steve, you may have to look at Interrupts to prevent main code "delays". Look into using Timer0 or Timer1 as an interrupt that is PIC clock based, and set it up to count to 1000mS. Most PIC clocks run too fast to make it 1 second with one timer interrupt setup. Set up a interrupt routine, perhaps triggering every 100mS, that counts 10 times to reach 1000mS. Then, look into a pin to interrupt on change (IOC pin), and in that interrupt routine, increment a counter, whether it be a byte, word, or long word. That increment is what you are counting of course. When timer0 interrupts for the 10th time, stop all interrupts, read the value you incremented in the other interrupt routine, and set a simply bit/byte flag for the main routine to check if "time is up". Your main routine can then process the value, then clear the timers and interrupt stored value and re-enable interrupts.
This way, your main routine runs forever with no pauses.
I use the following utility to calculate PIC timer values, including which registers to set. It is not exactly the same register names as Oshonsoft, but very close, you can easily set the equivalent registers. This calculator has an option to show settings in C, Basic or Pascal. It is from another compiler vendor, but seems open to the public:
https://libstock.mikroe.com/projects/view/398/timer-calculator

Next time, it would help if you mentioned what PIC you are using...

Thanks for your comprehensive reply; unfortunately I believe you are all crediting me with far more knowledge and experience of micros than I actually possess. I'm sure your post contains the key to answering my need, but I will require time to absorb and assimilate it. I think it's a pity that the problem of a loop being held up by an external pause operation, seems not to have been addressed in the Oshonsoft software. Surely it's not unique? Anyway, thanks again for your help .
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top