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

interrupts

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



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

interrupts
PostPosted: Fri May 16, 2008 6:35 pm     Reply with quote

Hi

I need some help... about interrupts, I need a little function with normal ports (RA, RB, RC or RD)..

for example I have simple 4 switch and I like connect them to PIC18F252 in port B or other...
I like click on switch number 1 and appear on terminal "SW1", on switch number 2 "SW2", etc..

how I can make a function with these 4 interrupts? some one can help me?

best regards,
Filipe Abrantes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 16, 2008 8:19 pm     Reply with quote

Look at this CCS example file. You can connect four push-button
switches to pins B4, B5, B6, B7. It will detect an interrupt-on-change
and display a message if a button is pushed.
Quote:
c:\Program Files\picc\Examples\Ex_pbutt.c


For this example, you need to enable Port B pull-up resistors with
this CCS function. Put it at the start of main():
Code:
 port_b_pullups(TRUE);


If you press a button on pin B4, it turns on an LED on pin B0.
If you press the button on pin B5, it turns the LED off.
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Wed May 21, 2008 10:44 am     Reply with quote

Hi

I need put pull-ups resistors on PIN B7,B6,B5,B4 and put on switch GND signal?

I dont put 5V on switch?

what in example EX_PBUTT.C mean:

Quote:
if ((!bit_test(last,4)) && (bit_test(current,4))) {dbutton4=1;}


what make "bit_test(last,4)"?

regards
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Wed May 21, 2008 12:19 pm     Reply with quote

Hi

I put pull-downs on PINs B7, B6, B5, B4 and connect on switch 5V (on-press)...

and make a litle experience but it dont work..

Quote:

#include <18F252.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=40000000)
#use rs232(baud=4800, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8, stream=gpsport)
#use rs232(baud=4800, parity=N, xmit=PIN_C4, rcv=PIN_C5, bits=8, stream=tlmport)

#build(reset=0x200)
#build(interrupt=0x208)
#org 0x0000,0x01ff
void bootloader() {
#asm
nop
#endasm
} // Reserve space for the bootloader


int btn1, btn2, btn3, btn4;

#int_rb
void active_alarm()
{
btn1=input(PIN_B7);
btn2=input(PIN_B6);
btn3=input(PIN_B5);
btn4=input(PIN_B4);
}

void main()
{

port_b_pullups(TRUE);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);

btn1=0;
btn2=0;
btn3=0;
btn4=0;

while(TRUE)
{

if (btn1==1) output_high(PIN_B3);
if (btn2==1) output_high(PIN_B3);
if (btn3==1) output_high(PIN_B3);
if (btn4==1)
{
output_low(PIN_B0);
output_low(PIN_B1);
output_low(PIN_B2);
}
}


}


some one can tell me because this not work?

regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 21, 2008 12:29 pm     Reply with quote

I don't know. Why would you turn on internal port_b_pullups, and then connect external pull-downs ?

Internal PortB pullups provide a "logic high" idle state for switches that
connect to ground when they are pressed.

You connected external pull-downs with switches that connect to +5v
when pressed. OK, but then why did you leave in the line of code to
enable internal pull-ups ?
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Wed May 21, 2008 12:53 pm     Reply with quote

hi

I think dont understand all but now a removed line port_b_pullups(TRUE);
but continue dont work.

I dont need have a pull-downs on switch?

I have some like that:



program without this line stay all ok?

regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 21, 2008 2:30 pm     Reply with quote

I don't have your exact hardware. I modified your program so it works
with my board. I changed the crystal to 4 MHz and set the fuse to XT.
I only have one button, so I put it on pin B4 and commented out B5-B7.
I changed the LED so it's on pin B0. It works. When the button is
pressed, the LED is turned on. When the button is released, the LED is off.
Code:
#include <18F452.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
//#use rs232(baud=4800, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8, stream=gpsport)
//#use rs232(baud=4800, parity=N, xmit=PIN_C4, rcv=PIN_C5, bits=8, stream=tlmport)


int btn1, btn2, btn3, btn4;

#int_rb
void active_alarm()
{
btn4=input(PIN_B4);
}

void main()
{
int8 c;

c = input_b();
clear_interrupt(INT_RB);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);

btn4=0;

while(TRUE)
  {
   if(btn4==1)
      output_high(PIN_B0);
   else
      output_low(PIN_B0);
  }

}
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Wed May 21, 2008 3:32 pm     Reply with quote

Hi

yes your example work perfectly but why you put

int8 c;
c=input_b();

after I try your example I return put my program but with main configuration:

int8 c;
c=input_b();

clear_interrupt(INT_RB);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);

and dont make more modifications on my program, and it dont work why?

this is my actual circuit (resistor=10K):


PCM Programmer, you think better I change my circuit and use negative logic on this interrupts and use pull-down resistors.

I dont need any pull-ups or pull-down?

I stay make an project I can change all.

if I need change my circuit, witch modifications I can make in the new program?

best regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 21, 2008 4:05 pm     Reply with quote

Quote:
yes your example work perfectly but why you put

int8 c;
c=input_b();

I read PortB to clear any existing "mismatch condition" before the
interrupts are enabled. This is done so that an interrupt does not
immediately occur when interrupts are enabled. You want the first
interrupt to occur only when the button is pressed.
The PIC data sheet says:
Quote:
A mismatch condition will continue to set flag bit RBIF.
Reading PORTB will end the mismatch condition and
allow flag bit RBIF to be cleared.


Quote:
and dont make more modifications on my program, and it dont work why?

Do some experiments and test it. Find the problem.
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Thu May 22, 2008 5:24 am     Reply with quote

Hi

I go start of begin, because if I modify your example program for example:

Quote:

