Showing posts with label PIC programming. Show all posts
Showing posts with label PIC programming. Show all posts

Interrupts' and their usage in pic microchip circuit design

 

1. What is an interrupt?
Basically CPU executes the instructions of the program one by one, doing what it is told in a precise and linear way. An interrupt disturbs this order. Coming maybe when least expected, its function is to alert the CPU that some significant external event has happened, to stop it from what it is doing and force it (at greatest speed possible) to respond to what has happened. Originally this was applied to allow emergency external events to get the attention of the CPU, emergencies like power failure, the system overheating or major failure of a subsystem. Then CPU takes necessary actions

2. What is an ISR? What command is used in PIC to return from an ISR?
ISR is interrupt service routine so that basically handle the necessary actions should be taken when some interrupt happens. We can use ISR by configuring following few steps
ü    Start the ISR at the interrupt vector, location 0004
ü    Enable the interrupt that is to be used, by setting the enable bit in the INTCON register
ü    Set the Global Enable bit, GIE
ü    Clear the interrupt flag within the ISR

End the ISR with a retfie instruction. RETFIE is exactly like a return, but it also sets the global interrupt enable (GIE). When a hardware interrupt occurs, it clears GIE and executes what amounts to a CALL instruction. Using RETFIE allows us to enable interrupts and return to the main program all in one step

3. What is Interrupt Latency?
The purpose of the interrupt is to attract the attention of the CPU quickly, but actually how quickly does this happen? The time between the interrupt occurring and the CPU responding to it is called the latency. Actually this is based on hardware configurations of the device

4. What are Interrupt flag bits and mask bits?

Flag bits-The occurrence of the interrupt, even if it is only momentary, is thus recorded. The output of the bistable, the latched version of the interrupt, is called the interrupt flag. This is then gated with an enable signal, Interrupt is enable. Basically we checked flag bits to detect weather interrupt is occurs or not

Mask bits- These bits are used to select which interrupts can be enable. Some interrupts can turn off and turn on. So do that we can use those bits. The action of disabling an interrupt is sometimes called masking. some microcontrollers have interrupts that cannot be masked. These are always external and are used to connect to external interrupt signals of the greatest importance

5. What is the bit that needs to be set in order to enable External interrupt on RBO (0th bit of PortB)?
    bsf intcon,inte
    And we must select global interrupt enable bit

6. What are timers? What is a pre-scalar?
We can use timers to maintain periodic events. Timers provide the timing for a range of activity such as motor controlling and various other applications
        The external input path includes the option of inverting the signal with the     Exclusive OR gate, the inversion being controlled by bit T0SE which is appear in     option register. The output of the first multiplexer branches before reaching a     second multiplexer. This selects     either a direct path or the path taken through a     programmable prescaler.we can set 1 trigger for 255 triggers by setting this
7. What is a watch dog timer?
Big problem of computer system is that the software fails in some way and that the system locks up or becomes unresponsive. In a desktop computer such lock-up can be annoying and one would normally have to reboot. In an embedded system it can be disastrous, as there may be no user to notice that there is something wrong and maybe no user interface anyway. If we enable this timer it’s perioditically counting up and it reaches top value of it then it reset the program (reset means go to the beginning of the program) if we neet to avoid reset we have to clear that by our program time to time.

Timer0 interrupt generation Assembly code for microchip pic16f877

list p=16f877
include <dev_fam.inc> ;this file is checked and corrected
include <math16.inc> ;
include <p16f877.inc>
;**********************************************************
;DEFINITIONS:CONSTANTS
FALSE equ 0
TRUE equ 1
PP0 equ b'00000000' ;PIC program page
PP1 equ b'00001000'
PP2 equ b'00010000'
PP3 equ b'00011000'
;------------------------------------------------------------
; RAM variables
    ORG 0X20
;-------------------------------------------------------------
PROGRAMPAGE set PP0
    org 0x00 ;RESET VECTOR
    goto start ;RUN main routine
    org 0x04 ;Interrupt VECTOR
;you can enter your interrupt check routines here
    goto ISR
