Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
BASCOM AVR, help reference (2007).PDF
Скачиваний:
305
Добавлен:
12.08.2013
Размер:
17.02 Mб
Скачать

© MCS Electronics, 1995-2007

'and while bits() will set all bits specified to 1, there is also Nbits() 'the N is for NOT. Nbits(1,2) means, set all bits except 1 and 2

B = Nbits(7) 'do not set bit 7

Print B

End

ON INTERRUPT

Action

Execute subroutine when the specified interrupt occurs.

Syntax

ON interrupt label [NOSAVE]

Remarks

Interrupt INT0, INT1, INT2, INT3, INT4,INT5, TIMER0 ,TIMER1, TIMER2, ADC , EEPROM , CAPTURE1, COMPARE1A, COMPARE1B,COMPARE1. Or you can use the AVR

name convention:

 

OC2 , OVF2, ICP1, OC1A, OC1B, OVF1, OVF0, SPI, URXC,

 

UDRE, UTXC, ADCC, ERDY and ACI.

Label

The label to jump to if the interrupt occurs.

NOSAVE When you specify NOSAVE, no registers are saved and restored in the interrupt routine. So when you use this option make sure to save and restore all used registers.

When you omit NOSAVE all used registers will be saved. These are SREG , R31 to R16 and R11 to R0 with exception of R6,R8 and R9 .

R12 – R15 are not saved. When you use floating point math in the ISR(not recommended) you must save and restore R12-R15 yourself in the ISR.

My_Isr:

Push R12 ' save registers Push R13

Push R14

Push R15

Single = single + 1 ' we use FP

Pop R15 ' restore registers

Pop R14

Pop R13

Pop R12

RETURN

You must return from the interrupt routine with the RETURN statement.

The first RETURN statement that is encountered that is outside a condition will generate a

page -576-

© 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-