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

How do I create interrupts?

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








How do I create interrupts?
PostPosted: Tue Aug 23, 2005 4:00 am     Reply with quote

Ok on PORTA I want all my pins set as input pins, and I want them to all trigger respective interrupts. I tried using if statements

//defines of inputs from PIC_2 to LCD_PIC for readability
#define open_o PIN_A0
#define short_o PIN_A1
#define s_sound PIN_A2
#define on_off PIN_A3
#define on_open PIN_A4

while (TRUE)
{

if (open_o)
{
lcd_send_nibble(0x20);
lcd_putc("\f>Open loop\n");
delay_ms(100);
}

else if (short_o)
{
lcd_send_nibble(0x20);
lcd_putc("\f>short loop\n");
delay_ms(100);
}

else if (s_sound)
{
lcd_send_nibble(0x20);
lcd_putc("\f>beep\n");
delay_ms(100);
}

//main alarm triggered
else if (on_off)
{
lcd_send_nibble(0x20);
lcd_putc("\f>alarm triggered\n");
delay_ms(100);
}

//
else if (on_open)
{
lcd_send_nibble(0x20);
lcd_putc("\f>loop broken\n");
delay_ms(100);
}

else
{
lcd_putc("\f>System OK\n");
delay_ms(100);
}

}

I do not quite get the effect I am looking for, so what I want now is to replace my if statements with interrupt routines.

Thanks
qleyo



Joined: 23 Aug 2005
Posts: 22

View user's profile Send private message

PostPosted: Tue Aug 23, 2005 4:02 am     Reply with quote

That was my post above, I have just registered as I was unable to edit my post so

edit: How do I go about setting up the interrupts to the respective pins and then writing the interrupt routines?
Ttelmah
Guest







PostPosted: Tue Aug 23, 2005 4:25 am     Reply with quote

You don't.
The only interrupts available, are those supported by the _hardware_. Start by looking at the data sheet for the chip. The odds are that it will have no interrupts supported on PORTA. Generally PORTB, has more interrupt support, with B0, often being the dedicated interrupt input pin, and B4 to B7, generically supporting 'interrupt on change'. You cannot generate interrups on pins without the hardware.
One way you could work, would be to add external hardware to trigger the b0 interrupt if any of the PORTA pins goes to th active level. The other way is to poll the PORTA pins, inside a timer interrupt.

Best Wishes
Guest








PostPosted: Tue Aug 23, 2005 4:30 am     Reply with quote

I wondered why it was called RB0/int...hmm the texas instruments DSK I used was completely different, all pins could handle interrupts, I did not think it would be hardware specific... o well...

so will my current program work as it is? as I am polling the pins in an endless while loop.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Aug 23, 2005 4:43 am     Reply with quote

Quote:
so will my current program work as it is? as I am polling the pins in an endless while loop.
No, the defines you use to check the portA pins ar enot correct. If you check the header file for your PIC processor you will see something like
Code:
#define PIN_A0  31744
#define PIN_A1  31745
#define PIN_A2  31746

The PIN defines are basically some compiler internal references, not port addresses.

Change your test for port values to
Code:
 if (input(open_o))
{
  ...
}

else if (input(short_o))
{
  ...
}
etc
qleyo



Joined: 23 Aug 2005
Posts: 22

View user's profile Send private message

PostPosted: Tue Aug 23, 2005 6:00 am     Reply with quote

sweet,

i'll have a go and update you...thanks :-)
qleyo



Joined: 23 Aug 2005
Posts: 22

View user's profile Send private message

PostPosted: Tue Aug 23, 2005 7:06 am     Reply with quote

It works now with the modifications i know have

Code:


#include <16f84a.h>
#include <16f84a_lcd.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)

//defines of inputs from PIC_2 to LCD_PIC for readability
#define open_o  PIN_A0
#define short_o PIN_A1
#define s_sound PIN_A2
#define on_off  PIN_A3
#define on_open PIN_A4

//set_tris_x define
#define ALL_IN 0xff

void main()
{

   set_tris_a(ALL_IN);  //make port a all inputs
    lcd_init(); //initialise display
    delay_ms(6);

    lcd_putc("\f***OMEGA LITE***\n");
    delay_ms(200);

   lcd_putc("\f>ALARM ACTIVATED\n");
   delay_ms(200);

while (TRUE)
{

    if (input(open_o)) 
   {
      lcd_send_nibble(0x20);
      lcd_putc("\f>Open loop\n");
      delay_ms(100);   
   }

    if (input(short_o))
   {
      lcd_send_nibble(0x20);
      lcd_putc("\f>short loop\n");
      delay_ms(100);   
   }

    if (input(s_sound))
   {
      lcd_send_nibble(0x20);
      lcd_putc("\f>beep\n");
      delay_ms(100);   
   }

   //main alarm triggered
    if (input(on_off))
   {
      lcd_send_nibble(0x20);
      lcd_putc("\f>alarm triggered\n");
      delay_ms(100);
   }

   //
    if (input(on_open))
   {
      lcd_send_nibble(0x20);
      lcd_putc("\f>loop broken\n");
      delay_ms(100);   
   }

   else
   {
      lcd_putc("\f>System OK\n");
      delay_ms(100);         
   }

}

}



And it works fine :-D
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 23, 2005 7:10 am     Reply with quote

One note

Code:
if (open_o)

will not work but


Code:
if (input(open_o))
will work

CCS provides input(), output_low(), output_high(), output_float() functions that work with the pin defines.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Aug 23, 2005 8:23 am     Reply with quote

One note, this type of code cannot actually be considered an interrupt. It is simply scanning the condition of various inputs and then doing whatever the if() statements want. An actual interrupt is hardware, inside of the PIC, that will actually 'interrupt' whatever is going on, else where in the code, and go do what the 'interrupt' code dictates. Once the interrupt is finished doing it's thing the PIC will then return to where it was interrupted and continue on it's merry way. Interrupts are designed to do a task that needs to be done 'right now' and cannot wait for the code to reach it through normal execution.

Ronald
Guest








PostPosted: Wed Aug 24, 2005 8:15 am     Reply with quote

Yes if you read my post in detail, the chip I am using 16f84 has just one interrupt pin (or two?) well I would need five, so I have resorted to polling those pins and using if statements instead, perhaps a little twiddling with the timings and I should be fine...interrupts would have been the better way forward but o well....
RobM



Joined: 29 Apr 2005
Posts: 13
Location: Santa Cruz, CA

View user's profile Send private message

portb interrupt on change
PostPosted: Wed Aug 24, 2005 5:14 pm     Reply with quote

On the f84 the portb pins can be set up to interrupt on change.

I don't see how it would help you very much, your polling loop seems to work fine.

Oh, BTW, the 16F628 is cheaper, but has the same core plus a UART. The f84 is obsolete.
_________________
Rob
_______
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