CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Another problem with timer1 - PIC16F913

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pasini



Joined: 12 Dec 2005
Posts: 50
Location: Curitiba - Brazil

View user's profile Send private message Send e-mail

Another problem with timer1 - PIC16F913
PostPosted: Mon Dec 08, 2008 10:36 am     Reply with quote

MPLAB 8.10 / CCS 3.223 / PIC16F913

Hi I have a PIC 16F913 with 32kHz crystal. I want the timer1 to be interrupted every 250ms to verify PIN status and count 4ticks to make 1second. If I set the timer1 clock to be the same as the internal clock, the MCU is not interrupted, whereas if I set it to external clock it is interrupted. Timer1 is also a base clock for the LCD. Timer1 must run during sleep, and wakeup the PIC.
The problem seams to be with T1CON. What I am doing wrong ?

My code is:
Code:

#int_timer1
void isr_timer1()
{
   set_timer1(64680); // 250ms

   deb = deb << 1;
   if(!input(PIN_A0))
      deb |= 1;
   if(deb)
   {   msec++;
      if(msec>=4) //32)   
      {   msec=0;
         seg++;
         if(seg>59)
         {   seg = 0;
            min++;
            atual=0;
//            GravaHorario();
         }
         if(min>59)
         {   min = 0;
            hora++;
      //      GravaHorario();
         }
//         if(hora>99999)
//            hora=0;
            
//         atual = 0;
      }
   }

}


Inside main routine:

Code:


   set_timer1(64680); //250ms
   //T1CON = 0b10001111; // pre-scaler=1:1
   //T1CON = 0b10101111; // pre-scaler=1:4
   T1CON = 0b10111111; // pre-scaler=1:8

   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);


Thanks for any help,
Paisni
Ttelmah
Guest







PostPosted: Mon Dec 08, 2008 10:56 am     Reply with quote

First your count.
Are you sure you have a '32KHz' crystal, not 32.768KHz. The latter is the 'standard' for watch crystals.
Then:
Timer runs off 32768Hz/8.
4096 counts per second.
1/4 second = 1024 counts.
Preload required is 65536-1024 = 64512

Then, use the CCS functions to setup the timer.
setup_timer1(T1_EXTERNAL|T1_CLK_OUT|T1_DIV_BY_8);

Set this _before_ you preload the counter. Otherwise the counter may have advanced much faster than expected in the intervening instructions.

To wake the chip up from sleep with the timer, it _must_ run from the external clock. The internal clock _stops when the chip sleeps. This is why you won't get the interrupt, if you use the internal clock.

Add your offset to the timer value, rather than just loading it. Wake up from sleep, takes quite a ong time, since the chip waits for the master oscillator to stabilise, before waking. Hence the timer may already have counted, before your reach the ISR.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group