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.

Dual counter

Status
Not open for further replies.

bananasiong

New Member
Hi,
I'm doing a dual down counter with a PIC. There is only a set of 7-segment display to display the time. User can select each counter by pressing 2 different button. There're a few more buttons: increment hour, increment minute, reset, start counting down and the two counter selection buttons.
Now I can make a single counter, quite accurate. I've tested to count down for 1 hour.

I'm still thinking of the way to make 2 counter with a single PIC and this is my plan: I've no problem with the increment state, i.e. user can increase the counting time by selecting each counter and pressing the increment buttons. When counter A is counting down and counter B is in increment mode, the delay time of the counter A is 1 second, but at the same time, the delay time for increasing the counting time of the counter B is around 100 ms, to avoid the value from adding two or more when the user press the button only once.
When both counter A and counter B are counting down, the delay time for them is 500 ms instead of 1 s, it is because the instruction is run one by one. So this will make both counter to count down second by second. My only problem is when both counters are in different mode...
I've reserved the interrupt for displaying the 7-segment displays by multiplexing, can I make another interrupt to solve my problem? Any Idea?
Thanks
 
There's only one interrupt vector, and all interrupts use it, which is why your interrupt routine needs to check what causes the interrout and act accordingly.

However, for you purposes I see no need for a second interrupt at all anyway?. Just decrement one counter and increment the other.
 
If counter A is counting down second by second, 1 second is needed for increment a value to counter B, or I should put the checking button part at the delay time?
 
How often is your interrupt for your display? Let's say (hope) it is 1mS. Add an 8 bit counter to your interrupt and whenever it gets to 10 set a flag. Think of this flag as a 100th of a second interrupt. In your main code, when the flag is set, increment or decrement your counters.

If your interrupt happens to be every 234uS then things get a little more complicated. You have to have a 16 bit variable that you add 234 to each interrupt and when it gets to 1000 you set your flag and subtract 1000 from the variable.

Hope that makes sense.

Mike.
 
Sorry but I don't know how to calculate the interrupt rate. When I see flickering, I just reduce the value and this is my setting:
Code:
	bcf	STATUS,   RP0
	movlw	b'00011101'
	movwf   T2CON
	bsf	STATUS,   RP0
	movlw   d'249'
	movwf	PR2
*I'm using PIC16F877A and a 4 Mhz crystal.

Thanks
 
With those settings, you have prescaller set to 4 and postscaller set to 4. You therefore have a time period of 4*4*250 which is 4mS.

To get a hundreth count, you need to count to 2½. Not to easy. So, count twice per interrupt and set your flag when it gets to 5.

Something like,
Code:
	incf	Count,F
	incf	Count,F
	movlw	5
	subwf	Count,W
	btfss	STATUS,C
	goto	NotTime
	movwf	Count
	bsf	Flags,0
NotTime

Mike.
 
The interrupt rate is not really critical to me, as long as the 7-segment display don't be flickering. So I think I'll change it to 20 ms, is it too long? I haven't tried it yet.
It is easier if I put the checking input program in the ISR, but the ISR looked quite long. How many shouldn't the ISR be more than ususlly?

Thanks
 
bananasiong said:
the 7-segment display don't be flickering. So I think I'll change it to 20 ms, is it too long? I haven't tried it yet.
Thanks
Do you see the seven segments flickering when you press & hold the Input buttons?It should keep at a avg rate.very smaller delays can hurt this.
 
Hi,
I have almost done the counter, but the codes are too long, I've simplified it as attached so that won't be confusing you guys.

I have defined a constant named "STATUS_timer", which indicates the status of the counter:
bit 0:display counter 1 if set and display counter 2 if clear
bit 1:set if counter 1 is counting down, else it is 0
bit 2:set if counter 2 is counting down, else it is 0
bit 3:set if counter 1 has finished counting
bit 4:set if counter 2 has finished counting
bit 5:clear if content of counter 1 is 0
bit 6:clear if content of counter 2 is 0
bit 7:not used.


