Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 22, 2024, 06:45:18 06:45


Login with username, password and session length


Pages: [1]
Print
Author Topic: A lot of timers in the PIC  (Read 3970 times)
0 Members and 1 Guest are viewing this topic.
dragoncool
Guest
« on: April 17, 2008, 04:35:25 16:35 »

Hi guys,

I would like to make a system where I have at least 5 counters and one clock.
I know that because of the clock, the best way is put a external crystal and use TIMER interrupt.
But in the PIC that I will use, there are no more than 3 TIMERS. And I have at least 5 counters!
Could some one help me please?

Thanks a lot,
Joao
Logged
dreamwithin
Guest
« Reply #1 on: April 17, 2008, 04:58:29 16:58 »

you can select PIC24F series, they all have 5 16 bit timers. Or, you can write a group of code to build some soft timers by using one timer ,but that will get lower resolution for each soft timer.
Logged
titi
Active Member
***
Offline Offline

Posts: 230

Thank You
-Given: 3826
-Receive: 834



« Reply #2 on: April 17, 2008, 05:03:02 17:03 »

Hello,

Generally, i use 1 timer using internal clock with interrupt, by example every 1ms.
After i use software counters that i increment every 1ms sec in interrupt routine.
In main program, i can test software counter.
Example if counter1>=100 then do something and put counter1 to 0.
I can use a lot of counter that i can reset when i have use it.
if counter2>=1000 then toggle LED and reset counter2.
Generally it is better to do action in main program and not in interrupt routine to have alway a good period.
It is also possible when the counter is equal to a given value the interrupt routine put a variable to 1 and this variable can be
test and reset in main program to do an action every x time.
You need to write your program for testing the counters in the main loop. 

Best regards.   
Logged
dragoncool
Guest
« Reply #3 on: April 17, 2008, 05:24:24 17:24 »

I think that I understood:

I enter the interruption every 100ms and increment a counter(variable count).
in the main function, I will have something like this.

int count1 = 10;   //will run every 10s
int count2 = 100; //will run every 100s
int count3 = 1*3600; //Will run every hour
int count4 = 2*3600*24; //will run every 2 days

while (true)
{
    if(count>=10)    //If count is 10 seconds
    {
          if(--count1)                 //Call function1
          {
              function_count1();
              count1 = 10;
          }
          if(--count2)                 //Call function2
          {
              count2 = 100;
              function_count2();
          }
          if(--count3)                 //Call function3
          {
              count3 = 3600;
              function_count3();
          }
          if(--count4)                 //Call function4
          {
              count1 = 2*3600*24;
              function_count4();
          }
    }
}

right?

Thanks,
Joao
Logged
zuisti
Senior Member
****
Offline Offline

Posts: 409

Thank You
-Given: 242
-Receive: 780


« Reply #4 on: April 17, 2008, 07:42:43 19:42 »

I enter the interruption every 100ms and increment a counter(variable count)
in the main function, I will have something like this.

int count1 = 10;   //will run every 10s
int count2 = 100; //will run every 100s
int count3 = 1*3600; //Will run every hour
int count4 = 2*3600*24; //will run every 2 days

while (true)
{
    if(count>=10)    //If count is 1 seconds
    {
          count = 0;
          if(!(--count1))                 //Call function1
          {
              function_count1();
              count1 = 10;
          }
          if(!(--count2))                 //Call function2
          {
              count2 = 100;
              function_count2();
          }
          if(!(--count3))                 //Call function3
          {
              count3 = 3600;
              function_count3();
          }
          if(!(--count4))                 //Call function4
          {
              count1 = 2*3600*24;
              function_count4();
          }
    }
}

I think it will better with my red corrections (see above).
zuisti
« Last Edit: April 17, 2008, 07:50:58 19:50 by zuisti » Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC