Delay Using Timer1 pic 16f628a + CCS compiler PCM 4.068

Status
Not open for further replies.

raflab

Newbie level 1
Joined
Aug 11, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,288
I'm trying to create a delay with N minutes using timer 1 .. I found the following code
Code:
#include <16F628A.h> 
#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP 
#use delay(clock=4000000) 

//====================== 
void main() 
{ 
int16 delay = 0; 

SET_TIMER1(delay); 

SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_8); 

while(TRUE) 
  { 
   if(GET_TIMER1() <32768) 
      OUTPUT_LOW(PIN_A1); 
   if(GET_TIMER1() >32768) 
      OUTPUT_HIGH(PIN_A1); 
  } 
}

So if is it possible I want to replace the 0.5 sec by 2 minutes ( for example )
( I said N minutes because that's related to the user that he will be able to send it through VB interface using Serial port communication )
I won't use the delay_ms because it will be imprecise
I really need some help !! :-|
Thanks
 

For quartz 4MHz vs TMR1 maximum possible delay is 524,28ms
But you can make variable counter, for example i:


Code:
   while(1) {  
      
     //procedure setting timer1 (tmr1h tmr1l = 62500)
     if(TMR1IF) { i++; TMR1IF = 0;}    // I'm not sure for this in CCS
     
     if (i == N) {                                // where N - time in seconds
                     PIN_A1 = !PIN_A1; 
                     i = 0;
                     }
         
    
      
   }

And best choice for this task - using interrupt
 
Last edited:

    V

    Points: 2
    Helpful Answer Positive Rating
And best choice for this task - using interrupt
Code:
          i++; TMR1IF = 0;
                   if (i == N) {                                // where N - time as a multiple of 524,288 ms
                     PIN_A1 = !PIN_A1; 
                     i = 0;
                   }
Moving the above segment to the ISR lets you multitask - do something else while waiting for the timer to overflow. For that, you also need to set the interrupts, so
Code:
   INTCON = 0xC0; //GIE = 1, PEIE = 1
   PIE1 = 1; //TMR1IE = 1
This enables the interrupt associated.

Hope this helps.
Tahmid.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…