|
|
View previous topic :: View next topic |
Author |
Message |
tmach
Joined: 20 Dec 2003 Posts: 14
|
Help with code |
Posted: Fri Feb 13, 2004 1:03 pm |
|
|
Hello!
I am trying to use the interrupt on RB0 to control pin D1 on my PIC16F874A. I have used an example posted in this forum but I do not get expected output out of pin D1.
Initially I set high on pin D1. On a H to L change on RB0, after 2 sec if RB0 still remains low, I turn pin D1 to low. I turn on the PIC with RB0 connected to high and then connect RB0 to low. I expect to see PIN RD1 go low after 2 seconds. I do not see any change on RD1. It always remains high. Please advice.
mach
*****************************************************
#include <16f874A.h>
#device ADC=10;
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7, ENABLE=PIN_C0)
#bit INTF_BIT = 0x0B.1
//Global declares
int settle = 2000;
#int_ext
void INTRB0(void);
main()
{
DELAY_US(10);
ext_int_edge(H_TO_L);
INTF_BIT = 0;
ENABLE_INTERRUPTS(INT_EXT);
ENABLE_INTERRUPTS(GLOBAL);
OUTPUT_HIGH(PIN_D1);
}
#INT_EXT
intRB0()
{
disable_interrupts(INT_EXT);
disable_interrupts(GLOBAL);
delay_ms(settle);
INTF_BIT = 0;
if(!INPUT(PIN_B0)) OUTPUT_LOW(PIN_D1);
}
******************************************************** |
|
|
Ttelmah Guest
|
Re: Help with code |
Posted: Fri Feb 13, 2004 3:29 pm |
|
|
tmach wrote: | Hello!
I am trying to use the interrupt on RB0 to control pin D1 on my PIC16F874A. I have used an example posted in this forum but I do not get expected output out of pin D1.
Initially I set high on pin D1. On a H to L change on RB0, after 2 sec if RB0 still remains low, I turn pin D1 to low. I turn on the PIC with RB0 connected to high and then connect RB0 to low. I expect to see PIN RD1 go low after 2 seconds. I do not see any change on RD1. It always remains high. Please advice.
mach
*****************************************************
#include <16f874A.h>
#device ADC=10;
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7, ENABLE=PIN_C0)
#bit INTF_BIT = 0x0B.1
//Global declares
int settle = 2000;
#int_ext
void INTRB0(void);
main()
{
DELAY_US(10);
ext_int_edge(H_TO_L);
INTF_BIT = 0;
ENABLE_INTERRUPTS(INT_EXT);
ENABLE_INTERRUPTS(GLOBAL);
OUTPUT_HIGH(PIN_D1);
}
#INT_EXT
intRB0()
{
disable_interrupts(INT_EXT);
disable_interrupts(GLOBAL);
delay_ms(settle);
INTF_BIT = 0;
if(!INPUT(PIN_B0)) OUTPUT_LOW(PIN_D1);
}
******************************************************** |
You need to put a 'stopper' at the end of your main code.
Add:
[code]
while(true) ;
[/code/
as the last line of your code.
Your main routine, will run 'off the end', since it does not loop. The compiler inserts a 'sleep' instruction after the end of the code. You will be hitting this.
Best Wishes |
|
|
tmach
Joined: 20 Dec 2003 Posts: 14
|
Re: Help with code |
Posted: Sun Feb 15, 2004 12:05 am |
|
|
Yes,
I use a while loop at the end in the main program. But I am using this while loop to implement a softare SPI. For example I use statements such as while(~clk) to wait for clock edge. The code would look something like this:
#INT_EXT
intRB0()
{
disable_interrupts(INT_EXT);
disable_interrupts(GLOBAL);
delay_ms(settle); //Let RB0 settle for 2 seconds
INTF_BIT = 0;
if(!INPUT(PIN_B0)) OUTPUT_LOW(PIN_D1); //if RB0 is still low, disable D1
printf("test");
}
main()
{
output_high(pin_D1); //initially set D1 high
statements;
.....
...
while(1)
{
while(~clk);
statements;;
}
}
Still there is no change on pin D1 even if RB0 changes from H->L. I have added a printf to see if the ISR is operating and I do see printf working each time there is a H->L transition on D1. But the statement
if(!INPUT(PIN_B0)) OUTPUT_LOW(PIN_D1); does not seem to execute.
All I want is regardless of my software SPI, i.e even if the control is waiting for the clock transition, I should be able to process the change on RB0 and set the appropriate signal on D1, because RB0 is a current sense signal and is critical for circuit protection. Please tell me where I am doing wrong.
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Feb 15, 2004 1:32 pm |
|
|
Here are a few comments on your code.
This is not necessarily enough to make your code work,
but it's just a few things that I noted on a quick scan.
#include <16f874A.h>
// The CCS syntax for #device statements does not include
// putting a semi-colon at the end of the line. Remove it.
#device ADC=10;
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7, ENABLE=PIN_C0)
#bit INTF_BIT = 0x0B.1
//Global declares
// In CCS, an "int" is an unsigned 8-bit value.
// This next line will not work.
// An "int" can only hold a number up to 255.
// You need to change the data type to be "int16" instead.
// This is the reason why you were not getting a 2 second delay.
int settle = 2000;
#int_ext
void INTRB0(void);
main()
{
DELAY_US(10);
ext_int_edge(H_TO_L);
INTF_BIT = 0;
ENABLE_INTERRUPTS(INT_EXT);
ENABLE_INTERRUPTS(GLOBAL);
OUTPUT_HIGH(PIN_D1);
// Add the following line to prevent
// the PIC from going to sleep.
while(1);
}
#INT_EXT
intRB0()
{
disable_interrupts(INT_EXT);
You don't have to turn off Global interrupts here.
// It's done automatically by the PIC, in hardware,
// when an interrupt occurs.
disable_interrupts(GLOBAL);
// delay_ms(settle);
// You don't need the following line.
// The compiler automatically puts in code
// to clear the INTF flag at the end of this
// interrupt service routine.
INTF_BIT = 0;
if(!INPUT(PIN_B0)) OUTPUT_LOW(PIN_D1);
} |
|
|
tmach
Joined: 20 Dec 2003 Posts: 14
|
|
Posted: Wed Mar 03, 2004 4:01 pm |
|
|
Thanks, I got it working now.
mach |
|
|
|
|
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
|