Lecture Slides on Output Compare | ECE 6960, Study notes of Electrical and Electronics Engineering

Material Type: Notes; Class: Fab & Char Tech for Nanostru; Subject: Electrical & Computer Engg; University: University of Utah; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-d63
koofers-user-d63 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Slide 1
'
&
$
%
ECE/CS 3720: Embedded System Design
(ECE 6960/2 and CS 6968)
Chris J. Myers
Lecture 14: Output Compare
Slide 2
'
&
$
%
Basic Principles of Output Capture
Output compare can create square waves, generate pulses,
implement time delays, and execute periodic interrupts.
Can also use with input capture to measure frequency.
Each output capture module has:
An external output pin, OCn
A flag bit
A force control bit FOCn
Two control bits, OMn, OLn
An interrupt mask bit (arm)
A 16-bit output compare register
1
Slide 3
'
&
$
%
Basic Components of Output Compare
Slide 4
'
&
$
%
Basic Principles of Output Compare (cont)
Output compare pin can control an external device.
Output compare event occurs and sets flag when either:
1. The 16-bit TCNT matches the 16-bit OC register
2. The software writes a 1 to the FOC bit.
OMn, OLn bits specify effect of event on the output pin.
Two or three actions result from a compare event:
1. The OCn output bit changes
2. The output compare flag is set.
3. An interrupt is requested if the mask is 1.
2
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Lecture Slides on Output Compare | ECE 6960 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

& '

% $

ECE/CS 3720: Embedded System Design

(ECE 6960/2 and CS 6968)

Chris J. Myers

Lecture 14: Output Compare

& '

% $

Basic Principles of Output Capture

  • (^) Output compare

(^) can create square waves, generate pulses,

  • •^ Each output capture module has:^ •^ Can also use with input capture to measure frequency.implement time delays, and execute periodic interrupts. An external output pin, OCn

A flag bit

A force control bit FOCn

Two control bits, OMn, OLn

An interrupt mask bit (arm)

A 16-bit output compare register

1

Slide 3

& '

Basic Components of Output Compare

Slide 4

& '

Basic Principles of Output Compare (cont)

  • (^) Output compare event occurs and sets flag when either: • (^) Output compare pin can control an external device.
  1. The software writes a 1 to the FOC bit.1. The 16-bit TCNT matches the 16-bit OC register
  • (^) Two or three actions result from a compare event: • (^) OMn, OLn bits specify effect of event on the output pin.
  1. An interrupt is requested if the mask is 1.2. The output compare flag is set.1. The OCn output bit changes

2

& '

% $

Applications of Output Compare

  • (^) Can create a

(^) fixed (^) time delay.

  1. Calculate TCNT+1. Read the current 16-bit TCNT

fixed

  1. Set 16-bit output compare register to TCNT+

fixed

  1. Wait for the output compare flag to be set 4. Clear the output compare flag
  • (^) Maximum delay is 65,536 cycles. • (^) Delay of steps 1 to 4 sets the minimum delay.

& '

% $

Output Compare Interface on 68HC

3

Slide 7

& '

Control Bits and Flags

OMn

OLn Effect of when TOCn=TCNT

(^0) (^0)

Does not affect OCn

(^0) (^1)

Toggle OCn

(^1) (^0)

Clear OCn=

(^1) (^1)

Set OCn=

Slide 8

& '

Setting the TFLG1 Register

ldaa #$20^ •^ The following works:^ •^ Care again must be taken when clearing TFLG1.

TFG1 = 0x20;

ldx^ •^ The following does not:staa $ (^) #$

TFLG1 |= 0x20;

bset $23,X,$

4

& '

% $

Periodic Interrupt Using Output Capture

#define OC5 #define Rate 2000

0x

unsigned int Time;

(^) // Inc every 1ms

void TOC5handler(void){#pragma interrupt_handler TOC5handler() TFLG1=OC5;

// Ack interrupt

Time++; }TOC5=TOC5+Rate; // Executed every 1 ms

