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

18f452 External Interrupts Problem

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



Joined: 13 May 2007
Posts: 34

View user's profile Send private message

18f452 External Interrupts Problem
PostPosted: Mon May 21, 2007 7:00 am     Reply with quote

I've written what I thought was some simple code to get an external interrupt working on a pic18f452 but my interrupt routine never ever gets hit and I don't know why although I'm guessing I haven't configures this properly. Here's the code:

Code:


#include "J:\My Documents\CCS Projects\GNSMotors02\main.h"

#define  LED0        PIN_B1

boolean pressed = false;

void led_on()
{
   output_high(LED0);
}

void led_off()
{
   output_low(LED0);
}

#int_EXT
void  EXT_isr(void)
{
   led_on();
   delay_ms(100); //debounce switch
   pressed = true;
   
}

void Do_States()
{
   if(pressed)
   {
      //SetMotorPositions();
   }
}

void main()
{
   //port_b_pullups(TRUE);
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   
   //timer 2 set up for maximum frequency of 4MHz clock
   //4000000 / 1024  3906.25Hz
   setup_timer_2(T2_DIV_BY_1,255,1);   
   
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   
   //set up pwm outputs
   setup_ccp1(CCP_PWM);
   setup_ccp2(CCP_PWM);
   
   //initialise at 0% duty cycle
   set_pwm1_duty (0);
   set_pwm2_duty (0);

   //set up interrupt on B0
      clear_interrupt(INT_EXT);
      enable_interrupts(INT_EXT);
      ext_int_edge(0,H_TO_L);
   enable_interrupts(GLOBAL);

   set_tris_b(11111101);
   port_b_pullups(00000001);

   while(1)       
   {   
      led_off();
       Do_States();     
   }

}


I've got a non latching switch connected to port b pin 0 and it is normally high, going low when pressed (although reading the voltage at pin b0 when the switch is not pressed only gives me 1.7V - is this right?!); It does go to 0V when pressed. The EXT_isr routine never gets hit so the interrupt doesn't seem to be working at all. Any ideas why this is?
drh



Joined: 12 Jul 2004
Posts: 192
Location: Hemet, California USA

View user's profile Send private message

PostPosted: Mon May 21, 2007 8:16 am     Reply with quote

Change this:
Quote:
set_tris_b(11111101);
port_b_pullups(00000001);


to this:
set_tris_b(0b11111101);
port_b_pullups(0b00000001);
_________________
David
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon May 21, 2007 9:03 am     Reply with quote

Quote:

I've got a non latching switch connected to port b pin 0 and it is normally high, going low when pressed (although reading the voltage at pin b0 when the switch is not pressed only gives me 1.7V - is this right?!);

No. The voltage should be close to +5V.
Something wrong:
1) in your hardware
2) in the pin direction configuration
3) in the pull up resistor


It is not a good practice to use delays inside an interrupt handler !!!

To 'see' the RB0 interrupt action, the following code should be enough.

Code:
 
#int_ext
void EXT_ISR()
 {
  output_low(LED0);
  pressed = true;
 }


void main()
{
  enable_interrupts(INT_EXT);
  ext_int_edge(0,H_TO_L);
  enable_interrupts(GLOBAL);

  while(1)
         {
           .......
           your stuff
           .......

           if(pressed)
             {
               delay_ms(200);
               clear_interrupt(INT_EXT); 
               output_high(LED0);
               delay_ms(200);               
               pressed = FALSE;
             }
         }
}



Humberto
jemly



Joined: 13 May 2007
Posts: 34

View user's profile Send private message

PostPosted: Wed May 23, 2007 4:25 am     Reply with quote

Thank you Humberto that's really useful!
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