INTERRUPT PROGRAMMING IN C

SECTION 11.6: INTERRUPT PROGRAMMING IN C
So far all the programs in this chapter have been written in Assembly. In this section we show how to program the 8051/52′s interrupts in 8051 C language. In reading this section, it is assumed that you already know the material in the first two sections of this chapter.
8051 C interrupt numbers
The 8051 C compilers have extensive support for the 8051 interrupts with two major features as follows:
  1. They assign a unique number to each of the 8051 interrupts, as shown in Table
    11-4.
  2. It can also assign a register bank to an ISR. This avoids code overhead due to
    the pushes and pops of the RO – R7 registers.
    Table 11-4: 8051/52 Interrupt Numbers in C

Example 11-14 shows how a simple interrupt is written in 8051 C.



Example 11-14
Write a C program that continuously gets a single bit of data from PI. 7 and sends it to Pl.O, while simultanepusly creating a square wave of 200 (as period on pin P2.5. Use timer 0 to create the square wave. Assume that XTAL = 11.0592 MHz.
Solution:




Example 11-15
Write a C program that continuously gets a single bit of data from PI. 7 and sends it to Pl.O in the main, while simultaneously (a) creating a square wave of 200 us period on pin P2.5, and (b) sending letter ‘A’ to the serial port. Use Timer 0 to create the square wave. Assume that XTAL = 11.0592 MHz. Use the 9600 baud rate.
Solution:


Example 11-16
Write a C program using interrupts to do the following:
  1. Receive data serially and send it to PO,
  2. Read port PI, transmit data serially, and give a copy to P2,
  3. Make timer 0 generate a square wave of 5 kHz frequency on PO.l.
    Assume that XTAL = 11.0592 MHz. Set the baud rate at 4800.
    Solution:




Example 11-17
Write a C program using interrupts to do the following:
  1. Generate a 10000 Hz frequency on P2.1 using TO 8-bit auto-reload,
  2. Use timer 1 as an event counter to count up a 1-Hz pulse and display it on PO. The
    pulse is connected to EX1.
    Assume that XTAL = 11.0592 MHz. Set the baud rate at 9600.






SUMMARY
An interrupt is an external or internal event that interrupts the microcontroller to inform it that a device needs its service. Every interrupt has a program associated with it called the ISR, or interrupt service routine. The 8051 has 6 interrupts, 5 of which are user-accessible. The interrupts- are for reset: two for the timers, two for external hardware interrupts, and a serial communication interrupt. The 8052 has an additional interrupt for Timer 2.
The 8051 can be programmed to enable or disable an interrupt, and the interrupt priority can be altered. This chapter showed how to program 8051/52 interrupts in both Assembly and C languages.

Next post:

Previous post: