Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 1st February 2007, 02:15 AM   (permalink)
Default Dual counter

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
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 1st February 2007, 06:45 AM   (permalink)
Default

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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline   Reply With Quote
Old 1st February 2007, 11:14 AM   (permalink)
Default

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?
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 1st February 2007, 12:09 PM   (permalink)
Default

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.
Pommie is offline   Reply With Quote
Old 1st February 2007, 12:24 PM   (permalink)
Default

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
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 1st February 2007, 01:01 PM   (permalink)
Default

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.
Pommie is offline   Reply With Quote
Old 2nd February 2007, 12:13 AM   (permalink)
Default

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
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 2nd February 2007, 03:57 AM   (permalink)
Default

Quote:
Originally Posted by bananasiong
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.
__________________
Gayan

My Website
http://gsmicro.blogspot.com/
Gayan Soyza is offline   Reply With Quote
Old 22nd March 2007, 02:26 AM   (permalink)
Default

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
Attached Files
File Type: txt test.txt (4.2 KB, 23 views)
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 22nd March 2007, 04:01 AM   (permalink)
Default

I'd like to see the schematic of the hardware that goes with that code.
Mike, K8LH is offline   Reply With Quote
Old 22nd March 2007, 10:30 AM   (permalink)
Default

Quote:
Originally Posted by Mike, K8LH
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
Attached Images
File Type: gif interface.GIF (15.0 KB, 38 views)
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 22nd March 2007, 10:44 AM   (permalink)
Default

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 by Mike, K8LH; 22nd March 2007 at 11:02 AM.
Mike, K8LH is offline   Reply With Quote
Old 22nd March 2007, 10:45 AM   (permalink)
Default

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 ......
__________________
Gayan

My Website
http://gsmicro.blogspot.com/

Last edited by Gayan Soyza; 7th July 2008 at 02:52 AM.
Gayan Soyza is offline   Reply With Quote
Old 22nd March 2007, 11:05 AM   (permalink)
Default

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.
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 22nd March 2007, 11:10 AM   (permalink)
Default

Hi bananasiong are you going to make a dual time clock?
__________________
Gayan

My Website
http://gsmicro.blogspot.com/
Gayan Soyza is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
How we can do it in Assembly..Help Needed Ayne Micro Controllers 37 29th December 2006 05:49 PM
dual decade ripple counter baftab Datasheet/Parts Requests 1 30th September 2006 12:57 PM
Dual Beam People Counter Problem........ fingers Electronic Projects Design/Ideas/Reviews 7 21st September 2004 12:02 PM
Crude assembly counter Mark Lazarides Micro Controllers 5 28th February 2004 07:22 PM
Query about 4518 Dual 4 bit counter andipi General Electronics Chat 2 12th October 2003 10:03 PM



All times are GMT. The time now is 12:06 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.