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

interrupt on change

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







interrupt on change
PostPosted: Wed Jun 23, 2004 11:33 am     Reply with quote

A question for the masters here regarding the RB interrupt.

I have a really simple routine that I'm testing, and it's not behaving like I thought that it should.

I'm playing with a 4x4 keypad hooked up to port B, the lower 4 bits are outputs (low), and the upper 4 bits are inputs. Pullups are enabled. Pressing a key will connect a lower port B bit to an upper port B bit and pull down one of the upper 4 bits, and trigger the RB interrupt on change.

My interrupt routine is as follows:

Code:
#int_RB
void RB_isr(void) {
      key_pressed = TRUE;
}


My main routine simply checks the status of key_pressed, and if true, prints "Key pressed" on an lcd module I've hooked up to it.

What has me scratching my head is that if I press and release a button, "Key pressed" gets displayed. However, if I press and hold the button, nothing happens. As soon as I release the button, then "Key pressed" appears.

What am I missing? Shouldn't a button press instantly cause the interrupt routine to run, and shouldn't it run again once the button is released, since there are two changes happening?

How do I get the thing to recognize the initial press, and also recognize the subsequent release? What I'm ultimately after is an interrupt driven routine that recognizes momentary button presses, but will also support press-and-hold actions as well.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 23, 2004 1:30 pm     Reply with quote

You will get an RB interrupt if the logic level on one of the pins changes
from the previous state it was in, based on the last time Port B was
read (or written to, according to the data sheet).

In other words, if you want to detect a falling edge, you need to read
the port when the inputs are at a high level. This will load a logic '1'
into a latch. Then when you press a key and put a '0' on the pin,
the logic will now detect this "mismatch" condition and cause an
interrupt.

So I think you need to do several things:
Read Port B initially, and ensure that the value read is all logic 1's
on the RB4-RB7 pins before you proceed. (I assume you have
Port B pullups enabled).
Manually clear the RBIF flag before enabling interrupts.
Also clear RB interrupts by reading Port B within your ISR.

This post contains a demo program to show how to use Port B.
http://www.ccsinfo.com/forum/viewtopic.php?t=15126
Please don't take this as an example of how to do an interrupt driven
keypad. The example is only intended to "get you going" on INT_RB
interrupts by showing some aspects of how to use it.
PICCER



Joined: 10 Oct 2005
Posts: 1

View user's profile Send private message

PostPosted: Mon Oct 10, 2005 9:16 am     Reply with quote

Hello

I'm wondering what I should add the PCM PROGRAMMER?s code that it regonize right button in PORTB what I have pressed. Buttons are connected to RB4,RB5,RB6. So can someone put me some sample code?
Code:

#include "18F4520.h"
#device ICD=true
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, NOLVP
#use Delay(Clock=20000000)
#zero_ram
#use RS232(DEBUGGER)

#byte port_b = 0xF81

void int_rb_isr(void);


char got_interrupt;
char interrupt_count;


void main(void)
{

set_tris_b(0x30);

port_b = 0;

port_b_pullups(TRUE);
delay_us(10);

interrupt_count = 0;
got_interrupt = FALSE;

enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);


while(1)
{
disable_interrupts(INT_RB);
if(got_interrupt == TRUE)
{
printf("\%d\n\r", interrupt_count);
got_interrupt = FALSE;
}
enable_interrupts(INT_RB);

}

}
/
#int_rb
void int_rb_isr(void)
{

static char i;

i = port_b;

interrupt_count++;

got_interrupt = TRUE;
}




Cheers
John
newguy



Joined: 24 Jun 2004
Posts: 1907

View user's profile Send private message

PostPosted: Mon Oct 10, 2005 9:50 am     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=19726
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