Other Features

Interrupt

1. Introduction

This cheat sheet explores interrupt processing in NC30. It describes themethod to write an interrupt subroutine for software and hardware interrupts.

2.Software interrupts

Software interrupts are generated by instructions that generate an interrupt request when executed. Software interrupts are non-maskable interrupts.The various types of software interrupts are described below.

2.1Undefined instruction interrupt

The UND instruction causes this interrupt. The vector address for this interrupt is located ina fixed vector tablefromFFFDC to FFFFDF. As shown in the example below, the user must define the interrupt sub routine for UND in the startup file sect30.inc, use .glb directive, in order to define the interrupt subroutine “UND_Instruction” and write the subroutine function in C file.

Sect30.inc file:
;------; fixed vector section
;------
.sectionfvector,ROMDATA
.org0fffdcH
UDI:
.glb UND_Instruction
.lwordUND_Instruction

Example 1Defining the Interrupt Subroutine for UND Interrupt in Startup File sect30.inc

Note:

  • If the interrupt routine is not defined in startup file, then a dummy_int routine is executed.

2.2Overflow interrupt

This interrupt is generatedby the overflow flag.If the overflow bit is set to 0 and the INTOinstruction is executed, then this interrupt is generated. The vector address for this interrupt is located in the fixed vector table betweenaddresses FFFE0 to FFFE3. The overflow flag is changed by the following instructions: ABS, ADC, ADCF, ADD, CMP, DIV, DIVU, DIVX, NEG, RMPA, SBB, SHA and SUB.

2.3 Break interrupt

This interrupt is generatedby BRK instruction. The vector address for this interrupt is located in the fixed vector table between addressesFFFE4 to FFFE7.

2.4Interrupt generatedbyINT instruction

This interrupt is generatedbyINT instruction followed by an interrupt number.64 software interrupts (0 to 63) are available. Software interrupts 0 to 31 are assigned to peripheral I/O interrupts.

The software interrupt can be defined using #pragma INTCALL preprocessor directive. Below is the syntax for defining software interrupt.

Syntax: #pragma INTCALL INT-No. C-function-name() – call the function usingINT instruction.

As shown in the below example, the Func() is defined as a software interrupt with an interrupt number of 20. Therefore,the compiler uses the INT 20 instruction to call the interrupt routine.

char Func(char);
#pragma INTCALL 20 Func();
void main(void){
Func(1);
F0010 D812 _main MOV.B #1H,R1L
F0012 EBD4 INT #20
}
F0014 F3 RTS
F0015 04 NOP
char Func(char cFlag){
F0016 7CF201 _Func ENTER #01H
F0019 722BFF MOV.B R1L,-1H[FB]
return (cFlag);
F001C 0AFF MOV.B -1H[FB],R0L
F001E 7BF5 STC FB,A1
F0020 7AD5 LDC A1,SP
F0022 ED80 POPM FB
F0024 FB REIT
}

Example 2 Defining Software Interrupts

Note:

  • The INT-no in #pragma INTCALL INT-no function_name cannot be greater than63. If the software interrupt number in the above example is greater than 63, then the compiler will generate the flowing error message when compiling:[Error(ccom)] Invalid #pragma INTCALL interrupt number.

3.Hardware interrupts

The following are the two categoriesof hardware interrupts supported byM16C.

3.1Special interrupts

Special interrupts are non-maskable interrupts. Below is the list of the special interrupts:

  • Reset: A reset interrupt occurs when reset pin on the microcontroller is pulled low.
  • NMI interrupt: This interrupt occurs when NMI pin is pulled low.
  • DBC: This interrupt is only used by the debugger.
  • Watchdog timer interrupt: This interrupt occurs when the watchdog timer overflows. This interrupt resets the system.
  • Single step interrupt:This interrupt is used exclusively for debugger purposes. The user does not normally need to use this interrupt.A single-step interrupt occurs when the debug flag (D flag) is set to 1.In this case, an interrupt is generatedeach time an instruction is executed.
  • Address match interrupt: This interrupt occurs when the program's execution address matches the content of the address matchregister while the address match interrupt enable bit is set to 1.This interrupt does not occur if any address other than the start address of an instruction is set in theaddress match register.

