PIC 16f1829 interrupt all the code ?!

Thread Starter

randomqwertyuiop

Joined Apr 27, 2018
3
hi,
not sure if this is the right sub to post;
basically I can't make a simple counter from external interrupt...
i can make a counter and a interrupt, I can make a delay, but now I need to make an external interrupt that will start a timer then keep increment 'ing that 'counter' until external stimulus changes...

the problem is that i'm not only very newbie but also very stupid since it seems to be the easiest thing ever just not for me ...
honestly don't understand what i'm doing wrong because my interrupt stops the entire PIC and I feel like I want to die.

I wanted to make a simple program without interrupt but it is a must ... so idk what to do I tried getting help from IRL teachers and colleges but they either trolling me or they don't know either since nothing has helped me so far.

I'm NOT looking for some one to make my work... I really need help I want to understand this ...
Code:
#include <xc.h>
int count = 0;
int aux = 0;
int aux2 = 0;
int tempoL; // utiliza 4 bytes(tipo INT);
int tempoH;

void setup () {
    ANSELAbits.ANSA2 = 0;
    OSCCONbits.IRCF = 0b1110;    // 8Mhz INTOSC
    INTCONbits.GIE = 1;          // HABILITA INTERRUPÇÃO GLOBAL
    INTCONbits.PEIE=1;           // HABILITA INTERRUPÃO POR PERIFERICOS
    INTCONbits.IOCIE = 1;        //HABILITA INTERRUPÇÃO POR MUDANÇA DE FLANCO
    IOCAPbits.IOCAP2 = 1;        //INTERRUPÇÃO NO FLANCO POSITIVO
    IOCANbits.IOCAN2 = 1;
    T1CONbits.T1CKPS0 = 1;       // TIMER1 PRESCALER 1:8
    T1CONbits.T1CKPS1 = 1;
    PIE1bits.TMR1IE = 1;
    T1CONbits.TMR1ON = 0;
    TMR1L = 0x00;        //CARREGA TIMER1 COM O VALOR 3035
    TMR1H = 0x00;
}

void main(void) {
    TRISC = 0b00000101;
    PORTC = 0b00000000;
    TRISA = 0b00000100;
    int setx = 0;
    while(1)
    {
        if (aux==1)
        {
            PORTCbits.RC1=1;
        }
        if (PORTCbits.RC0==0)
        {
            PORTCbits.RC1=0;
        }
        if ((setx==0))
        {
            setx=1;
            setup();
        }
    }
    return;
}

void interrupt a(){
   
    if(IOCAFbits.IOCAF2 == 1){  // TESTA A FLAG DE MUDANÇA DE ESTADO RA2
        IOCAFbits.IOCAF2 = 0;   //BAIXA FLAG
        if(aux == 0){           //
            count = 0;
            tempoL = 0;
            tempoH = 0;
            aux++; 
            TMR1L = 0xDB;        //CARREGA TIMER1 COM O VALOR 3035
            TMR1H = 0x0B; 
            T1CONbits.TMR1ON = 1;// ARRANCA TIMER1
        }else {
            if(aux==1){         //
                tempoL = TMR1L;  // O VALOR NO TIMER1 FICA NA VARIAVEL TEMPO (VALOR QUE CONTOU DESDE O ULTIMO REARME)
                tempoH = TMR1H;
                T1CONbits.TMR1ON = 0;// STOP TIMER1
                aux =0 ;          //REINICIA AUX
                aux2 = 1;
            }
        }
    }
    if(PIR1bits.TMR1IF == 1){   // TESTA FLAG DE ESTOURO TIMER0
        PIR1bits.TMR1IF = 0;    //BAIXA A FLAG
        count++;                //INCREMENTA CONTADOR
        TMR1L = 0xDB;
        TMR1H = 0x0B;          //REINICIA TIMER1 COM O VALOR 3035
    }
}
The code is not complete I didn't make the supposed counter on the main void yet I know,
but the code is not working as intended anyway...
so I need it to accept interrupt on at external pin, start a timer... an keep counting until the external pin that started the timer is changed.
but what it does is a simple delay to the entire PIC...
 

jpanhalt

Joined Jan 18, 2008
11,087
Welcome to AAC.

Can't comment on code in C. I do Assembly. One thing I do, if I cannot hardware simulate it, is to use an LED (LED + resistor in series) on a port pin. Then I flash or set the LED when the code gets to that point. I doubt the interrupt completely stops your code unless your interrupt doesn't have an error handler that will let it return. FYI, I used that today to find an incorrect baud setting. In my experience, the 16F1829 is reasonably simulated with MCP products.

John
 

AlbertHall

Joined Jun 4, 2014
12,625
You could use the MPLAB simulator. Set a breakpoint at the start of the interrupt service routine and set up up an asynchronous input on A2.
When you change A2 you can follow what happens in the interrupt routine.
 

Thread Starter

randomqwertyuiop

Joined Apr 27, 2018
3
Welcome to AAC.

Can't comment on code in C. I do Assembly. One thing I do, if I cannot hardware simulate it, is to use an LED (LED + resistor in series) on a port pin. Then I flash or set the LED when the code gets to that point. I doubt the interrupt completely stops your code unless your interrupt doesn't have an error handler that will let it return. FYI, I used that today to find an incorrect baud setting. In my experience, the 16F1829 is reasonably simulated with MCP products.

John
I see what you mean flashing the LED when/where I think the code stops...? (elaborated more about this below )
Not sure what an error handler is so will research about that thank you.
MCP?

I have been told that sometimes simulation wont work, but will after making it irl; or that it's a simple software bug .... :S idk I have seen bigger and more complicated things working fine, almost sure it's a problem with the code...
ye the code works on mplab mostly as expected( i have narrowed the problem down to void setup the INTCON and/or IOCAP bits ).

but in proteus it's just a mess, it doesn't fully stop the code, delays it, i'm not english native that was my bad.~
(for me it's stopping the code since i wanted the interrupt only on RA2, and anything else is delayed ie: the simple line of ' if RC0==1 then RC1=1' )

what the code does (in proteus) is a simple delay on all the pins, what I expected + intend it to do:
have an expected external interrupt on 1 pin RA2, that will start a counting/timer process, while the rest of the code keeps running, and then stop the timer when RA2 changes state, (depending on what the timer number is, this is for a length measurement with IR sensors will be the length of the thing passing by the IR after math...


You could use the MPLAB simulator. Set a breakpoint at the start of the interrupt service routine and set up up an asynchronous input on A2.
When you change A2 you can follow what happens in the interrupt routine.
the interrupt works and I can see it, but i'm not sure why it's not working as I intended
 

Thread Starter

randomqwertyuiop

Joined Apr 27, 2018
3
With the simulator you can step through the interrupt one line at a time and see what happens to all the values.
yes...
my problem is i thought i made 1 code but turns out it's not what I expected and now idk where is my problem or well I have an idea about what the prob is and where it's at but idk what to do to fix it
 

jpanhalt

Joined Jan 18, 2008
11,087
An error handler is just a way of doing something to get out of an error situation. In the example mentioned, a wrong baud setting got into the interrupt, turned the LED on, but didn't exit the interrupt at the usual place. That is, turning the LED off was the last step before RETFIE in the interrupt sequence that should have been followed. Knowing that, I was able to pinpoint the reason for branching to the "error handler" and fix it rather quickly.

My first choice is simulation, but unfortunately, MPLab 8.92 provides rather spotty support for hardware simulation of peripherals. Moreover, for the chip I was using, it provided no hardware simulation support.

Using an LED is not my first choice, but it is a nice tool.
 
Top