bit 0 is to display
bit 1 and 2 is to tell the PIC when the button is pressed in the main routine, count down start in interrupt routine.
bit 3 and 4: when the counter has finish counting in the interrupt routine, tell the main routine.
bit 5 and 6: just to see whether counter 1 or counter 2 is empty or not. for example, if counter 1 is empty and the 'start' button is pressed, it go to receiver1; if it is not empty and the button is pressed, it starts counting down.


I have found a problem here:
When I turned on the power, it is in the counter 1 routine (defined in initialize), i.e. display counter 1. When I press button to increment counter 1 and press start, the counter start counting, and it is displaying counter 1. Then I press to change to counter 2 (counter 1 is still counting, just display counter 2), when I press the same button to change to counter 1, cannot, it is just showing counter 2. But if I increment counter 2 and press start, then I can change back to display counter 1, and I can see both counter are counting down independently.
This happens vice versa. If I start it with counter 2, the same thing happen, I can't display counter 1 when counter 1 is counting and in the counter 2 routine (counter 2 is not counting)
I have tested a few and got these results:
I can change to counter 1 or counter 2 as I like when both of them are not counting down.
I can change to counter 1 or counter 2 as I like when both of them are counting down.
I can't change to counter 1 which is counting down when I change to counter 2 which is not counting and vice versa.

Can anyone help me with this? Maybe I explaination is not very clear.

Thanks
 

Attachments

  • test.txt
    4.2 KB · Views: 252
Mike said:
I'd like to see the schematic of the hardware that goes with that code.
Yes, here it is. BTW, do you understand what I said? My problem.

Thanks
 

Attachments

  • interface.GIF
    interface.GIF
    15 KB · Views: 264
Your schematic looks ok.

I'm afraid I don't understand what you're trying to do. Is that code example complete? I don't see where you're driving the displays and several other subroutines seem to be missing. What are you displaying, hours and minutes?

You want two independent up/down counters that increment or decrement at what rate? One second? One Minute? Or, manually for each switch press?
 
Last edited:
Hi bananasiong I think you have a menu selecting problem which I understand.
See the attached video. Are you seeking something like that.Its also a dual counter designed by me.
1)automatic & manual mode.
If you select automatic mode its counting automtically.
If you select manual you have to give inputs ......
 
Last edited:
It is actually displaying hour and minute, if the hour is empty, it will display minute and second. This function is already done.

I can increment the counting time by pressing the buttons - increment hour and increment second. Then the count down time second by second.

And, Gayan Soyza, thanks for that, but I can't watch 3gp file.
 
No, not the real clock, but dual counter. User set the time and then start counting, both are working independently.
 
Hi,
I have done the dual counter, and the result is expected but I need more time to test it.
I don't know why it didn't work previously. I have checked and checked the program, there shouldn't be problem. Just now I deleted all the hex file and recompiled again and load the same program into the PIC again, it worked :D

Does anyone know how to increase the brightness of the multiplexed 7-segment displays? Can I ignore the collector resistor since there is already base transistor to control the current flow through the collector.

I have almost done my PCB and found that there are 2 extra IO pins. If I need a ground and don't want to route for so long, can I just use the extra pins to make ground (0)?

Thanks
 
bananasiong said:
Does anyone know how to increase the brightness of the multiplexed 7-segment displays? Can I ignore the collector resistor since there is already base transistor to control the current flow through the collector.

No, you can't ignore the current limiting resistors - the base resistor is nothing to do with it, that's to limit the current from the PIC to the base - the transistors are used as switches, not amplifiers.

If you want to increase the brightness use PNP transistors to feed the segments, with lower value current limiting resistors (modifying the program accordingly). Currently the brightness is limited by what current the PIC can provide.
 
Yes I'm using the PNP transistors.
I thought the collector resistor is to limit the Ic max? As long as the Ic is below Ic max, Ic is controlled by the base current isn't it?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top