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 CCS Technical Support

Using Timer1 of 16F877A to count seconds

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



Joined: 05 Jun 2012
Posts: 10
Location: Sri Lanka

View user's profile Send private message

Using Timer1 of 16F877A to count seconds
PostPosted: Tue Jun 12, 2012 10:48 am     Reply with quote

I want to count seconds using timer1 of 16F877A. I used 4 as the prescaler. So the number of interrupts per second = 20000000/(4x4x65536).
To check whether this works, I wrote the following code.

Code:

#include <16F877A.h>
#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#define INTS_PER_SECOND 19         // (20000000/(4*4*65536))


int int_count;         // Number of interrupts left before a second has elapsed
int16 seconds = 0;

void setTimer()
{
   OUTPUT_HIGH(PIN_B5);
   int_count=INTS_PER_SECOND;
   set_timer1(0);
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);

}

#int_timer1
void clock_isr() {

    if(--int_count==0)
    {
      ++seconds;
      int_count=INTS_PER_SECOND;
    }

}

void main()
{
   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_DIV_32);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL);

   enable_interrupts(INT_TIMER1);

   OUTPUT_B(0);

   SET_TRIS_B(10000000);

setTimer();
 while(1)
   {

   while(seconds==1)
      {
         OUTPUT_HIGH(PIN_B1);
      }
   }
 }


But I can't see PIN_B1 becoming high when a second is elapsed.

I used this very same code to count seconds using timer0 and it worked.
Can someone please tell me what's wrong here?
_________________
"Silence is argument carried out by other means"
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Jun 12, 2012 11:07 am     Reply with quote

you never reset "seconds"... so in order for this to happen:

Code:
   while(seconds==1)
      {
         OUTPUT_HIGH(PIN_B1);
      }


you will have to wait 254 seconds... EDIT: its a 16bit variable... so 65534 (18+hours)

and then... even when it does happen again... you wont notice.. because you never bring PIN_B1 back low...

as a word of advise... leave TRIS alone... delete that line.

G
_________________
CCS PCM 5.078 & CCS PCH 5.093


Last edited by Gabriel on Tue Jun 12, 2012 11:16 am; edited 2 times in total
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Jun 12, 2012 11:11 am     Reply with quote

Code:
#int_timer1
void clock_isr() {

    if(--int_count==0)
    {
      TOGGLE_PIN(PIN_B1)                <------------- that would be better.
      int_count=INTS_PER_SECOND;
    }

}


if your timing is correct... you should se a ~1Hz output signal on B1.
_________________
CCS PCM 5.078 & CCS PCH 5.093
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 12, 2012 11:35 am     Reply with quote

Quote:

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);


enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);

Where is your #int_rtcc routine ?


Quote:
SET_TRIS_B(10000000);

What's the default number base used by the C language ?
Patronus



Joined: 05 Jun 2012
Posts: 10
Location: Sri Lanka

View user's profile Send private message

PostPosted: Tue Jun 12, 2012 11:51 am     Reply with quote

Gabriel,
Thanks for the reply.
yeah, checking it inside isr is better. Anyway I don't get a 1Hz output signal..

When my program runs, at the beginning timer is set and when number of interrupts that occurred becomes 19, the variable "seconds" should increment from 0 to 1.
What's wrong with the logic there? Why 254 seconds?

PCM Programmer,

I'm sorry, I just used the code that I used to check timer0 and that's why those lines are still there. As I know the least significant bit is for B0.
_________________
"Silence is argument carried out by other means"
Patronus



Joined: 05 Jun 2012
Posts: 10
Location: Sri Lanka

View user's profile Send private message

PostPosted: Tue Jun 12, 2012 12:05 pm     Reply with quote

Hello PCM Programmer... if I either add #int_rtcc routine or remove

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
enable_interrupts(INT_RTCC);

lines, the code works..

That was the problem I guess..?

Thanks a lot..
_________________
"Silence is argument carried out by other means"
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Jun 12, 2012 12:27 pm     Reply with quote

Quote:
When my program runs, at the beginning timer is set and when number of interrupts that occurred becomes 19, the variable "seconds" should increment from 0 to 1.
What's wrong with the logic there? Why 254 seconds?


Because the statement :

Code:
   while(seconds==1)
      {
         OUTPUT_HIGH(PIN_B1);
      }


will only happen when seconds = 1.

assume Seconds is =1 ... it goes into the while loop...
your timer keeps going... you have 19 more interrupts and then... seconds becomes 2... then 3 then 4 and so on... only when it overflows and flips over to 0 you will be able to go from 0 to 1... thus the statement will only execute ONCE every "size of Seconds variable" .. in this case its int16 so 65534 times...


Edit:
and like i said ... when it finally does happen... you wont notice it becase PIN_B1 is already high from the last time it did happen.... toggle the pin or set it low after some time before the next time seconds = 1.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Patronus



Joined: 05 Jun 2012
Posts: 10
Location: Sri Lanka

View user's profile Send private message

PostPosted: Tue Jun 12, 2012 12:49 pm     Reply with quote

Gabriel, anyway I should be able to see the first time it happens right? The problem was that I couldn't see that happening. Now after doing what I've mentioned in previous reply, I can see it happening.
_________________
"Silence is argument carried out by other means"
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 12, 2012 1:05 pm     Reply with quote

Quote:

SET_TRIS_B(10000000);
What's the default number base used by the C language ?

As I know the least significant bit is for B0.

http://www.ccsinfo.com/forum/viewtopic.php?t=47355&highlight=binary&start=5
Patronus



Joined: 05 Jun 2012
Posts: 10
Location: Sri Lanka

View user's profile Send private message

PostPosted: Tue Jun 12, 2012 1:31 pm     Reply with quote

I'm sorry... I haven't understand the question..
Bad mistake. I'll be more careful.
_________________
"Silence is argument carried out by other means"
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