Coder Social home page Coder Social logo

interrupts-pic's Introduction

Interrupts in PIC Microcontrollers (PIC16F887):

Interrupts are a crucial feature in microcontrollers like the PIC16F887. They allow the microcontroller to respond to external events or internal conditions promptly, without having to continuously poll for them. This improves efficiency and responsiveness in embedded systems.

Types of Interrupts:

1- External Interrupts: These interrupts are triggered by external events such as a change in voltage level on a pin, a timer overflow, or a signal from another device. The PIC16F887 has multiple external interrupt pins (e.g., RB0/INT, RB7/INT0) that can be configured to trigger interrupts.

2- Timer Interrupts: The microcontroller's built-in timers can generate interrupts when they overflow or match a specific value. This feature is useful for tasks that require precise timing, such as generating PWM signals or measuring time intervals.

3- Peripheral Interrupts: Some peripherals within the microcontroller, such as the USART (serial communication module) or the ADC (analog-to-digital converter), can generate interrupts to signal the completion of a data transfer or conversion.

Interrupt Service Routine (ISR):

When an interrupt occurs, the microcontroller suspends its current execution and jumps to a predefined Interrupt Service Routine (ISR) to handle the interrupt. The ISR performs necessary tasks related to the interrupt, such as reading data from sensors, updating variables, or responding to external commands.

Interrupt Priority:

In PIC microcontrollers, interrupts can have different priority levels. This allows the programmer to specify which interrupts should be serviced first if multiple interrupts occur simultaneously. Priority levels can be configured in the microcontroller's interrupt control registers.

Interrupt Configuration:

To use interrupts in a PIC microcontroller like the PIC16F887, the following steps are typically involved:

1- Configure the specific interrupt source (e.g., external pin, timer, peripheral).

2- Enable global interrupts and set the interrupt priority, if necessary.

3- Write the ISR to handle the interrupt.

4- Clear the interrupt flag in the ISR to acknowledge the interrupt and allow subsequent interrupts of the same type to occur.

Example:

    list    p=16F887           ; list directive to define processor
    #include <p16F887.inc>     ; processor specific variable definitions

    __CONFIG _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_OFF & _CPD_OFF & _LVP_OFF & _CP_OFF & _BODEN_ON & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
    __CONFIG _CONFIG2, _IESO_OFF & _FCMEN_OFF
    
    ; Register definitions
    LED_PIN     equ RB3            ; Define LED pin
    INT_PIN     equ RB0            ; Define external interrupt pin

    ; Interrupt vector
    org 0x0004                      ; Interrupt vector location
    goto ISR                        ; Jump to ISR when interrupt occurs

    ; Interrupt Service Routine (ISR)
    ISR:
        btfss   INTCON, INTF       ; Check if RB0/INT interrupt flag is set
        return                     ; If not set, return from ISR
        ; Toggle LED pin
        bcf     INTCON, INTF       ; Clear RB0/INT interrupt flag
        movlw   0x01
        xorwf   LED_PIN, f         ; Toggle LED pin
        retfie                      ; Return from interrupt, enable interrupts

    ; Main program
    org 0x0000                      ; Program start address
    goto Main                       ; Jump to main program

    Main:
        bsf     STATUS, RP0        ; Bank 1
        movlw   0x00
        movwf   TRISB              ; Set PORTB as output
        bcf     STATUS, RP0        ; Bank 0
        bcf     INTCON, INTE       ; Disable RB0/INT external interrupt
        bsf     INTCON, INTEDG     ; Interrupt on falling edge of RB0/INT
        bsf     INTCON, GIE        ; Enable global interrupts
        bcf     INTCON, PEIE       ; Disable peripheral interrupts

    Loop:
        nop                         ; No operation, or other main program instructions
        goto    Loop                ; Endless loop

    end                             ; End of program

Here's a simple flowchart illustrating the main program loop (prog.principal) in the assembly code:

                    +---------------------------------------+
                    |             Start Program             |
                    +---------------------------------------+
                    |                                       |
                    |              Initialize               |
                    |           Configuration               |
                    |               Bits                    |
                    |                                       |
                    +---------------------------------------+
                    |             Main Loop                 |
                    +---------------------------------------+
                    |                                       |
                    |       Check for RB0/INT Interrupt     |
                    |                                       |
                    |       ┌───────────────────────┐       |
                    |       │Interruption Déclenchée│       |
                    |       │   (INTF activé)       │       |
                    |       └───────────────────────┘       |
                    |                 │                   |
                    |       ┌─────────┴─────────┐         |
                    |       │    Toggle LED     │         |
                    |       └───────────────────┘         |
                    |                 │                   |
                    |       ┌─────────┴─────────┐         |
                    |       │  Clear INTF Flag  │         |
                    |       └───────────────────┘         |
                    |                 │                   |
                    |       ┌─────────┴─────────┐         |
                    |       │   Return from ISR  │         |
                    |       └───────────────────┘         |
                    |                                       |
                    +---------------------------------------+
                    |             End Program               |
                    +---------------------------------------+

By ALI RCHAM (3L11X) & RAYAN BOUMEHDI (SRA9ZIT)

interrupts-pic's People

Contributors

3l11x avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.