;------------------------------------------------------------------
    org 0xA0
    start
    ;code for Initialization of Timer0 and other bits
       BANKSEL   INTCON ;BANK2
       MOVLW   0xA0
       MOVWF   INTCON ;Global interrupt and TMR0 interrupt enabled
    ;set timer 0
    banksel TMR0
    movlw b'00000000'   
    movwf TMR0
    ;get one timer 0 inturrupt after 256 triggers
    banksel OPTION_REG
    movlw b'10000110'   
    movwf OPTION_REG
    ;set port B as output
    banksel TRISB
    movlw b'11110000'
    movwf TRISB        
    banksel PORTB
    movlw b'00000000'
    movwf PORTB
    loop
    ;loops forever until inturrupts come
    goto loop
    ISR
    ;handling the interrupt
       comf  PORTB,1    ; Toggle Port B
      BANKSEL   INTCON ;BANK0
       BCF      INTCON,2 ;CLEAR INTERRUPT FLAG
    Retfie ;return and set global in turrupt enable
    end
--------------------------------------------------------------------------

Interrupt service routine handler- programmable Integrated circuit

 

When trigger switch connected to int/RB0 that will start counting 0 to 10 we can get out put from RA0 toRA3 as 0000 to 1010 use with pic = 16F84

org 0x00                     ;This is where we come on power up and reset

;*******************SETUP CONSTANTS*******************
INTCON         EQU 0x0B       ;Interrupt Control Register

PORTB           EQU 0x06       ;Port B register address

PORTA          EQU 0x05       ;Port A register address

TRISA         EQU 0x85       ;TrisA register address

TRISB            EQU 0x86       ;TrisB register address

STATUS          EQU 0X03      ;Status register address

COUNT          EQU 0x0c         ;This will be our counting variable

TEMP              EQU 0x0d      ;Temporary store for w register

  goto    main                              ;Jump over the interrupt address

;***************INTERRUPT ROUTINE***************

  org                  0x04                ;This is where PC points on an interrupt

  movwf TEMP              ;Store the value of w temporarily

incf                  COUNT,1       ;Increment COUNT by 1, and put the result

;back into COUNT

movlw 0x0A               ;Move the value 10 into w

subwf              COUNT,0       ;Subtract w from COUNT, and put the

;result in w

btfss                STATUS,0       ;Check the Carry flag. It will be set if

                                                ;COUNT is equal to, or is greater than w,

                                                ;and will be set as a result of the subwf

                                                ;instruction

goto                carry_on           ;If COUNT is <10, then we can carry on

goto                clear                 ;If COUNT is >9, then we need to clear it

carry_on

bcf                  INTCON,0x01 ;We need to clear this flag to enable

  ;more interrupts

movfw TEMP              ;Restore w to the value before the interrupt

  retfie                                      ;Come out of the interrupt routine

clear

clrf                  COUNT          ;Set COUNT back to 0

bcf                  INTCON,1      ;We need to clear this flag to enable

                                                ;more interrupts

retfie                              ;Come out of the interrupt routine

;*******************Main Program*********************

main

;*******************Set Up The Interrupt Registers****

bsf                   INTCON,7      ;GIE – Global interrupt enable (1=enable)

bsf                   INTCON,4      ;INTE - RB0 Interrupt Enable (1=enable)

bcf                  INTCON,1      ;INTF - Clear FLag Bit Just In Case

;*******************Set Up The Ports******************

  bsf                STATUS,5     ;Switch to Bank 1

movlw 0x01           

movwf TRISB              ;Set RB0 as input

movlw 0x10                       

movwf TRISA              ;Set R 0 to RA3 on PortA as output

  bcf                  STATUS,5  ;Come back to Bank 0

;*******************Now Send The Value Of COUNT To Port A           

loop

movf                 COUNT,0       ;Move the contents of Count into W

movwf              PORTA           ;Now move it to Port A

goto                 loop                 ;Keep on doing this

end                                           ;End Of Program

 

image

if you need more details go to http://www.mstracey.btinternet.co.uk

There many more things about pic and interrupt service routine .This works for me with Mplab ide and proteous 7 environment

Nuwan added this:

1. You use RC OSC and no capacitor. (R1 and OSC1) This is ok with some pic's but, some are fail. You use distributed capacitance of Rosc pin and it is better to connect 20pF cap to ground.


2. RB0 ,Int 0 RC low pass filter R2 value is too large. Use between 2K ~ 10K value.

Empowering the Future of API Management: Unveiling the Journey of WSO2 API Platform for Kubernetes (APK) Project and the Anticipated Alpha Release

  Introduction In the ever-evolving realm of API management, our journey embarked on the APK project eight months ago, and now, with great a...