Continue to Site

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.

timer 0 explain below program

Status
Not open for further replies.

jaya krishna

Member level 1
Joined
May 10, 2012
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
India,coimbatore
Activity points
1,770
hi every one ,how to calculate the timer value 76 it show in below program its 1second and also how the clock _isr() loop will excute.there is [if(--int_count==0)]how it decremented the value of 76. i need clear explain of this program.
PHP:
#if defined(__PCH__)
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=40000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#endif

#define INTS_PER_SECOND 76 //Processor and clock specific 
#define LED0 PIN_B0 

int8 int_count=0; 
int8 data = 0; 
////////////////////////////////////////////////// 

#int_rtcc 

clock_isr() 
{ 

set_tris_b(0x00);
   //this block maintains timer variables with a resolution of a ~ 1 s 
   if(--int_count==0) 
   { 
      int_count=INTS_PER_SECOND; //reset 
      if(data == 0) 
      { 
         output_low(LED0); 
         data = 1; 
        // delay_ms(50);
      } 
      else 
      { 
         output_high(LED0); 
         data = 0; 
        // delay_ms(50);
      } 
   } 
} 

void main(void) 
{ 

   int_count=INTS_PER_SECOND; 
   setup_counters( RTCC_INTERNAL, RTCC_DIV_256); 
   setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256 | RTCC_8_BIT); 
   enable_interrupts(INT_RTCC); 
   enable_interrupts(GLOBAL); 
   while(1) 
   { }
}

thanks advance
by
jai
 

The part of code that set how frequently the interrupt is called is not shown.

As a guess, the clock_isr() routine is triggered by an event set in the clock alarm registers.

The "if(--int_count==0)" code means subtract 1 from int_count then check if the value is zero. The opposite would be to place the -- after int_count in whch case it would check for zero before subtracting 1.

Brian.
 

thank u friend for ur reply ,i get the concept i think, i declare data in global variable data=0;condition is true it passes to the if condition led is low and then data is assign as 1.global variable data is assign as 1.again value is reset .if condition is it passes through else part.led become high . but still wat is the uses of --int_count ==0;first time it assign zero in global declaration second time loop it assign the int_count=75;then it check the if condition. it will be fail.....but program excute correctly..i not understand the particular function.
 

The line "int8 int_count=0;" doesn't have to make the value zero, it's just the writers preference and maybe useful for debugging. It would work just as well if it said "int8 int_count;" which would leave the variable with a random value in it.

The program flow in 'C' always start with the main() function so the first line executed is "int_count=INTS_PER_SECOND;" which puts the value 75 in int_count because INT_PER_SECOND has already been defined to be 75.

At the end of the main() function the program goes into a loop "while(1){ }" which is stays in until an interrupt occurs. The interrupt calls the function clock_isr() which counts down from 75 to zero. Only when it reaches zero does the following code run. that code resets int_count back to 75 and then executes the LED code. So what you are doing is executing that part of code only once in every 75 interrupts, it's a way of slowing down the rate at which the code is run.

Incidentally, do not use the delay_ms lines inside the interrupt routine, I would remove them altogether. Because the interrupt is called at set periods, if you add delays of your own it is possible that your delay causes the code to be re-executed from the start while you are still waiting in the delay routine, this will give unexpected results. It is always best to exit an ISR as quickly as possible so delays in an ISR are a bad thing.

Brian.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top