ubc104 embedded systems review: interrupts & timers

64
UBC104 Embedded Systems Review: Interrupts & Timers

Upload: karina-lease

Post on 14-Dec-2015

230 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: UBC104 Embedded Systems Review: Interrupts & Timers

UBC104 Embedded Systems

Review: Interrupts & Timers

Page 2: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 2

Block Diagram

Page 3: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 3

Interrupts

Definition of ‘Interrupt’

Event that disrupts the normal execution of a program and causes the execution of special

instructions

Page 4: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 4

Interrupt Handling Code that deals with interrupts:

Interrupt Handler or Interrupt Service Routines (ISRs)

Possible code:

void ISR(void) interrupt 1 {++interruptcnt;

}

Interrupt number

Page 5: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 5

Interrupt Overheads Interrupt arrives

Complete current instruction

Save essential register information

Vector to ISR

Save additional register information

Execute body of ISR

Restore other register information

Return from interrupt and restore essential

registers

Resume task

InterruptLatency

InterruptTermination

Page 6: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 6

SFR Map – Timer

Page 7: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 7

Timer Codevoid TimerInit(void) { T2CON = 0x04; // Load Timer 2 control register

TH2 = 0xFC; // Load Timer 2 high byte RCAP2H = 0xFC; // Load Timer 2 reload capt. reg.

high byte TL2 = 0x18; // Load Timer 2 low byte RCAP2L = 0x18; // Load Timer 2 reload capt. reg. low

byte

ET2 = 1; // Enable interrupt TR2 = 1; // Start Timer 2 running}

void handleTimer2 (void) interrupt 5 {/* execute interrupt code */

}

Initialization

Start of Timer

Interrupt Service Routine

Page 8: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 8

Calculation of Register Settings

Oscillator = 24 MHz Timer clock= (24 / 12) MHz = 2 MHz Timer cycle = = 500ns Delay count = delay time / cycle time

1 ms / 500 ns = 2000 Base number= 213-2000 = 8192-2000 = 6192 = 0x1830

2 MHz1

Page 9: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 9

Summary: Interrupts & Timers Interrupts:

Event that disrupts the normal execution of a program and causes the execution of special instructions

Interrupt Latency: Time from event to execution of service routine

Interrupt Response Time:Interrupt latency + Time for service routine

Interrupt Termination: Time taken after interrupt service routine

Timer: Counter that causes interrupt at overflow

Page 10: UBC104 Embedded Systems Review: Interrupts & Timers

UBC104 Embedded Systems

RS232 Communication

Page 11: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 11

Overview

Basics of serial communications RS232 details 8051-support for serial communication

Page 12: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 12

SFR Map – UART Registers

Page 13: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 13

UART Registers

Page 14: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 14

Serial Interface Basics Also called

Universal Asynchronus

Receiver/ Transmitter (UART)

or after the I standards: RS232 (-C) or EIA232

Page 15: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 15

Serial Interface Basics

also called Universal Asynchronus Receiver/ Transmitter (UART)

or the relevant standards: RS232 (-C) or EIA232

2 Receive Data

3 Transmit Data

5 Signal Ground

Page 16: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 16

Typical 3-wire Interface

3

2

2

3

5

5

Page 17: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 17

uController Pin-Out

Page 18: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 18

RS232 Signals

Signals between +25V and -25V;

some say ±15V

usually +12V to -12V

Page 19: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 19

RS232 Signals

Signals between +25V and -25V;

some say ±15V

usually +12V to -12V Logic ‘1’

Logic ‘0’

Page 20: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 20

RS232 Signals

Signals between +25V and -25V;

some say ±15V

usually +12V to -12V

Page 21: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 21

RS232 Signals

Signals between +25V and -25V;

some say ±15V

usually +12V to -12V 8051 runs on 3V or 5V

Page 22: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 22

RS232 Signals

Signals between +25V and -25V;

some say ±15V

usually +12V to -12V 8051 runs on 3V or 5V

Driver chip translates between voltages

Page 23: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 23

Valid Signals

Page 24: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 24

Valid Signals

Figure courtesy of http://www.camiresearch.com

Page 25: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 25

MCBx51 Board

TX0RX0

23

Page 26: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 26

Basic 3-Wire Connection of Machines

TXD

GND

RXD

Page 27: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 27

Basic 3-Wire Connection of Machines

What goes over these wires?

TXD

GND

RXD

Page 28: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 28

RS-232 Frame

Every RS-232 consists of: 1 start bit 8 data bits 1 stop bit (optional 1 parity bit)

Page 29: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 29

RS-232 Frame

Start D0 D1 D2 D3 D4 D5 D6 D7 Stop

Every RS-232 consists of: 1 start bit 8 data bits 1 stop bit (optional 1 parity bit)

Page 30: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 30

RS-232 Frame

Start D0 D1 D2 D3 D4 D5 D6 D7 Stop

Every RS-232 consists of: 1 start bit 8 data bits 1 stop bit (optional 1 parity bit)

‘a’= 0x61 = 0110 0001

Page 31: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 31

RS-232 Frame

Start D0 D1 D2 D3 D4 D5 D6 D7 Stop

1 1 1 1

Every RS-232 consists of: 1 start bit 8 data bits 1 stop bit (optional 1 parity bit)

‘a’= 0x61 = 0110 0001

1

0

Page 32: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 32

RS-232 Frame

Start D0 D1 D2 D3 D4 D5 D6 D7 Stop

1 1 1 1 0

Every RS-232 consists of: 1 start bit 8 data bits 1 stop bit (optional 1 parity bit)

‘a’= 0x61 = 0110 0001

1

0

Page 33: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 33

RS-232 Frame

Start D0 D1 D2 D3 D4 D5 D6 D7 Stop

1 1 1 1 0 1 0 0 0 0 1 1 0

Every RS-232 consists of: 1 start bit 8 data bits 1 stop bit (optional 1 parity bit)

‘a’= 0x61 = 0110 0001

1

0

Page 34: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 34

RS-232 Frame

Start D0 D1 D2 D3 D4 D5 D6 D7 Stop

1 1 1 1 0 1 0 0 0 0 1 1 0 0

Every RS-232 consists of: 1 start bit 8 data bits 1 stop bit (optional 1 parity bit)

‘a’= 0x61 = 0110 0001

1

0

Page 35: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 35

RS-232 Frame

Start D0 D1 D2 D3 D4 D5 D6 D7 Stop

1 1 1 1 0 1 0 0 0 0 1 1 0 0 1 1

Every RS-232 consists of: 1 start bit 8 data bits 1 stop bit (optional 1 parity bit)

‘a’= 0x61 = 0110 0001

1

0

Page 36: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 36

Signal Timing

1 1 1 1 0 1 0 0 0 0 1 1 0 0 1 1 1 1

Start D0 D1 D2 D3 D4 D5 D6 D7 Stop

?

Page 37: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 37

Signal Timing (continued)

1 1 1 1 0 1 0 0 0 0 1 1 0 0 1 1 1 1

Start D0 D1 D2 D3 D4 D5 D6 D7 Stop

?bit period

Page 38: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 38

Baud Rate

Baud specifies the inverse of the bit-period

e.g. 9600 Baud = a bit-period of 1/9600 second

= 104.2 microseconds

Typicall data rates: 75, 110, 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 33600, 56000, 115000 and (rarely) 330000 baud.

Page 39: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 39

Serial I/O with bare hands#define BIT_PERIOD 1042

sendByte(unsigned char content) {unsigned char mask;

mask= 1;P3.1= 0; // Start bit

wait (BIT_PERIOD);

for (i= 0; i<8; i++) { // Send contentP3.1= (content&mask) ? 1 : 0;

wait (BIT_PERIOD);mask<= 1;

}

P3.1= 0; // Stop bit wait (BIT_PERIOD);

P3.1= 1;}

Page 40: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 40

Serial I/O with bare hands#define BIT_PERIOD 1042

sendByte(unsigned char content) {unsigned char mask;

mask= 1;P3.1= 0; // Start bit

wait (BIT_PERIOD);

for (i= 0; i<8; i++) { // Send contentP3.1= (content&mask) ? 1 : 0;

wait (BIT_PERIOD);mask<= 1;

}

P3.1= 0; // Stop bit wait (BIT_PERIOD);

P3.1= 1;}

Page 41: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 41

Serial I/O with bare hands#define BIT_PERIOD 1042

sendByte(unsigned char content) {unsigned char mask;

mask= 1;P3.1= 0; // Start bit

wait (BIT_PERIOD);

for (i= 0; i<8; i++) { // Send contentP3.1= (content&mask) ? 1 : 0;

wait (BIT_PERIOD);mask<= 1;

}

P3.1= 0; // Stop bit wait (BIT_PERIOD);

P3.1= 1;}

You do not need to do this!!!

Page 42: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 42

UART modes Mode 0: 8 data bits (LSB first) are transmitted/received at a fixed baud rate of

1/12 of the oscillator frequency. (synchronus mode)

Page 43: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 43

UART modes Mode 0: 8 data bits (LSB first) are transmitted/received at a fixed baud rate of

1/12 of the oscillator frequency. (synchronus mode) Mode 1: 10 bits are transmitted (through TXD) or received (through RXD): a

start bit (0), 8 data bits (LSB first), and a stop bit (1) at a variable baud rate.

Page 44: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 44

UART modes Mode 0: 8 data bits (LSB first) are transmitted/received at a fixed baud rate of

1/12 of the oscillator frequency. (synchronus mode) Mode 1: 10 bits are transmitted (through TXD) or received (through RXD): a

start bit (0), 8 data bits (LSB first), and a stop bit (1) at a variable baud rate. Mode 2: 11 bits are transmitted (through TXD) or received (through RXD): a

start bit (0), 8 data bits (LSB first), a programmable 9th data bit, and a stop bit (1). On transmit, the 9th data bit (TB8 in SCON) can be assigned the value of 0 or 1 (for example, the parity bit (P, in the PSW) could be moved into TB8). On receive, the 9th data bit goes into RB8 in Special Function register SCON. The baud rate is programmable to either 1/32 or 1/64 the oscillator frequency.

Mode 3: Mode 3 is the same as Mode 2 in all respects except the baud rate is variable.

Page 45: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 45

UART modes Mode 0: 8 data bits (LSB first) are transmitted/received at a fixed baud rate of

1/12 of the oscillator frequency. (synchronus mode) Mode 1: 10 bits are transmitted (through TXD) or received (through RXD): a

start bit (0), 8 data bits (LSB first), and a stop bit (1) at a variable baud rate. Mode 2: 11 bits are transmitted (through TXD) or received (through RXD): a

start bit (0), 8 data bits (LSB first), a programmable 9th data bit, and a stop bit (1). On transmit, the 9th data bit (TB8 in SCON) can be assigned the value of 0 or 1 (for example, the parity bit (P, in the PSW) could be moved into TB8). On receive, the 9th data bit goes into RB8 in Special Function register SCON. The baud rate is programmable to either 1/32 or 1/64 the oscillator frequency.

Mode 3: Mode 3 is the same as Mode 2 in all respects except the baud rate is variable.

Mode 0 Baud Rate = 12

Oscillator Frequency

Mode 2 Baud Rate = 2 SMOD1 x 64

Oscillator Frequency

Mode 1/3 Baud Rate = 2 SMOD1 x 32

Timer Overflow Rate

Page 46: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 46

SMOD1

SMOD1: Double Baud rate SMOD0: Enables framing error detection

Page 47: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 47

Framing Error Detection

Page 48: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 48

Timer 1

http://www.keil.com/c51/baudrate.asp

Mode 1/3 Baud Rate = 2 SMOD1 x 32

Timer Overflow Rate

Mode 1/3 Baud Rate = 12 x [256 – TH1]

Oscillator Frequency2 SMOD1

x32

Baud Rate[256 – TH1] =

Oscillator Frequency2 SMOD1

x32 12 x

Baud RateTH1 = 256 –

Oscillator Frequency2 SMOD1

x32 12 x

Page 49: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 49

Timer 1 Commonly Used BAUD Rates

Page 50: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 50

SCON Documentation

Page 51: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 51

SCON & SBUF

SCON controls the functionality of the UART device: SM0&SM1 = Determine the mode SM2 = Multiprocessor Flag REN = Receive Enable Flag TB8/RB8 = Transmit/Receive Parity Flag TI/RI = Transmit/Receive Interrupt Flag

SBUF is the transmit&receive buffer

Page 52: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 52

SBUF

SBUF is actually two separate registers: a transmit buffer and a receive buffer register: When data is moved to SBUF, it goes to the transmit buffer

where it is held for serial transmission; moving a byte to SBUF initiates the transmission.

When data is moved from SBUF, it comes from the receive buffer.

Page 53: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 53

SBUF and TI/RI

8 data

SBUF

8

TI

Stop bit

Start bit

Send

8-bitdata

Transmitter

Buffer isempty

10 bitparallelto serialconversion

Serial data transmit

TRANSMITTER HALF

8 data bits

startbit

stopbit

Tx

bitbit

8 data

SBUF

8

RI

Start bit

Stop bit

Receive

8-bitdata

Receive

data is

available

10 bitserial toparallelconversion

Serial data receive

RECEIVER HALF

8 data bits

stopstart

Rx

Page 54: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 54

SBUF and Parity Bit in TB8/RB8

8 dataSBUF

8

TI

Stop bit

Start bit

Send8-bitdata

TransmitterBuffer is

empty

11 bitparallelto serialconversion

Serial data transmit

8 data bitsstart

bitstop

bit

Tx

9th. bit

TB8Put parity bit here

(ninth bit!)

p

parity bit

8 data

SBUF

8

RI

Start bit

Stop bit

Receive8-bitdata

Receivedata is

available

11 bitserial toparallelconversion

Serial data receive

8 data bits

stopbit

startbit

Rx

9th. bit

p

parity bit

RB8Read parity bit here

(ninth bit!)

Page 55: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 55

Timer 1 Example

1. Set T1 for Mode 2

2. Load TH1 with the initial value

3. Set SCON for Mode 1

4. Start Timer1

5. Clear TI

6. Load SBUF with the byte to be transferred

7. Wait until TI becomes 1

8. Go back to Step5 for next byte

TCON= 0x20;

TH1= 0xF5;

SCON|= 0x50;

TR1= 1;

TI= 0;

do {

SBUF= byte[i];

while (!TI);

} while(more_bytes);

Page 56: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 56

Timer 1 Example (continued)

1. Set T1 for Mode 2

2. Load TH1 with the initial value

3. Set SCON for Mode 1

4. Start Timer1

5. Clear RI

6. Wait for byte to be received

7. Copy received byte out of SBUF

8. Return received byte

TCON= 0x20;

TH1= 0xF5;

SCON|= 0x50;

TR1= 1;

RI= 0;

while (!RI);

result= SBUF;

return SBUF;

Page 57: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 57

Possible Configurations

Page 58: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 58

T2CON Documentation

Page 59: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 59

Timer 2 Commonly Used BAUD Rates

Page 60: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 60

Timer 2 Calculation

Mode 1/3 Baud Rate = 32 x [65536 – (RCAP2H, RCAP2L)]

Oscillator Frequency

Baud Rate(RCAP2H,RCAP2L) = 65535 –

Oscillator Frequency

32 x

Page 61: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 61

Timer2 Example

1. Set T1 for Mode 2

2. Load RCAP high/low with the initial value

3. Set SCON for Mode 1

4. Start Timer1

5. Clear TI

6. Load SBUF with the byte to be transferred

7. Wait until TI becomes 1

8. Go back to Step5 for next byte

T2CON|= 0x30;

RCAP2H = 0xFF;

RCAP2L = 0xB2;

SCON|= 0x50;

TR2= 1;

TI= 0;

do {

SBUF= byte[i];

while (!TI);

} while(more_bytes);

Page 62: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 62

Internal Baud-Rate Generator

Page 63: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 63

BDRCON

Page 64: UBC104 Embedded Systems Review: Interrupts & Timers

UBC 104 Embedded Systems 64

Using BRL to Transmit

1. Set T1 for Mode 2

2. Load TH1 with the initial value

3. Set SCON for Mode 1

4. Start Timer1

5. Clear TI

6. Load SBUF with the byte to be transferred

7. Wait until TI becomes 1

8. Go back to Step5 for next byte

BDRCON= 0x0E;

BRL= 0xF5;

SCON|= 0x50;

BDRCON|= 0x10;

TI= 0;

do {

SBUF= byte[i];

while (!TI);

} while(more_bytes);