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

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

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

Добавлен: 12.06.2025

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

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

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

© MCS Electronics, 1995-2007

Action

Returns the minimum value of a byte or word array.

Syntax

var1 = MIN(var2) MIN(ar(1), m , idx)

Remarks

var1

Variable that will be assigned with the minimum value.

var2

The first address of the array.

The MIN statement can return the index too

Ar(1)

Starting element to get the minimum value and index of

M

Returns the minimum value of the array

Idx

Return the index of the array that contains the minimum value. Returns 0 if

there is no minimum value.

The MIN() ans MAX() functions work on BYTE and WORD arrays only.

See also

MAX

Example

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

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

: minmax.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: show the MIN and MAX functions

'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

' These functions only works on BYTE and WORD arrays at the moment !!!!!

'Dim some variables

Dim Wb As Byte , B As Byte

Dim W(10) As Word ' or use a BYTE array

page -573-


© MCS Electronics, 1995-2007

'fill the word array with values from 1 to 10

For B = 1 To 10 W(b) = B

Next

Print "Max number " ; Max(w(1))

Print "Min number " ; Min(w(1))

Dim Idx As Word , M1 As Word

Min(w(1) , M1 , Idx)

Print "Min number " ; M1 ; " index " ; Idx

Max(w(1) , M1 , Idx)

Print "Max number " ; M1 ; " index " ; Idx

End

MID

Action

The MID function returns part of a string (a sub string).

The MID statement replaces part of a string variable with another string.

Syntax

var = MID(var1 ,st [, l] ) MID(var ,st [, l] ) = var1

Remarks

var

The string that is assigned.

Var1

The source string.

st

The starting position.

l

The number of characters to get/set.

See also

LEFT , RIGHT

Example

Dim S As String * 15 , Z As String * 15

S ="ABCDEFG"

Z = Left(s , 5)

'ABCDE

Print Z

Z = Right(s , 3) : Print Z

Z = Mid(s , 2 , 3) : Print Z

End

NBITS

Action

page -574-


© MCS Electronics, 1995-2007

Set all except the specified bits to 1.

Syntax

Var = NBITS( b1 [,bn])

Remarks

Var

The BYTE/PORT variable that is assigned with the constant.

B1 , bn

A list of bit numbers that NOT must be set to 1.

While it is simple to assign a value to a byte, and there is special boolean notation &B for assigning bits, the Bits() and NBits() function makes it simple to assign a few bits.

B = &B01111101 : how many zero’s are there?

This would make it more readable: B = NBits(1, 7)

You can read from the code that bit 1 and bit 7 are NOT set to 1.

It does not save code space as the effect is the same.

The NBITS() function will set all bits to 1 except for the specified bits. It can only be used on bytes and port registers.

Valid bits are in range from 0 to 7.

See Also

BITS

Example

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

---

: bits-nbits.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demo for Bits() AND Nbits()

'micro

: Mega48

'suited for demo

: yes

'commercial addon needed

: no

'use in simulator

: possible

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

---

$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 B As Byte

'while you can use &B notation for setting bits, like B = &B1000_0111 'there is also an alternative by specifying the bits to set

B = Bits(0 , 1 , 2 , 7) 'set only bit 0,1,2 and 7

Print B

page -575-


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