View previous topic :: View next topic |
Author |
Message |
Guest
|
How do I create interrupts? |
Posted: Tue Aug 23, 2005 4:00 am |
|
|
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
|
|
Posted: Tue Aug 23, 2005 4:02 am |
|
|
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
|
|
Posted: Tue Aug 23, 2005 4:25 am |
|
|
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
|
|
Posted: Tue Aug 23, 2005 4:30 am |
|
|
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
|
|
Posted: Tue Aug 23, 2005 4:43 am |
|
|
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
|
|
Posted: Tue Aug 23, 2005 6:00 am |
|
|
sweet,
i'll have a go and update you...thanks :-) |
|
|
qleyo
Joined: 23 Aug 2005 Posts: 22
|
|
Posted: Tue Aug 23, 2005 7:06 am |
|
|
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
|
|
Posted: Tue Aug 23, 2005 7:10 am |
|
|
One note
will not work but
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
|
|
Posted: Tue Aug 23, 2005 8:23 am |
|
|
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
|
|
Posted: Wed Aug 24, 2005 8:15 am |
|
|
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
|
portb interrupt on change |
Posted: Wed Aug 24, 2005 5:14 pm |
|
|
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
_______ |
|
|
|