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.

Counter with pic, in CCS C Compilers

Newbie level 3
Joined
Jul 16, 2024
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
21
Hello, I'm trying to make a control code and I can't, the count works as expected without delay_ms (3000); but when I put it on, the count is much higher, would there be another way to pause without using this delay? I want a pause to turn on and a pause to turn off, without this disturbing the time count, I'm using the ccs c compiler. Thank you in advance.

Code:
 while(TRUE)
   {
    if (conta >= 3600 )
    {
      conta = 0;
      minuto++;
    }
      
       delay_ms (3000);   
    
       if(input(!PIN_A3))
      {
       output_low(pin_a0);
       output_low(pin_a1);               
       conta = 0;                   
       minuto=0;
       }
        
           delay_ms (3000);               
      
      if(( minuto <= 10  ) && (input(PIN_A3)))
     {
      output_high(pin_a0); 
      output_high(pin_a1); 
     }
      
   if( minuto > 10)
     {
      output_low(pin_ao);
      output_low(pin_a1);             
      conta = 0;                   
      minuto=0;
      }
      }
      }
 
Would be a good idea to sketch intended timing. I don't recognize how it's time couting, variable conta is nowhere increased in the shown code.

Timer interrupts are a solution, but not the only one. You must get rid of a linear code structure with delays stopping operation. Loop must not necessarily be stopped to execute a delay. It can be also implemented as branch controlled by a delay counter.
 
I used it that way too and it didn't work. Remembering that I receive this count from the 60 Hz network in _RA2.
Code:
int16 minuto = 0;
int16 conta = 0;
int second = 0;
int LG = 0;
#use delay (clock=4000000)


#INT_RA                             
//------------------------------------------------------------------------
void RA_isr(void)                       
{
  clear_interrupt(INT_RA2);
  if(input(PIN_A2))                 
  {
   conta++;
   LG++;
  }
}

void main()
{
  
     set_tris_a  (0b0001100 ); 
     enable_interrupts(GLOBAL);           
     enable_interrupts(INT_RA2);
     clear_interrupt(INT_RA2);       
     output_low(pin_a0);
     output_low(pin_a1);
 
 while(TRUE)
   {
    if (conta >= 3600 )  // a minute passed
    {
      conta = 0;
      minuto++;
    }
 if (LG >= 60 )  //  a seconds passed
    {
    LG=0;
    second++;
    
    }
    
      ///////////// delay_ms (3000); 
  
       if((input(!PIN_A3)) && (second==3))
      {
       output_low(pin_a0);
       output_low(pin_a1);             
       conta = 0;                 
       minuto=0;
       second=0;
       LG = 0;
       }
      
   /////delay_ms (3000);             
    
      if(( minuto <= 10  ) && (input(PIN_A3)) && (second==3)))
     {
      output_high(pin_a0);
      output_high(pin_a1);
     }
    
   if( minuto > 10)
     {
      output_low(pin_ao);
      output_low(pin_a1);           
      conta = 0;                 
      minuto=0;
     second=o;
     LG = 0;
      }
      }
      }
 
Last edited:
Hi,

since "conta" is updated within the ISR the variable needs to be declared as "volatile".

And IF the update rate of conta is 1s as recommended ... then 1 minute equals 60 ticks, not 3600 as you wrote.

Klaus
 
You added the routine to deal with I/O interrupt (the counting), but still is missing the routine to deal with Timer interrupt (the time base).
 
PIN_A2 receives the positive signal from the electrical network, 60hz, 60 cycles equal to 1 second, 3600 cycles equal to 1 minute.
the recommendation was to generate a 1s timer interrupt ...

You did not follow this recommendation. Then probably it will still not work.

Klaus
 

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top