INTERRUPT PROGRAMMING

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

INTERRUPT PROGRAMMING

Postby mohdismiaswaly » Wed Apr 24, 2013 4:23 pm

Hello,

I am searching if there any project in the Cytron's website that has interrupt programming code? I am using PIC18F4550 chip. I am connecting pin RB0 with push button switch and pin RB7 with LED. Currently, I am blinking the LED (pin RB7) non stop. So, I would like to introduce interrupt program inside the existing program so that when I push the button switch (pin RB0), the LED will stop blinking.

Any idea?

Thanks
mohdismiaswaly
Apprentice
 
Posts: 39
Joined: Mon Nov 05, 2012 2:51 pm

Re: INTERRUPT PROGRAMMING

Postby shahrul » Wed Apr 24, 2013 7:09 pm

Open PIC18F4550 datasheet, search address INTCON. See at bit 4 of the address, INT0IE: INT0 External Interrupt Enable bit.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: INTERRUPT PROGRAMMING

Postby mohdismiaswaly » Fri Apr 26, 2013 3:30 pm

So I just need to enable that bit into that register?

That's all?

TQ
mohdismiaswaly
Apprentice
 
Posts: 39
Joined: Mon Nov 05, 2012 2:51 pm

Re: INTERRUPT PROGRAMMING

Postby shahrul » Fri Apr 26, 2013 6:43 pm

Enable Interrupt bit and add Interrupt function.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: INTERRUPT PROGRAMMING

Postby mohdismiaswaly » Sat Apr 27, 2013 5:14 pm

Hello Mr.Shahrul,

Can you share with me a simple code under interrupt function in c language?
I just want to test the functionality of interrupt programming.

Thanks in advance..
mohdismiaswaly
Apprentice
 
Posts: 39
Joined: Mon Nov 05, 2012 2:51 pm

Re: INTERRUPT PROGRAMMING

Postby Brian Griffin » Sat Apr 27, 2013 6:42 pm

For example, if you need to have timer0 interrupts, you have to enable the bit in the INTCON register, which is the TMR0IE, and then afterwards switch on the timer0 in T0CON register.

So, you can do this:
CODE: SELECT_ALL_CODE

INTCON.TMR0IF = 0;
INTCON.TMR0IE = 1;

T0CON.TMR0ON = 1;
//other code for setting timer0 here, like prescale and stuff

INTCON.GIE = 1;




You have to do this also, depending on the compiler, you should include a place for the interrupt to jump into when the interrupt flag is up:
CODE: SELECT_ALL_CODE
void interrupt() {

if(INTCON.TMR0IF)
{
   
   // do something here, like reload the timer0 or something.
   INTCON.TMR0IF = 0;  // clear flag.

}

}



Different C compilers for microcontrollers have their own method of handling interrupts. But the concept remains the same.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: INTERRUPT PROGRAMMING

Postby khairul » Tue May 14, 2013 2:09 am

Hai, sorry to INTERUPT... i have a problem regarding with UART and TIMER, i have a code to transmit a serial data to a module and wait for the module to return a packet of data back..

My problem is the coding just wait forever until the packet of data received to the pic due to this coding line
"
CODE: SELECT_ALL_CODE
while(PIR1bits.RCIF==0); //wait for data to received
". and i just want it to wait for a while, if there is no data then leave from the task.

So my solution (if it is possible) is to create a TIMER to wait for a while and at the same time waiting for the packet of data to be received and eventually when the timer is off, i just create a code to jump back to the main.

So it is possible to create code like that?
khairul
Novice
 
Posts: 17
Joined: Wed Feb 02, 2011 2:07 pm

Re: INTERRUPT PROGRAMMING

Postby shahrul » Tue May 14, 2013 7:48 am

khairul WROTE:Hai, sorry to INTERUPT... i have a problem regarding with UART and TIMER, i have a code to transmit a serial data to a module and wait for the module to return a packet of data back..

My problem is the coding just wait forever until the packet of data received to the pic due to this coding line
"
CODE: SELECT_ALL_CODE
while(PIR1bits.RCIF==0); //wait for data to received
". and i just want it to wait for a while, if there is no data then leave from the task.

So my solution (if it is possible) is to create a TIMER to wait for a while and at the same time waiting for the packet of data to be received and eventually when the timer is off, i just create a code to jump back to the main.

So it is possible to create code like that?

This is need to use UART Interrupt. PIC not waiting for the UART data. It will remain run main program, when UART data comes in, PIC automatically read UART. So, PIC not wasting time to waiting for the data.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: INTERRUPT PROGRAMMING

Postby khairul » Tue May 14, 2013 8:54 am

ok, i will look on that... if i cannot solve it, i post again.. tq all especially En.Shahrul for helping me.
khairul
Novice
 
Posts: 17
Joined: Wed Feb 02, 2011 2:07 pm


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 26 guests