• Keine Ergebnisse gefunden

Here, we will explain how to control 16-bit timer interrupts and PWM output, using the source code for melody33 middleware as an example. Note that the E0C33A104's 16-bit timer significantly differs in functionality from that of the E0C33208.

Interrupt settings

The following describes the compare B interrupt of 16-bit timer 4.

Vector section

#define INT30 mdyInt // mdy interrupt routine (1)

.code

.word RESET .word RESERVED .word RESERVED .word RESERVED .word ZERODIV .word RESERVED .word ADDRERR .word NMI .word RESERVED .word RESERVED .word RESERVED .word RESERVED .word SOFTINT0 .word SOFTINT1 .word SOFTINT2 .word SOFTINT3 .word INT0 .word INT1 .word INT2

| .word INT28 .word INT29

.word INT30 (1)

.word INT31 .word INT32 .word INT33 (1) Setting the interrupt vector

Register the interrupt routine mdyInt as the vector for INT30 (compare B interrupt of 16-bit timer 4).

Interrupt disable and PSR save/restore routine

.lcomm MDY_PSR 0x4

.global mdyIntOff

mdyIntOff: (1)

xld.w %r4,MDY_PSR

ld.w %r5,%psr // save %psr and IE disable ld.w [%r4],%r5

ld.w %r4,0 ld.w %psr,%r4 ret

.global mdyIntOn

mdyIntOn: (2)

xld.w %r4,MDY_PSR ld.w %r5,[%r4]

ld.w %psr,%r5 // restore %psr ret

(1) Disabling interrupts

Save the PSR contents and set the IE bit to 0 to disable interrupts.

(2) Enabling interrupts

Restore the contents of PSR saved in (1).

These settings are called from C.

16-bit timer setup section

//*******************************************************************

// void mdyTmOpen(unsigned short freq)

// start timer 4, underflow interrupt with freq count // prescaler is 1/1024

//*******************************************************************

void mdyTmOpen(unsigned short freq) {

unsigned char ucTmp;

// interrupt disable

mdyIntOff(); (1)

// set TM4 prescaler to 1/1024, 0b00001110

*(volatile unsigned char *)(0x4014b) = 0xe; (2)

// set TM4 reload and compare data

*(volatile unsigned short *)(0x481a2) = freq; //set compare b (3)

*(volatile unsigned short *)(0x481a4) = 0x0; //dummy data for up counter // set TM4 control register

// fine mode off,compare buf off,reverse off,internal clock,clock out off,preset,stop // 0x401a6 0010,

*(volatile unsigned char *)(0x481a6) = 0x0;

// set TM4 match compare b come to cpu interrupt

*(volatile unsigned char *)(0x40291) &= 0xBf; //set timer 4 enable (4) // set TM4,interrupt priority level 3

ucTmp = *(volatile unsigned char *)(0x40268);

ucTmp = ucTmp & 0xf0;

ucTmp = ucTmp | 0x3;

*(volatile unsigned char *)(0x40268) = ucTmp;

// clear TM4 interrupt factor flags (write 1, and reset)

*(volatile unsigned char *)(0x40284) &= 0x0C;

// set TM4 underflow interrupt enable

*(volatile unsigned char *)(0x40274) |= 0x04; //set timer 4 enable // start TM4 counter

*(volatile unsigned char *)(0x481a6) |= 0x01; (5)

// interrupt enable

mdyIntOn(); (6)

}

(1) Disabling interrupts

Disable interrupts as a precautionary measure.

// interrupt disable mdyIntOff();

(2) Setting the prescaler

A divide-by-1024 clock from the prescaler is fed into timer 4 as its input clock.

// set TM4 prescaler to 1/1024, 0b00001110

*(volatile unsigned char *)(0x4014b) = 0xe;

(3) Timer 4 cycle (compare B), compare A, and other settings

The compare B interrupt cycle of timer 4 is set to (freq + 1) × 1024 clock periods by the following settings:

// set TM4 reload and compare data

*(volatile unsigned short *)(0x481a2) = freq; //set compare b

*(volatile unsigned short *)(0x481a4) = 0x0; //dummy data for up counter Set other parameters for timer 4.

// set TM4 control register

// fine mode off,compare buf off,reverse off,internal clock,clock out off, ...

// 0x401a6 0010,

*(volatile unsigned char *)(0x481a6) = 0x0;

(4) Setting the interrupt controller

Set the interrupt controller so that the compare B interrupt of timer 4 is forwarded to the CPU as an immediate interrupt, not as an IDMA start request.

// set TM4 match compare b come to cpu interrupt

*(volatile unsigned char *)(0x40291) &= 0xBf; //set timer 4 enable Set the interrupt priority to 3.

// set TM4,interrupt priority level 3

ucTmp = *(volatile unsigned char *)(0x40268);

ucTmp = ucTmp & 0xf0;

ucTmp = ucTmp | 0x3;

*(volatile unsigned char *)(0x40268) = ucTmp;

As a precaution, clear the interrupt factor flag.

