ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 8351

Скачиваний: 1

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

© MCS Electronics, 1995-2007

RETI instruction. You may have only one such RETURN statement in your interrupt routine because the compiler restores the registers and generates a RETI instruction when it encounters a RETURN statement in the ISR. All other RETURN statements are converted to a RET instruction.

The possible interrupt names can be looked up in the selected microprocessor register file. 2313def.dat for example shows that for the compare interrupt the name is COMPARE1. (look at the bottom of the file)

What are interrupts good for?

An interrupt will halt your program and will jump to a specific part of your program. You can make a DO .. LOOP and poll the status of a pin for example to execute some code when the input on a pin changes.

But with an interrupt you can perform other tasks and when then pin input changes a special part of your program will be executed. When you use INPUT "Name ", v for example to get a user name via the RS-232 interface it will wait until a RETURN is received. When you have an interrupt routine and the interrupt occurs it will branch to the interrupt code and will execute the interrupt code. When it is finished it will return to the Input statement, waiting until a RETURN is entered.

Maybe a better example is writing a clock program. You could update a variable in your program that updates a second counter. But a better way is to use a TIMER interrupt and update a seconds variable in the TIMER interrupt handler.

There are multiple interrupt sources and it depends on the used chip which are available.

To allow the use of interrupts you must set the global interrupt switch with a ENABLE INTERRUPTS statement. This only allows that interrupts can be used. You must also set the individual interrupt switches on!

ENABLE TIMER0 for example allows the TIMER0 interrupt to occur.

With the DISABLE statement you turn off the switches.

When the processor must handle an interrupt it will branch to an address at the start of flash memory. These addresses can be found in the DAT files.

The compiler normally generates a RETI instruction on these addresses so that in the event that an interrupt occurs, it will return immediately.

When you use the ON ... LABEL statement, the compiler will generate code that jumps to the specified label. The SREG and other registers are saved at the LABEL location and when the RETURN is found the compiler restores the registers and generates the RETI so that the program will continue where it was at the time the interrupt occurred.

When an interrupt is services no other interrupts can occur because the processor(not the compiler) will disable all interrupts by clearing the master interrupt enable bit. When the interrupt is services the interrupt is also cleared so that it can occur again when the conditions are met that sets the interrupt.

It is not possible to give interrupts a priority. The interrupt with the lowest address has the highest interrupt!

Finally some tips :

page -577-

©MCS Electronics, 1995-2007

*when you use a timer interrupt that occurs each 10 uS for example, be sure that the interrupt code can execute in 10 uS. Otherwise you would loose time.

*it is best to set just a simple flag in the interrupt routine and to determine it's status in the main program. This allows you to use the NOSAVE option that saves stack space and program space. You only have to Save and Restore R24 and SREG in that case.

*Since you can not PUSH a hardware register, you need to load it first:

PUSH R24 ; since we are going to use R24 we better save it

IN r24, SREG ; get content of SREG into R24

PUSH R24 ; we can save a register

;here goes your asm code POP R24 ;get content of SREG

OUT SREG, R24 ; save into SREG

POP R24 ; get r24 back

See Also

On VALUE

Partial Example

Enable Interrupts

'enable the

Enable Int0

interrupt

'jump to label2 on

On Int0 Label2 Nosave

INT0

Do'endless loop

nop

Loop

End

Label2:

Dim A AsByte

If A > 1 Then

'generates a RET

Return

because it is inside a condition

EndIf

'generates a RETI

Return

because it is the first RETURN

'generates a RET

Return

because it is the second RETURN

ON VALUE

Action

Branch to one of several specified labels, depending on the value of a variable.

Syntax

ON var [GOTO] [GOSUB] label1 [, label2 ] [,CHECK]

page -578-


© MCS Electronics, 1995-2007

Remarks

Var

The numeric variable to test.

This can also be a SFR such as PORTB.

label1,

The labels to jump to depending on the value of var.

label2

CHECK

An optional check for the number of provided abels.

Note that the value is zero based. So when var is 0, the first specified labelis jumped/branched.

It is important that each possible value has an associated label.

When there are not enough labels, the stack will get corrupted. For example : ON value label1, label2

And value = 2, there is no associated label.

You can use the optional CHECK so the compiler will check the value against the number of provided labels. When there are not enough labels for the value, there will be no GOTO or GOSUB and the next line will be executed.

See Also

ON INTERRUPT

ASM

The following code will be generated for a non-MEGA micro with ON value GOTO. Ldi R26,$60 ; load address of variable

Ldi R27,$00 ; load constant in register Ld R24,X

Clr R25

Ldi R30, Low(ON_1_ * 1) ; load Z with address of the label

Ldi R31, High(ON_1_ * 1)

Add zl,r24

; add value to Z

Adc zh,r25

Ijmp

; jump to address stored in Z

ON_1_:

Rjmp lbl1

; jump table

Rjmp lbl2

Rjmp lbl3

The following code will be generated for a non-MEGA micro with ON value GOSUB.

;##### On X Gosub L1 , L2

Ldi R30,Low(ON_1_EXIT * 1)

Ldi R31,High(ON_1_EXIT * 1) Push R30 ;push return address Push R31

Ldi R30,Low(ON_1_ * 1) ;load table address Ldi R31,High(ON_1_ * 1)

Ldi R26,$60 Ld R24,X Clr R25

page -579-


© MCS Electronics, 1995-2007

Add zl,r24 ; add to address of jump table

Adc zh,r25

Ijmp ; jump !!!

ON_1_:

Rjmp L1

Rjmp L2

ON_1_EXIT:

As you can see a jump is used to call the routine. Therefore the return address si first saved on the stack.

Example

'-----------------------------------------------------------------------------

------------

: ongosub.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demo : ON .. GOSUB/GOTO

'micro

: Mega48

'suited for demo

: yes

'commercial addon needed

: no

'-----------------------------------------------------------------------------

------------

$regfile = "m48def.dat"

' specify the used

micro

' used crystal

$crystal = 4000000

frequency

' use baud rate

$baud = 19200

$hwstack = 32

' default use 32

for the hardware stack

' default use 10

$swstack = 10

for the SW stack

' default use 40

$framesize = 40

for the frame space

Dim A As Byte

'ask for input

Input "Enter value 0-2 " , A

Rem Note That The Starting Value Begins With 0

On A Gosub L0 , L1 , L2

Print "Returned"

If Portb < 2 Then

'you can also use

the portvalue

On Portb Goto G0 , G1

End If

End_prog:

End

L0:

Print "0 entered"

Return

L1:

Print "1 entered"

Return

L2:

Print "2 entered"

page -580-


© MCS Electronics, 1995-2007

Return

G0:

Print "P1 = 0"

Goto End_prog

G1:

Print "P1 = 1"

Goto End_prog

OPEN

Action

Opens a device.

Syntax

OPEN "device" for MODE As #channel

OPEN file FOR MODE as #channel

Remarks

Device

MODE

The default device is COM1 and you don't need to open a channel to use INPUT/OUTPUT on this device.

With the implementation of the software UART, the compiler must know to which pin/device you will send/receive the data.

So that is why the OPEN statement must be used. It tells the compiler about the pin you use for the serial input or output and the baud rate you want to use.

COMB.0:9600,8,N,2 will use PORT B.0 at 9600 baud with 2 stopbits.

The format for COM1 and COM2 is : COM1: or COM2:

There is no speed/baud rate parameter since the default baud rate will be used that is specified with $BAUD or $BAUD1

The format for the software UART is: COMpin:speed,8,N,stopbits[,INVERTED] Where pin is the name of the PORT-pin.

Speed must be specified and stop bits can be 1 or 2. 7 bit data or 8 bit data may be used.

For parity N, O or E can be used.

An optional parameter ,INVERTED can be specified to use inverted RS-232. Open "COMD.1:9600,8,N,1,INVERTED" For Output As #1 , will use pin PORTD.1 for output with 9600 baud, 1 stop bit and with inverted RS-232.

For the AVR-DOS filesystem, Device can also be a string or filename constant like

"readme.txt" or sFileName

You can use BINARY or RANDOM for COM1 and COM2, but for the software UART pins, you must specify INPUT or OUTPUT.

page -581-