asm(" sei");void ritual(void) {

// make atomic

TMSK1|=OC5;

(^) // Arm output compare 5

TFLG1=OC5;Time = 0;

// Initially clear OC5F

TOC5=TCNT+Rate; // First one in 1 ms

asm(" cli"); } & '

% $

Square-Wave Generation

Component

6811

Process the interrupt (cycles,

μs)

14= μs

Execute the entire handler (cycles,

μs) 34= μs

Minimum period (

μs)

(^24) μs

Interrupt every

Time to process

Freq.

Period (cycles)

(cycles)

Overhead (%)

10 Hz

100 ms 50,

(^48)

100 Hz

10 ms 5,

(^48)

1

1 kHz

1 ms (^500)

(^48)

10

5 kHz

(^200) μs (^200)

(^48)

24

1/P

P (μ s) P

(^48)

4800/P

7

Slide 15

& '

Initialization for Square-Wave Generation

Period rmb

(^2)

;units sec

Init

sei

;make atomic

oraa #$20ldaa TMSK1 ;Old value

(^) ;TMSK1 OC3I=

anda #$CFldaa TCTL1staa TMSK1 ;Arm OC3F

(^) ;OM3=

oraa #$

(^) ;OL3=

ldaa #$20staa TCTL

(^) ;clear OC3F

lddstaa TFLG (^) TCNT (^) ;current time

stdaddd #2000 ;first in 1 ms (^) TOC

cli

;enable

rts

Slide 16

& '

ISR for Square-Wave Generation

OC3HAN ldaa #$

;clear OC3F [2]

staa TFLG1 ;Ack

[4]

ldd (^) TOC

[5]

addd Period ;next

[6]

std (^) TOC

[5]

rti

[12]

org (^) $FFE

fdb (^) OC3HAN

8

& '

% $

Square-Wave Generation in C

unsigned int Period;

// Period in usec

void TOC3handler(void){#pragma interrupt_handler TOC3handler() TOC3=TOC3+Period;

(^) // calculate Next

TFLG1=0x20;}

// ack,

(^) OC3F=

void ritual(void){ asm(" sei");

// make atomic

TFLG1 = 0x20;

// clear OC3F

TMSK1|= 0x20;

// arm OC

TOC3TCTL1 = (TCTL1&0xCF)|0x10;

= TCNT+50; // first right away

asm(" cli"); }

& '

% $

Pulse-Width Modulation

Component

Process the interrupt (cycles)

Execute the handler (cycles)

Total time T (cycles)

9

Slide 19

& ' Init for Pulse-Width Modulated Square-Wave

High rmb

(^) 2 ;number of cycles high

Low (^) rmb (^) 2 ;number of cycles low

RITUAL sei

;make atomic

oraa #$20ldaa TMSK1 ;Old value

(^) ;TMSK1 OC3I=

oraa #$30ldaa TCTL1staa TMSK1 ;Arm OC3F

(^) ;OM3=1, OL3=

ldaa #$20staa TCTL

(^) ;clear OC3F

lddstaa TFLG (^) TCNT (^) ;current time

addd #

;first in 25s

std (^) TOC

cli

;enable

rts

Slide 20

& ' ISR for Pulse-Width Modulated Square-Wave

OC3HAN ldaa #$

;clear OC3F [2]

staa TFLG1 ;Ack

[4]

bita #$10ldaa TCTL2 ;rise/fall? [4]

[2]

beq (^) zero

[3]

one

ldd (^) TOC

[5]

addd High ;OC3 is 1

[6]

std (^) TOC

[5]

ldaa TCTL

[4]

anda #$BF

[2]$

staa TCLT

[4]

bra (^) done

[3]

10

& '

% $

Delayed Pulse Generation in C

void Pulse(unsigned int Delay, unsigned int Width){

asm(" sei");

(^) // make atomic

OC1M=0x20;TOC3=TOC1+Width;TOC1=TCNT+Delay;

// connect OC1 to PA5/OC

OC1D=0x20;

// PA5=1 when TOC1=TCNT

TCTL1=(TCTL1&0xCF)|0x20;

// PA5=0 when TOC3=TCNT TFLG1 = 0x20;

// Clear OC3F

TMSK1|= 0x20;

// Arm OC3F

asm(" cli");}

