shsn
Junior Member level 3
Hi,
After reading bits of information on interrupt routine I decided to implement this to test it out. My current program has following routine:
So what I wanted to do was trigger a hardware interrupt on pin RB0 to flash all the LEDs UNTIL RB0 goes low. To achieve this (according to my assumption), I need a loop to keep the LEDs flash but I can't seem to do this or it is not possible to do this with interrupts.
If I load this program onto the PIC and run it, the external interrupt works (when RB0 pin goes to high, triggered by tactile switch) but letting go of the switch (despite the voltage going to 0V) it does not exit the interrupt routine -- it stays in the interrupt routine unless powered down.
If anyone could point me to the right direction or perhaps another topic related to interrupt I'm happy to dig more. Any help is appreciated.
Thank you.
Eric.
After reading bits of information on interrupt routine I decided to implement this to test it out. My current program has following routine:
- check and load ADC value into variable 'adc_value'.
- go through IF statement that matches conditions
- if a match is found turn LEDs
- if no match is found, flash all LEDs
So what I wanted to do was trigger a hardware interrupt on pin RB0 to flash all the LEDs UNTIL RB0 goes low. To achieve this (according to my assumption), I need a loop to keep the LEDs flash but I can't seem to do this or it is not possible to do this with interrupts.
Code:
unsigned int adc_val;
void checkMode() {
if (RA2_bit == 0) {
PORTB = 0b00000010;
}
else {
PORTB = 0b00000100;
}
}
void driveLED(unsigned int adc_val) {
if (adc_val >= 0 && adc_val <= 40) {
PORTD = 0b00000000;
PORTC = 0;
}
else if (adc_val > 41 && adc_val <= 91) {
PORTD = 0b10000000;
PORTC = 0;
}
else if (adc_val > 92 && adc_val <= 153) {
PORTD = 0b11000000;
PORTC = 0;
}
else if (adc_val > 154 && adc_val <= 215) {
PORTD = 0b11100000;
PORTC = 0;
}
else if (adc_val > 216 && adc_val <= 276) {
PORTD = 0b11110000;
PORTC = 0;
}
else if (adc_val > 277 && adc_val <= 337) {
PORTD = 0b11111000;
PORTC = 0;
}
else if (adc_val > 338 && adc_val <= 399) {
PORTD = 0b11111100;
PORTC = 0;
}
else if (adc_val > 400 && adc_val <= 460) {
PORTD = 0b11111110;
PORTC = 0;
}
else if (adc_val > 461 && adc_val <= 521) {
PORTD = 0b11111111;
PORTC = 0;
}
else if (adc_val > 522 && adc_val <= 582) {
PORTD = 0b11111111;
PORTC = 0b10000000;
}
else if (adc_val > 583 && adc_val <= 644) {
PORTD = 0b11111111;
PORTC = 0b11000000;
}
else if (adc_val > 645 && adc_val <= 705) {
PORTD = 0b11111111;
PORTC = 0b11100000;
}
else if (adc_val > 706 && adc_val <= 767) {
PORTD = 0b11111111;
PORTC = 0b11110000;
}
else if (adc_val > 768 && adc_val <= 828) {
PORTD = 0b11111111;
PORTC = 0b11111000;
}
else if (adc_val > 829 && adc_val <= 890) {
PORTD = 0b11111111;
PORTC = 0b11111100;
}
else if (adc_val > 891 && adc_val <= 951) {
PORTD = 0b11111111;
PORTC = 0b11111110;
}
else {
PORTD = 0;
PORTC = 0;
delay_ms(30);
PORTD = 0b11111111;
PORTC = 0b11111110;
delay_ms(30);
}
}
void main() {
TRISA = 0b000111;
PORTA = 0b000000;
ADCON1 = 0b111110; // varying voltage input
TRISD = 0;
PORTD = 0;
TRISC = 0;
PORTC = 0;
TRISB = 0;
PORTB = 0;
TRISE = 0b000;
PORTE = 0b000;
INTCON.GIE = 1;
INTCON.INTE =1 ;
INTCON.INTF = 0;
TRISB0_bit = 1;
OPTION_REG.INTEDG = 1;
checkMode();
do {
adc_val = ADC_read(0);
driveLED(adc_val);
}
while(1);
}
void interrupt(void) {
do {
PORTD = 0;
PORTC = 0;
delay_ms(30);
PORTD = 0b11111111;
PORTC = 0b11111111;
delay_ms(30);
}
while(INTCON.INTF==0);
}
If I load this program onto the PIC and run it, the external interrupt works (when RB0 pin goes to high, triggered by tactile switch) but letting go of the switch (despite the voltage going to 0V) it does not exit the interrupt routine -- it stays in the interrupt routine unless powered down.
If anyone could point me to the right direction or perhaps another topic related to interrupt I'm happy to dig more. Any help is appreciated.
Thank you.
Eric.