Top Banner
Programming in C Chapter 10
21

Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

Mar 12, 2018

Download

Documents

docong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

Programming in C

Chapter 10

Page 2: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

Lesson 11

C Programming Examples for

Interrupts

Page 3: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education3

8051 versions 5 or 6 or 7 standard hardware

interrupts

• INT0, T0, INT1, T1, serial port and T2

• Can be numbered as 0, 1, 2, 3, 4, 5, …

• The vector addresses (also the service routine

start address in case of 8051) start from

0x0003, 0x000B, 0x0013, … .

Page 4: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education4

8051 versions 5 or 6 or 7 standard hardware

interrupt vector addresses

• Also the service routine start address in case

of 8051) start from 0x0003, 0x000B, 0x0013,

… .

• New versions add more interrupts

Page 5: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education5

Cx51 32 interrupt vector addresses

• Cx51 compiler provides for support up to 32

interrupt numbers

• Numbered as 0, 1, 2, 3, 4, 5, … , 30 or 31

• Vector addresses 0x0003 + interrupt number

× 0x0008

Page 6: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education6

Two SFRs IE and IP

• IE for enabling the actions on interrupts

• IP for specifying the priorities over the default

priorities

• Lower the interrupt number higher is default

priority

Page 7: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education7

Interrupt function attribute

• Interrupt number

• Cx51 compiler enables the specification of the

attribute after the name of the function and the

word interrupt

Page 8: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education8

The interrupts numbers

• 0 or 1 or … INT0, T0, INT1, T1, serial port

and T2 are numbered as 0, 1, 2, 3, 4 and 5

• Interrupt number= 1 for the timer T0 function

for the interrupt

Page 9: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education9

Cx51 compiler features

• Enables that each Interrupt function can be

assigned a bank to use the registers

• Instructions using registers can thus be short

Page 10: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education10

Cx51 compiler

• If required, the saves the contents of DPH,

DPL, ACC, B, and PSW SFRs on the stack

when the interrupt function called

• The registers required in the interrupt function,

which are used by other functions, are also

saved on to stack, if they are not in the bank

specified as the interrupt attribute

Page 11: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education11

Advantage for association of register-

bank with the interrupt function

• When the specific bank are associated with the

specific interrupt number— the Context

switching instructions reduced

• Context switches fast

• Push of previous function variables to the

stack and then pop from the stack later before

return most often not be required

Page 12: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education12

Example Interrupt bank number 3

• Bank 3 assigned for the timer T0 function for

the interrupt

Page 13: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education13

Interrupt function declarations

• May not or may include a return value

• The return value specified similar to one in a C

function

Page 14: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education14

C program configuring the INT1 interrupt at

pin 3 at P3 as falling edge interrupt

• Use of IE and TCON registers

• Assume interrupt function returns the number

of falling edges at the pulses at INT1 pin

• Assume interrupt disabled on 1000th falling

edge

Page 15: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education15

C Program

#include <reg51.h> /* Include header file for the

registers and SFRs of 8051. */

void main (void)

{

unsigned int numEdges = 0; /* Declare variable

numEdges as 16-bit positive integer and assign

it initial value = 0 */

Page 16: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education16

Main Program continued

IT1 = 1; /* Configure interrupt 1 for falling edge on INT1 pin P3.3 */

EA = 1; /* Enable Global interrupt */

EX1 = 1; /* Enable EX0 interrupt */

. /* remaining statement in main. */

while (1) { /* Wait endlessly */

; } /* End of the while loop */

} /* End of the main */

Page 17: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education17

Interrupt Function

unsigned int int1ISR (void) interrupt 1 using 2 {

if (numEdges < 1000){ /* count INT1

interrupts from 0 up to 3 */

numEdges ++; } else

{numEdges = 0; /* Reset Count INT1 interrupts

from 0 on next falling edge, */

Page 18: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education18

Interrupt Function

EX1= 0}; /* and disable INT1 interrupts on

1000 falling edge. */

return (numEdges); /* Function returns an

unsigned integer numEdges */

} /* End of interrupt function for INT1 */

Page 19: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

Summary

Page 20: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

2011 Microcontrollers-... 2nd Ed. Raj Kamal

Pearson Education20

We learnt

• Programming for INT pin Interrupts

• Program for INT1 interrupt at pin 3 at P3 as

falling edge interrupt

Page 21: Programming in C · PDF fileC Programming Examples for Interrupts . 2011 Microcontrollers- ... 8051 versions 5 or 6 or 7 standard hardware interrupts • INT0, T0, INT1, T1, serial

End of Lesson 11 on

C Programming Examples for

Interrupts