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

Exit INT RTCC

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



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

Exit INT RTCC
PostPosted: Tue Aug 22, 2017 10:31 pm     Reply with quote

Hi,

I'm trying to exit INT_RTCC when I push button B4, pin B5 will become low but it will not exit the interrupt. Hope anybody can show me how to solve it. Below is my code.

Code:

#include <18F4550.h>
#fuses HSPLL,NOWDT,PROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN          // not use due to project used <usb_bootloader.h>
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)

#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#define INTS_PER_SECOND 9    //91     // (20000000/(4*4*65536))

#INT_RTCC                   // This function is called every time
void clock_isr()            // timer 1 overflows (65535->0), which is
{                           // approximately 19 times per second for
                            // this program.
int32 v;        // A running seconds counter
int8 int_count; // Number of interrupts left before a second has elapsed
       
if(--int_count==0)
  {
   if(input(PIN_B4))  //if push button B4 ON
     {
      ++v;
      int_count = INTS_PER_SECOND;
     }
   else   //if push button B4 OFF
     {
      output_low(PIN_B5); 
      disable_interrupts(INT_RTCC);
     }
  }
}

 
void main()
{
int  r2, y;
int8 i;
int16 aa[16];
int32 v;      // A running seconds counter
int8 int_count;    // Number of interrupts left before a second has elapsed
     
setup_timer_0(RTCC_DIV_2|RTCC_INTERNAL); // presacaler=2, Use EXTERNAL clock
                                         // if prescaler = 2, then every tick approximately = 0.167 Usecond 
                                         // 4x2 / 48000000 = 0.167usecond
while(true)
  {
   while(input(PIN_B4));  //wait push button B4 ON
     {
     }

   if(input(PIN_B0))
      output_high(PIN_B5);

   for(y=0; y<=200; y=y+8)
      {
       printf("\r");
       set_timer0(0);
       enable_interrupts(INT_RTCC);
       enable_interrupts(GLOBAL);

       int_count = INTS_PER_SECOND;
       v = 0;

       while (!input(PIN_B6));

       set_timer0(0); // init timer0 to 0
       delay_ms(220);

       while (input(PIN_B6));

       r2=1;

       for(i=2;i<=16;i++)
          {
           while (!input(PIN_B6)); // wait for B6 to go LOW
           
           set_timer0(0); // init timer0 to 0
       
           while (input(PIN_B6)); // wait for B6 to go HIGH
       
           aa[i]=get_timer0();
          }
     
      }

   output_low(PIN_B5);
  }
}           
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Wed Aug 23, 2017 1:05 am     Reply with quote

You have to declare int_count as static to preserve it between interrupts.
Code:
static int8 int_count= INTS_PER_SECOND;

otherwise the var gets destroyed every time you exit the interrupt.
When re-entering it will use a scratch value with some undetermined value in it.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Wed Aug 23, 2017 1:41 am     Reply with quote

Dear alan,

I've changed the code as you suggested.
Code:

int32 v;        // A running seconds counter
static int8 int_count; // Number of interrupts left before a second has elapsed

if(input(PIN_B4))  //if push button B4 ON     
  {   
    if(--int_count==0)   
     {
      ++v;
      int_count = INTS_PER_SECOND;
     }
  }
   else   //if push button B4 OFF
     {
      output_low(PIN_B5); 
      disable_interrupts(INT_RTCC);
     }
}

It will give the same result, pin B5 will output LOW when I push button B4. However I want to make it when I push once more button B4, pin B5 will become output HIGH.
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Wed Aug 23, 2017 2:24 am     Reply with quote

There is another oddity. You have a local variable called 'v' in the interrupt, which you increment, and then another local variable called 'v' in the main. Now you don't use it as yet (except to increment it in the interrupt), but assuming you want this to be a counter that is handled in the interrupt and then visible in the main, there needs to be a single global declaration of this if it is to ever work...

Now you have B5 being set low in two places, but the only thing that can set it high again, is if B4 is low, B0 is high, and B6, has gone high then low 16 times.
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