& '

% $

Delayed Pulse Generation in C (cont)

void TOC3handler(void){ #pragma interrupt_handler TOC3handler() OC1M=0;

// disconnect OC1 from PA

TCTL1&=0xCF;OC1D=0;

(^) // disable OC

TMSK1&=0xDF;} // disarm OC3F

13

Slide 27

& '

Frequency Measurement

  • (^) Input Capture handler incrementscapture to create a fixed time interval. • (^) Can use input capture to count pulses, and outputpulses for a fixed amount of time. • (^) Direct measurement of frequency involves counting input

(^) Counter

.

  • (^) Output compare handler calculates frequency:

f (^) = fixed timeCounter

  • (^) The frequency resolution is:

f (^) =

fixed time

Slide 28

& '

Frequency Measurement

14

& '

% $

Frequency Measurement in C

#define IC1F 0x

// connected here

#define Rate 20000

(^) 10 ms

asm(" sei");void ritual(void) {#define OC5F 0x

// make atomic

TOC5=TCNT+Rate;TMSK1|=OC5F+IC1F; // Arm OC5 and IC

// First in 10 ms

TCTL2 = (TCTL2&0xCF)|0x10;

/* IC1F set on rising edges */ Count = 0;

// Set up for first

Done=0;

/* Set on the subsequent measurements */ TFLG1=OC5F+IC1F;

// clear OC5F, IC1F

asm(" cli"); } & '

% $

Frequency Measurement in C (cont)

void TIC1handler(void){ #pragma interrupt_handler TIC1handler() Count++;

// number of rising edges

TFLG1=IC1F;}

(^) // ack, clear IC1F

void TOC5handler(void){#pragma interrupt_handler TOC5handler() TFLG1= OC5F;

// Acknowledge

Freq = Count;TOC5 = TOC5+Rate; // every 10 ms

// 100 Hz units

Count = 0; }Done = 0xff;

// Setup for next 15

Slide 31

& '

Conversion Between Frequency and Period

  • (^) Could measure frequency from period measurement:

f (^) = p 1

  • (^) If range of period measurement is 36

μs to 32ms with

resolution of 500ns, frequency range is 31 to 27,778Hz.

f (^) = p (^) · 1 (^500) ns 1 (^) = 2000000 p

  • (^) Resolution relationship is not as obvious: ∆f (^) =

1

(1/f (^) ) (^) − (^) ∆ p (^) − (^) f =

1

(1/f (^) ) (^) − (^500) ns (^) − (^) f

Slide 32

& '

Relationship Between Frequency and Period

Frequency (Hz)

Period (

μs) ∆f (Hz)

31,

(^32)

500

20,

(^50)

200

10,

100

50

5,

200

13

2,

500

2

1,

1,

500

2,

200

5,

100

10,

(^50)

20,

32,

16

& '

% $

Basic Time Involved in Frequency Measurement

& '

% $

Frequency Measurement in C

#define IC1F 0x

// connected here

#define Rate 50000

(^) 25 ms

asm(" sei");void ritual(void) {#define OC5F 0x

// make atomic

TOC5=TCNT+Rate;TMSK1|=OC5F+IC1F; // Arm OC5 and IC

(^) // First in 25 ms

TCTL2 = (TCTL2&0xCF)|0x10;

/* IC1F set on rising edges */ Count = 0;

// Set up for first

Done=0;

(^) // Set on subsequent meas

TFLG1 = OC5F+IC1F; // Clear OC5F IC1FFourHundred=0;

asm(" cli"); }

19

Slide 39

& '

% $

Frequency Measurement in C (cont)

void TIC1handler(void){ #pragma interrupt_handler TIC1handler() Count++;

// number of rising edges

TFLG1=IC1F;}

(^) // ack, clear IC1F

void TOC5handler(void){#pragma interrupt_handler TOC5handler() TFLG1= OC5F;

// Acknowledge

if (++FourHundred==400){TOC5 = TOC5+Rate; // every 25 ms Freq = Count;

0.1 Hz units

Count = 0; }}Done = 0xff;FourHundred=0;

// Setup for next 20