3.2 Peripheral Interrupts

These interrupts are generated by the peripheral functions built into the microcomputer (MCU) system. Thetypes of built-in peripheral functions vary with each M16C model, asdo the types of interrupt causes. Theinterrupt vector table uses the same software interrupt numbers (0to 31) that are used by the INT instruction.Peripheral I/O interrupts are maskable interrupts.

The following is the process for writing and defining aperipheral interrupt routine:

Step 1: Writing an interrupt routine

The #pragma INTERRUPT preprocessor directive is used to write the interrupt subroutine in the application code. The following is the syntax of #pragma INTERRUPT.

Syntax:

  • #pragma INTERRUPT interrupt_handler_name(vect=Vector_no) – define the interrupt handler with a vector number.
  • #pragma INTERRUPT /B interrupt_handler_name (vect=Vect_no) - define the interrupt handler with a vector number, and do not push the register onto the stack.Use a different set of registers in the ISR. This is known as bank-switching and allows for lower interrupt latency, but does not allow interrupt-nesting.
  • #pragma INTERRUPT /E interrupt_handler_name (vect=Vect_no) - define the interrupt handler with a vector number, and enable multiple interrupts within the ISR.

As shown in the example below, the interrupt routine for a timer is defined using #pragma INTERRUPT.As the interrupt routine timerA() is defined with /B option,the compiler uses a different set of registers to process the interrupt routine.

int iCounter;
#pragma INTERRUPT /B timerA()
void timerA(void){
F0010 EB44 _timerA FSET B
iCounter += 1;
F0012 C91F1004 ADD.W #1H,0410H
}
F0016 FB REIT
F0017 04 NOP
void main(){
iCounter = 0;
F0018 D90F1004 _main
MOV.W #0H,0410H
}
F001C F3 RTS

Example 3Declaring an ISR Using #pragma INTERRUPT /B

Note:

  • If the interrupt subroutine function is called from the program, then the compilerwill outputtheerror message [Error(ccom)] interrupt function, calledwhen compiling.

Step 2: Set the interrupt vector table

The user must set the interrupt subroutine in the variable vector table.The variable vector table is locatedin the startup file sect30.inc. Given below is the process forsetting the vector table.

  • Open the startup file sect30.inc.At the end of the file,the interrupt vector is declared as shown in Example 4.
  • Define the interrupt function name in the variable vector table.Define the function at the appropriate vector location, i.e. define the timer routine for timer B0 at comments ;timer B0 (vector 26).
  • As shown in Example 4, define the interrupt function name in place of dummy_int preceded by an underscore(_) using .glb and .lword directives.

As shown in the example below, the interrupt function timerA() is defined for peripheral timer A3 at vector address 24 using .glb and .lword directives.

;------
; variable vector section
;------
.section vector ;variable vector table
.org VECTOR_AD
.lword dummy_int ;BRK (vector 0)
.org (VECTOR_ADR+16)
.glb _timerA ;timer A3 (vector 24)
.lword _timerA
.lword dummy_int ;timer A4 (vector 25)
.lword dummy_int ;timer B0 (vector 26)
.lword dummy_int ;timer B1 (vector 27)
.lword dummy_int ;timer B2 (vector 28)
.lword _dummy_int ;int0
.lword dummy_int ;int1 (vector 30)
.lword dummy_int ;int2 (vector 31)
.
.
.
.lword dummy_int ;vector 46 (software interrupt vector)
.lword dummy_int ;vector 47 (software interrupt vector)

Example 4Adding the Vector Address in the Interrupt Vector Table in Startup File sect30.inc

4. References

For more details, please refer to the Compiler User Manual (nc30ue.pdf).

Revision History

Ver. No. / Date / Section No. / Changes / Reason for Changes
1.01 / 2007/08/06 / 1,2,3,4 / Format and grammar / RTA and RSO comments
2,3 / Change in variable name and function name / KPIT review comments
1.02 / 2007/08/30 / 1,2,3,4 / Format and grammar / KPIT review comments
1.03 / 2008/01/10 / 2 / Format and grammar / RTA review
3.2 / Additional line for #pragma INTERRUPT/B
1 / Changed “HEW” to “NC30”

1 V1.03