1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Interrupt programming

Discussion in '8051/8951' started by Parth86, Jan 2, 2014.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    511
    Likes:
    5
    I want to know about interrupt actually how they work in programming why we need interrupt
    If interrupt are generated by hardware called by hardware interrupt (generated by controller)
    If interrupt are generated by software called programming interrupt

    can anyone explain interrupt programming with small example
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,147
    Likes:
    907
    Location:
    Rochdale UK
    Once you get your head around interrupts... You'll wonder why you have never used them before...
    I have written a simple article using the midrange pic chip ..."Simple interrupts"... If you need to use the interrupts on an 8051, then I will do some on that platform.
     
  3. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,887
    Likes:
    500
    Location:
    Cleveland, OH, USA
    8051 is not that popular here, but maybe an example from PIC will be helpful. It is in Assembly (MPASM), which has some similarity to what you use.

    I am far from conversant on interrupts. One reason to use an interrupt is as an alternative to polling to detect a change in state. Since the MCU is not occupied with polling, it can get other work done.

    This thread: http://www.electro-tech-online.com/threads/cant-set-bit-0-in-register-asm.139356/ has two examples of interrupt driven routines for UART written by Mike (K8LH). You can see how the 104-uS interrupts from a timer are used instead of MCU delays. That frees up the MCU for handling the logic and steering needed for communication. It is half duplex. It can both transmit and receive, but not at the same time.

    John
     
    Last edited: Jan 2, 2014
    • Informative Informative x 1
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    511
    Likes:
    5

    thanks for giving important time . PIC controller isn't in course 8051 is in my course I want to generate delay with timer interrupt
    I know little bit about timer
    set control bit means timer start
    TR0=1; start timer
    TR1=1; start timer

    clear control bit means stop the timer

    TR0=0; stop timer
    TR1=0; stop timer

    TIME MODE- there are four mode of timer
    mode 0
    mode 1
    mode 2
    mode 3

    how to write code for timer interrupt
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,147
    Likes:
    907
    Location:
    Rochdale UK
    And similarly.. If you use interrupts you have to switch them on.. There is a register in the 8051 called the IE register.. Once you turn on an interrupt the appropriate flag will re-direct program flow automatically through the corresponding vector..

    In this case Timer0 interrupt vector T0 at vector address 0x0B If a jmp statement is place at this address then the program can run some code at this jump and return to whatever it was doing before the flag was set!!


    I see you are going to work in C ( TR0 = 1; ) C statement..

    With SDCC ( compiler I use )

    Code (c):

    void timer_isr (void) __interrupt (1)
        {
        variable++;    // Increment the count
        }
    // Then in the main..

    TR0 = 1; // Timer 0 on..
    ET0 = ;  // Timer 0 Interrupt on..
    EA = 1; // global interrupts on..
    // Now every overflow on timer 0  will kick the interrupt code in..
     
     
    • Like Like x 1
  7. Jon Wilder

    Jon Wilder Active Member

    Joined:
    Oct 22, 2010
    Messages:
    859
    Likes:
    82
    Location:
    Fresno, CA
    The beauty of the 8051 is that it uses vectored interrupts. Each interrupt source has its own interrupt vector address. The external interrupt 0 vector (i.e. the INT0 pin) is code address 0x0003, timer 0 interrupt is at address 0x000B, external interrupt 1 (i.e. the INT1 pin) is at address 0x0013, the timer 1 interrupt is at address 0x001B, and the UART interrupt is at address 0x0023.

    Notice there is 8 bytes of space between each interrupt vector. This means you will want no more than your context saving instructions along with a jump instruction that jumps the PC to some other section of the code memory that contains the interrupt handler to keep your interrupt code from overwriting another interrupt vector.

    Here is an example of how I would set up timer 0 interrupts and serial port interrupts in assembly for the 8051 -

    Code (text):



                org        0x0000            //reset vector
                ajmp        START

                org        0x000B            ;timer 0 interrupt vector
                push        ACC            ;store accumulator
                push        PSW            ;store program status word
                ajmp        T0Int            ;proceed to timer 0 interrupt
    T0IntExit:        pop        PSW            ;restore program status word
                pop        ACC            ;restore accumulator
                reti                    ;return to main code

                org        0x0023
                push        ACC            ;store accumulator
                push        PSW            ;store program status word
                jb        TI,SerTxInt
                jb        RI,SerRxInt        ;exit if no serial interrupt
    SerIntExit:        pop        PSW            ;restore program status word
                pop        ACC            ;restore accumulator
                reti                    ;return to main code

    T0Int:            ;timer 0 interrupt goes here
                ajmp        T0IntExit

    SerTxInt:        ;serial transmit interrupt handler goes here
                ajmp        SerIntExit        ;done, exit interrupt

    SerRxInt:        ;serial receive interrupt handler goes here
                ajmp        SerIntExit        ;done, exit interrupt

                org        0x0100

    START:            mov        TMOD,0x21        ;timer 0 16-bit timer
                                ;timer 1 8-bit autoreload for UART
                                ;baud rate generator
                mov        TH0,0x00        ;clear timer 0
                mov        TL0,0x00
                mov        TH1,0xFE        ;set UART baud for 31.25Kbps
                mov        TL1,0xFE        ;w/12MHz xtal
                mov        TCON,0x50        ;timer 0 and timer 1 on
                mov        SCON,0x50        ;8-bit asynchronous UART
                mov        IE,0x92            ;enable serial and timer 0 interrupts

                ;place start of main code here
       
                end
     
    With PIC 16F, you have one interrupt vector for all interrupt sources and you must query each interrupt bit to figure out which piece of hardware set the interrupt condition.
     
    • Like Like x 1
  8. rumpfy

    rumpfy Active Member

    Joined:
    May 26, 2012
    Messages:
    384
    Likes:
    61
    Location:
    Melbourne, Australia
    Vead,
    If the comments above are OK for you then good. However, I feel your question is looking for something more basic, so I will give a more basic story.
    With the operation of a digital computer, there are many tasks and functions performed, and these tasks will, or maybe, operate at different speeds. For example, to add two numbers together may take say 10 microseconds, but to print the answer may take say 10 seconds on a slow printer. So what does the computer do while the printer is printing. This kind of speed difference between the fastest device in the system and the slowest device in the network needs to be considered in the design of the computer hardware and the software that drives the program. This is what the 'interrupt' system does.
    The computer will at some point in the program it is executing, will have cause to stop calculating because it needs some information or it needs to wait for something to happen. What will be done is for the program to instruct the hardware (for example a clock), and the program will tell the clock to start timing. Then, the program will "set a flag", and then will go back to the point in the program where it left off. If there is nothing for the computer to do, the computer will go into a 'wait state', and will wait for the clock to finish its timing sequence. When the clock has timed out, the flag will tell the control unit that the time interval has completed, and the computer will then go and do what it has to do ( for example read a voltmeter reading). However, if the computer has some unfinished task when the clock is timing, then the program design will cause the computer to go back to its task of say calculating a result while waiting for the clock to finish.
    Interrupts can be quite complex in that they can be assigned different priority levels, and one interrupt can interrupt another interrupt of a lower priority. It is also possible in the hardware design of the interrupt structure, for the interrupt system to be turned off under software control.
    Generally for small systems, use of the interrupt system is not really useful, but where a system is dealing with lots of relatively slow I/O hardware and has lots of calculations to do, then use of the interrupt system can significantly improve the speed of getting the job done.
    In one system I had to work with, the I/O consisted of a slow character printer, a capacitance bridge measuring capacitances of devices connected to the bridge through a switching matrix and doing calculations and statistical analysis of the measured results. This system used the interrupt system to drastically speed up the testing time. The computer memory was only 4K words and results could not be stored because of the memory capacity of the computer; results had to be printed as soon as they were calculated.
    Sorry if this is too basic; but your question I thought was at this level.
     
    • Like Like x 1
  9. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    511
    Likes:
    5
    I would like to thanks for all member who help me special thanks for rumpfy ,
    I like the way of explaination thats very useful information
     
  10. Jon Wilder

    Jon Wilder Active Member

    Joined:
    Oct 22, 2010
    Messages:
    859
    Likes:
    82
    Location:
    Fresno, CA
    One place that is extremely wise to use interrupts is for serial port receive. The MCU never knows when some external serial device is going to pipe data down to the serial port. Rather than have the main function be the MCU continuously waiting for serial data, we can instead enable the serial port receive interrupt. This frees the MCU up to do other more important tasks, while only having to pay attention to the serial port at the time at which a byte is received and the interrupt condition is set.

    Now in the case of synchronous serial where you have master and slave devices (such as SPI and I2C), serial data is not sent or received until the master says to do so. For these applications it's not really necessary to use an interrupt since the master always knows when data will be piped down to it. But for asynchronous applications where everything runs on its own internal baud timer and a transmitting device can send data at any time it wants, serial interrupts are extremely necessary.

    Just my $0.02.
     
    • Like Like x 1
    • Informative Informative x 1

Share This Page