Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

double count down timer with pic887, need code advice

Status
Not open for further replies.

pasicr

Advanced Member level 1
Joined
Aug 13, 2005
Messages
440
Helped
39
Reputation
80
Reaction score
29
Trophy points
1,308
Location
Macedonia
Activity points
4,213
Hi for all,
I build hardware design for two digit (double) countdown timer,
I need code for double countdown timer with PIC887,
two digit, MAX7219 led driver,
MikroC for Pic IDE,


first value 24 seconds
second value 14 seconds

two buttons, named 24 and 14
when press button 24, start countdown from 24
when press button 24 again, stop counting,
when press button 24 again, continue countdown from last stop value

until 24 countdown, if press button 14,
then start countdown from 14
press 14 button again, stop countdown,
press 14 button again, continue countdown from last stop value

stop counting when reach ZERO

thanks in advance
best regards
 

c_mitra

Advanced Member level 5
Joined
Nov 13, 2012
Messages
3,791
Helped
928
Reputation
1,858
Reaction score
917
Trophy points
1,393
Activity points
29,863
Please try yourself first. Write flowcharts, check and recheck. When you will succeed and understand the idea, you will feel happy.

There is nothing wrong if you do not get it right in the first attempt. You will still learn even if your program fails.

Hint: do it with one counter and one button first.
 

pasicr

Advanced Member level 1
Joined
Aug 13, 2005
Messages
440
Helped
39
Reputation
80
Reaction score
29
Trophy points
1,308
Location
Macedonia
Activity points
4,213
The hardware is based on
hardware.jpg

I make pcb with pic16f887 at 8MHz
IMG_20160131_195724.jpg

I have one display with 2 seven segment digits

Right now I finish code on Mikroc for PIC,
but I will tested tomorrow in my office
the code is attached

I am not sure about interapt routine (button routine)

one button select 24 seconds
one button select 14 seconds
one button for stop/start

press button 24, start counting down immediately,
press button stop/start stop counting at the some value of remaining seconds from 24
press button stop/start again counting down continuous

if press button 14 display show 14 and start counting down from 14
stop/start button with same function like above...

thanks for all...
 

Attachments

  • KODOTmoj1.rar
    23.9 KB · Views: 8
Last edited:

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
15,216
Helped
4,958
Reputation
9,938
Reaction score
4,783
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
129,201
That's an impressive start to your project - well done!

There are a few problems but with only small modifications it will do 'something' even if not quite as you want it. At the moment you are not using any interrupts but it would be wise to do so. I suggest you try writing an interrupt that generates the one second timing, you can then use it to count the numbers down at one second intervals. To do that, you need to add an interrupt routine and configure one of the timers so it overflows and generates an interrupt every second. In the interrupt routine you can reset the timer for the next interval and also decrement the numbers.

Brian.
 
  • Like
Reactions: pasicr

    pasicr

    Points: 2
    Helpful Answer Positive Rating

gamegurus

Full Member level 1
Joined
Jan 14, 2005
Messages
97
Helped
19
Reputation
38
Reaction score
19
Trophy points
1,288
Activity points
632
What is your display size? Is it taking more current than MAX7219 can handle?

In your code, you should rearrange it using a state variable like below. right now there are many logic that can go wrong in the loop.

And the display update you should put a timer interrupt like 100ms to update a global counter variable to MAX7219.
That way you seperate the display update and the logic of your application.

Code:
#define STATE_IDLE 0
#define STATE_24    1
#define STATE_14    2

unsigned char state;
unsigned int counter;

void ISR_TIMER_100_MS()
{
 //   put the counter variable to MAX7219
}

void main()
{
   state = STATE_IDLE;
   while(1)
   {
     switch(state)
    {
       case STATE_IDLE:
         // if(something) 
         // state = STATE_14;
         // change your counter variable if required
       break;
       case STATE_14:
         // if(something) 
         // state = STATE_24;
         // change your counter variable if required
       break;
       case STATE_24:
         // if(something) 
         // state = STATE_IDLE;
         // change your counter variable if required
       break;
    }
   }
}
 
  • Like
Reactions: pasicr

    pasicr

    Points: 2
    Helpful Answer Positive Rating

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
15,216
Helped
4,958
Reputation
9,938
Reaction score
4,783
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
129,201
To explain, a one second interval is too long for most PIC timers when the clock speed is high. The easy fix is to use a shorter interval and keep a count of how many times it occurs. You can often use the different timing intervals to your advantage but in this case you only need to update the display as the numbers change because the MAX7219 holds the segment patterns in it's own memory. So I would trigger the 'count down' routines and the display update from inside the ISR. Something like this:

Code:
void ISR_TIMER_100MS()
{
.... reload the timer so the interrupt occurs again in 100mS time
.....count down the number of times this interrupt was called
.....if the count has reached zero, set it back to 10 and set a flag 'SecElapsed = 1'
}
With the correct values (depends on your clock speed) the variable 'SecElapsed' will take the value 1 once per second (10 x 100mS). Then:
Code:
void main()
{
.....if(SecElapsed == 1)
.....{
........SecElapsed = 0; // reset it, you only want to use it once each second
........count down the 14/24 variables if necessary
........update the display
.....}

Brian.
 
  • Like
Reactions: pasicr

    pasicr

    Points: 2
    Helpful Answer Positive Rating

c_mitra

Advanced Member level 5
Joined
Nov 13, 2012
Messages
3,791
Helped
928
Reputation
1,858
Reaction score
917
Trophy points
1,393
Activity points
29,863
One of the things you should take care is not to nest ISR within one another. As soon as the counter overflows, it sets the interrupt and we enter the ISR. Reload the timer counter, set flags etc and return from interrupt.

Now you can worry about two timers. However, one counter ticking every 1 or 0.1 sec is good enough. You will need to keep two sets of flags for the two times and update them together. That way you will have plenty of time to take care of housekeeping.

Although I have forgotten, you need to set up the PIC with the right flags when you start. Else many things will not work and debugging will be hell.
 
  • Like
Reactions: pasicr

    pasicr

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top