while(TRUE)
{
if(btn4==1)
output_high(PIN_B0);
else if (btn3==1)
output_high(PIN_B1);
}


led on PIN B0 stay on but led on B1 dont happen anythink....

I go start used negative logic...

start put switch connect on GND and resistor Pull-ups or I dont need resistors?)

for I used negative logic I need put on main function "port_b_pullups(TRUE)"?

regards
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Thu May 22, 2008 5:51 am     Reply with quote

Hi

now I try use 2 examples, one which i see on this forum and other is example of CCS EX_PBUTT.C but any work Crying or Very sad

Quote:

#include <18F252.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=40000000)

#build(reset=0x200)
#build(interrupt=0x208)
#org 0x0000,0x01ff
void bootloader() {
#asm
nop
#endasm
} // Reserve space for the bootloader

static int8 old_pins;
#define first_bit(x) (x&0x1)
#define second_bit(x) (x&0x2)
#define third_bit(x) (x&0x4)
#define fourth_bit(x) (x&0x8)

int btn1, btn2, btn3, btn4;

#INT_RB
void b_changed(void) {
int new_pins;
int changes;
new_pins=input_b(); //read the port to reset the latch
//Now simply test the bits, to see which pin(s)
//have changed.
changes=new_pins^old_pins; //This now reflects every bit that has
//changed.
changes=changes&new_pins; //This now reflects every bit that has
//gone _high_ (use 'old_pins', instead of 'new_pins',if you want to find
//the ones that have gone low).
old_pins=new_pins; //update your stored value for the future

btn1=0;
btn2=0;
btn3=0;
btn4=0;

if (first_bit(changes)) {
btn1=1;//here RB0 has gone high
}
if (second_bit(changes)) {
btn2=1;//Here RB1 has gone high
}
if (third_bit(changes)) {
btn3=1;//Here RB2 has gone high
}
if (fourth_bit(changes)) {
btn4=1;//Here RB3 has gone high
}

}


void main()
{

old_pins=input_b();
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);

btn1=0;
btn2=0;
btn3=0;
btn4=0;

while(1)
{
if (btn1==1) (output_high(PIN_B0));
else if (btn2==1) (output_high(PIN_B1));
else if (btn3==1) (output_high(PIN_B2));
else if (btn4==1)
{
output_low(PIN_B0);
output_low(PIN_B1);
output_low(PIN_B2);
}
}

}

link: http://www.ccsinfo.com/forum/viewtopic.php?t=34528&highlight=interrupts


Quote:

#include <18F252.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=40000000)

#build(reset=0x200)
#build(interrupt=0x208)
#org 0x0000,0x01ff
void bootloader() {
#asm
nop
#endasm
} // Reserve space for the bootloader



#define LOWTOHIGH TRUE
#define HIGHTOLOW FALSE

short int dbutton4, dbutton5, dbutton6, dbutton7;

#int_rb
void detect_rb_change() {
int current;
static int last=0;

set_tris_b(0xF0);
current=input_b();

#if LOWTOHIGH
if ((!bit_test(last,4))&&(bit_test(current,4))) {dbutton4=1;}
if ((!bit_test(last,5))&&(bit_test(current,5))) {dbutton5=1;}
if ((!bit_test(last,6))&&(bit_test(current,6))) {dbutton6=1;}
if ((!bit_test(last,7))&&(bit_test(current,7))) {dbutton7=1;}
#elif HIGHTOLOW
if ((!bit_test(current,4))&&(bit_test(last,4))) {dbutton4=1;}
if ((!bit_test(current,5))&&(bit_test(last,5))) {dbutton5=1;}
if ((!bit_test(current,6))&&(bit_test(last,6))) {dbutton6=1;}
if ((!bit_test(current,7))&&(bit_test(last,7))) {dbutton7=1;}
#endif

last=current;
}

void clear_delta() {
dbutton4=0;
dbutton5=0;
dbutton6=0;
dbutton7=0;
}

void main() {
clear_delta();

enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);

output_low(PIN_B0);
while (TRUE) {
if(dbutton4) {
output_high(PIN_B0);
dbutton4=FALSE;
}
if(dbutton5) {
output_low(PIN_B0);
dbutton5=FALSE;
}
}
}



file: EX_PBUTT.C

what correct circuit for use interrupts on port B with 4 switch?

best regards
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Thu May 22, 2008 11:40 am     Reply with quote

Hi

I make some modification and now 4 buttons works but the problem is, I dont know which button I press Confused

some one can help me?

Quote:

#include <18F252.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=40000000)

#build(reset=0x200)
#build(interrupt=0x208)
#org 0x0000,0x01ff
void bootloader() {
#asm
nop
#endasm
} // Reserve space for the bootloader


int btn1, btn2, btn3, btn4;

#int_rb
void active_alarm()
{

btn1=input(PIN_B7);
btn2=input(PIN_B6);
btn3=input(PIN_B5);
btn4=input(PIN_B4);
}

void main()
{
int8 c;
c=input_b();

clear_interrupt(INT_RB);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);

btn1=0;
btn2=0;
btn3=0;
btn4=0;

while(TRUE)
{

if ((btn1==1) || (btn2==1) || (btn3==1) || (btn4==1))
output_high(PIN_B0);
else
output_low(PIN_B0);
}

}


regards
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Thu May 22, 2008 2:56 pm     Reply with quote

Hi

I can enable and disable interrupts under the program several times?

for example, after have an interrupt and come to a function write disable_interrupts(INT_RB); for dont have interrupts and when out make enable_interrupts(INT_RB);

this is possible?

regards
Ken Johnson



Joined: 23 Mar 2006
Posts: 197
Location: Lewisburg, WV

View user's profile Send private message

PostPosted: Fri May 23, 2008 6:37 am     Reply with quote

Yes

Ken
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