// clear TM4 interrupt factor flags (write 1, and reset)

*(volatile unsigned char *)(0x40284) &= 0x0C;

Enable the compare B interrupt.

// set TM4 underflow interrupt enable

*(volatile unsigned char *)(0x40274) |= 0x04; //set timer 4 enable (5) Timer start

Let timer 4 begin counting.

// start TM4 counter

*(volatile unsigned char *)(0x481a6) |= 0x01;

(6) Enabling interrupts Reenable interrupts.

// interrupt enable mdyIntOn();

Note that a separate interrupt routine (mdyInt) needs to be written. Make sure that the interrupt factor flag is always cleared in the interrupt routine.

Example for clearing:

// clear TM4 interrupt factor flags (write H and reset)

*(volatile unsigned char *)(0x40284) &= 0x0C;

This prevents the re-occurrence of the same interrupt when interrupts are enabled.

PWM settings

The following section describes how to process PWM. The source code for melody33 middleware is used as an example.

PWM initial settings

//*******************************************************************

// void mdyTm0Set (unsigned short count, unsigned short compare) //*******************************************************************

static void mdyTm0Set (unsigned short count, unsigned short compare, int reverse) {

// interrupt disable

mdyIntOff(); (1)

// set P22 port to TM0

*(volatile char *)(0x402d8) |= 0x04; (2)

// set TM0 prescaler to 1/16, 0b0001011

*(volatile unsigned char *)(0x40147) = 0x0b; (3)

// set TM0 reload and compare data

*(volatile unsigned short *)(0x48182) = count; //compare B (4)

*(volatile unsigned short *)(0x48180) = compare; //compare A // set TM0 control register

// fine mode off, compare buf, reverse, internal clock, clock out on, preset, stop // internal clock, clock out on, preset, stop

// 0x4018e 0b00010100 or 0b00110100 if (reverse==1){

*(volatile unsigned char *)(0x48186) = 0x34;

} else{

*(volatile unsigned char *)(0x48186) = 0x24;

}

// reset TM0 counter

*(volatile unsigned char *)(0x48186) |= 0x02; (5)

// interrupt enable

mdyIntOn(); (6)

}

(1) Disabling interrupts

Disable interrupts as a precautionary measure.

// interrupt disable mdyIntOff();

(2) Selecting port functions

Because the ports used for PWM (16-bit timer) output are set for general-purpose input/output ports by default, change their function to PWM output.

// set P22 port to TM0

*(volatile char *)(0x402d8) |= 0x04;

(3) Setting the prescaler

A divide-by-16 clock from the prescaler is fed into timer 0 as its input clock.

// set TM0 prescaler to 1/16, 0b0001011

*(volatile unsigned char *)(0x40147) = 0x0b;

(4) Setting timer 0

Start by setting up compare A and compare B registers. Compare B + 1 counts comprise one cycle.

In normal mode, output starts from 0; in inverse mode, output starts from 1. Compare A + 1 counts select output between 0 and 1.

For example, when in normal mode compare B = 5 and compare A = 0, the output is 0 in the first clock period and 1 in the remaining other four clock periods. This is repeated.

// set TM0 reload and compare data

*(volatile unsigned short *)(0x48182) = count; //compare B

*(volatile unsigned short *)(0x48180) = compare; //compare A Set other parameters for timer 0.

// set TM0 control register

// fine mode off, compare buf, reverse, internal clock, clock out on, preset, stop // internal clock, clock out on, preset, stop

// 0x4018e 0b00010100 or 0b00110100 if (reverse==1){

*(volatile unsigned char *)(0x48186) = 0x34;

} else{

*(volatile unsigned char *)(0x48186) = 0x24;

}

(5) Reset the counter for timer 0 Reset the counter for timer 0 to 0.

// reset TM0 counter

*(volatile unsigned char *)(0x48186) |= 0x02;

(6) Enabling interrupts

Finish by reenabling interrupts.

// interrupt enable mdyIntOn();

PWM start section

//*******************************************************************

// void mdyTm0Start ()

//*******************************************************************

static void mdyTm0Start () {

// start TM0 counter

*(volatile unsigned char *)(0x48186) |= 0x03;

}

This function starts PWM.

PWM change section

//*******************************************************************

// void mdyTm0Change (unsigned short count, unsigned short compare) //*******************************************************************

static void mdyTm0Change (unsigned short count, unsigned short compare) {

// set TM0 reload and compare data

*(volatile unsigned short *)(0x48182) = count; // compare B

*(volatile unsigned short *)(0x48180) = compare; // compare A }

This function changes the cycles and duty of PWM waveform. In setting (4) of the mdyTm0set() function, the compare buffer (0x48186•D5 = 1) is enabled to allow compare A/B data to be written to the buffer asynchronously with the counter. The data once stored in the buffer is set in the compare A/B registers when the counter returns a 0 upon matching compare B. If the entire compare buffer is not being used, a single occurrence of compare A matching may be undetected unless synchronized since compare A/B data take effect when written.