|
|
View previous topic :: View next topic |
Author |
Message |
abirdev
Joined: 04 Oct 2019 Posts: 1
|
Interrupt |
Posted: Fri Oct 04, 2019 6:50 am |
|
|
Hello,
I have a pushbutton connected to the int2.
After pwr-on the COUNTS is always equal to 1, even if i don't push the button. Why ? I set the count = 0;
Code: |
//#include <18F47J53.h>
#include <18F25k50.h>
#Device ADC=10
#fuses INTRC_IO
#fuses NOCPUDIV
#fuses LS48MHZ
#fuses PLLEN
#fuses NODEBUG
#use delay(clock=8MHz)
//#use rs232(baud = 9600, xmit=PIN_D6, rcv = PIN_D5) //, stream=STREAM_USER_UART, timeout=200)
#use rs232(baud = 9600, xmit=PIN_C6, rcv = PIN_C7) //, stream=STREAM_USER_UART, timeout=200)
#define NSAMPLES 10 //size of sample buffer LDR
//#define PUSH_BUTTON PIN_C1
//#define RED_LED PIN_C6
//#define GREEN_LED PIN_A5
#define GREEN_LED PIN_B7
//#define Buzzer PIN_B6
int count;
int1 got_ext=FALSE;
#int_ext2
void_button_isr(void) {
got_ext=TRUE;
}
void main(void) {
count = 0;
enable_interrupts(INT_EXT2_H2L);
enable_interrupts(GLOBAL);
printf("*B0*");
//output_low(RED_LED);
//output_high(GREEN_LED);
//output_high(BUZZER);
long reading;
long readingV;
long Threshold;
setup_adc_ports(sAN2);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(2);
setup_comparator(NC_NC_NC_NC);
// Sw_On_a ();
long sumA = 0;
long s = 0;
long smp_bufA[NSAMPLES];
long adc_res = 0;
long adc_filt = 0;
while(TRUE) {
//output_high(GREEN_LED);
delay_ms(1000);
if(got_ext){
count++;
printf("*SV100*");
output_high(GREEN_LED);
delay_ms(100);
output_low(GREEN_LED);
printf("*SV0*");
got_ext=FALSE;
}
printf(" COUNTS:%d \n\r", count);
// printf("*LR0G255B0*");
//printf("*SV100*");
switch (count) {
case 1:
// printf("*SV100*");
printf("*B0*");
printf("*SV0*");
break;
case 2:
// printf("*SV100*");
printf("*B1*");
printf("*SV0*");
break;
case 3:
// printf("*SV100*");
printf("*B2*");
printf("*SV0*");
break;
case 4:
// printf("*SV100*");
printf("*B3*");
printf("*SV0*");
break;
case 5:
// printf("*SV100*");
printf("*B4*");
printf("*SV0*");
break;
case 6:
// printf("*SV100*");
printf("*B5*");
printf("*SV0*");
break;
case 7:
// printf("*SV100*");
printf("*B6*");
printf("*SV0*");
break;
case 8:
// printf("*SV100*");
printf("*B7*");
printf("*SV0*");
break;
case 9:
// printf("*SV100*");
printf("*B8*");
printf("*SV0*");
break;
case 10:
// printf("*SV100*");
printf("*B9*");
printf("*SV0*");
break;
case 11:
printf("*LR255G0B0*");
printf("*B10*");
printf("*SV0*");
break;
default:
break;
}
}
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Fri Oct 04, 2019 7:43 am |
|
|
In main() you should clear the interrupt, then delay a bit, then enable. Remember you need the PIC to 'settle down' BEFORE you ask it to 'run'. Depending on the pullup, VDD,PB, etc. There is a physical hardware 'setup' time.
Typically I delay_ms(10) before I ask the PIC to run the real program.
Also, you need to add 'ERRORS' to the #USE RS232(...options...)
and, check the ADC section of the datasheet to see IF ADC_CLOCK_INTERNAL is VALID for the actual PIC clock speed. For most PICs, it can only be used while asleep, but I don't use that PIC, so best to check !
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Fri Oct 04, 2019 10:51 pm |
|
|
If you read the data sheet, you will see that the act of setting which
edge the interrupt is to trigger on, may result in the interrupt triggering.
This is what you are seeing.
Use:
Code: |
enable_interrupts(INT_EXT2_H2L);
clear_interrupts(INT_EXT2);
enable_interrupts(GLOBAL);
|
Jay has already pointed out some other issues. |
|
|
|
|
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
|