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

© MCS Electronics, 1995-2007

for the frame space

 

Dim V As Byte , B1 As Byte

 

Dim C As Integer , D As Byte

 

Dim S As String * 15

 

Input "Use this to ask a question " , V

'leave out for no

Input B1

question

 

Input "Enter integer " , C

 

Print C

 

Inputhex "Enter hex number (4 bytes) " , C

 

Print C

 

Inputhex "Enter hex byte (2 bytes) " , D

 

Print D

 

Input "More variables " , C , D

 

Print C ; " " ; D

 

Input C Noecho

'supress echo

Input "Enter your name " , S

 

Print "Hello " ; S

 

Input S Noecho

'without echo

Print S

 

End

 

ELSE

Action

Executed if the IF-THEN expression is false.

Syntax

ELSE

Remarks

You don't have to use the ELSE statement in an IF THEN .. END IF structure. You can use the ELSEIF statement to test for another condition.

IF a = 1 THEN

...

ELSEIF a = 2 THEN

..

ELSEIF b1 > a THEN

...

ELSE

...

END IF

See also

page -476-

© MCS Electronics, 1995-2007

IF , END IF , SELECT-CASE

Example

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

 

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

: if_then.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demo: IF, THEN, ELSE

'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 , B1 As Byte

 

Input "Number " , A

'ask for number

If A = 1 Then

'test number

Print "You got it!"

 

End If

 

If A = 0 Then

'test again

Print "Wrong"

'thats wrong

Else

'print this if a

is not 0

 

Print "Almost?"

 

End If

 

Rem You Can Nest If Then Statements Like This

 

B1 = 0

 

If A = 1 Then

 

If B1 = 0 Then

 

Print "B1=0"

 

End If

 

Else

 

Print "A is not 0"

 

End If

 

Input "Number " , A

'

If A = 1 Then

Print "Ok"

'use elseif for

Elseif A = 2 Then

more tests

 

Print "2" : A = 3

 

Elseif A = 3 Then

 

Print "3"

 

End If

 

If A.1 = 1 Then Print "Bit 1 set"

'test for a bit

End

 

page -477-

© MCS Electronics, 1995-2007

ENABLE

Action

Enable specified interrupt.

Syntax

ENABLE interrupt

Remarks

Interrupt

Description

INT0

External Interrupt 0

INT1

External Interrupt 1

OVF0,TIMER0,

TIMER0 overflow interrupt

COUNTER0

 

OVF1,TIMER1,

TIMER1 overflow interrupt

COUNTER1

 

CAPTURE1, ICP1

INPUT CAPTURE TIMER1 interrupt

COMPARE1A,OC1A or

TIMER1 OUTPUT COMPARE A interrupt

COMPARE1, OC1

In case of only one compare interrupt

COMPARE1B,OC1B

TIMER1 OUTPUT COMPARE B interrupt

SPI

SPI interrupt

URXC

Serial RX complete interrupt

UDRE

Serial data register empty interrupt

UTXC

Serial TX complete interrupt

SERIAL

Disables URXC, UDRE and UTXC

ACI

Analog comparator interrupt

ADC

A/D converter interrupt

 

 

By default all interrupts are disabled.

To enable the enabling and disabling of interrupts use ENABLE INTERRUPTS.

Other chips might have additional interrupt sources such as INT2, INT3 etc.

See also

DISABLE

Partial Example

Enable Interrupts

'allow interrupts to be set

Enable Timer1

'enables the TIMER1 interrupt

ENCODER

page -478-

© MCS Electronics, 1995-2007

Action

Reads pulses from a rotary encoder.

Syntax

Var = ENCODER( pin1, pin2, LeftLabel, RightLabel , wait)

Remarks

Var

The target variable that is assigned with the result

Pin1 and pin2

These are the names of the PIN registers to which the output of the

 

encoder is connected. Both pins must be on the same PIN register. So

 

Pinb.0 and Pinb.7 is valid while PinB.0 and PinA.0 is not.

LeftLabel

The name of the label that will be called/executed when a transition

 

to the left is encoded.

RightLabel

The name of the label that will be called/executed when a transition

 

to the right is encountered.

wait

A value of 0 will only check for a rotation/pulse. While a value of 1

 

will wait until a user actual turns the encoder. A value of 1 will thus

 

halt your program.

 

 

There are some conditions you need to fulfill :

The label that is called by the encoder must be terminated by a RETURN statement.

The pin must work in the input mode. By default all pins work in input mode.

The pull up resistors must be activated by writing a logic 1 to the port registers as the examples shows.

Rotary encoders come in many flavors. Some encoders also have a build in switch.

A sample of an encoder

page -479-

© MCS Electronics, 1995-2007

Since the microprocessor has internal pull up resistors, you do not need externalpull up resistors for most encoders.

Example

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

 

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

: encoder.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demonstration of encoder function

'micro

: Mega128

'suited for demo

: yes

'commercial addon needed

: no

'An encoder has 2 outputs

and a ground

'We connect the outputs to pinb.0 and pinb.1

'You may choose different pins as long as they are at the same PORT 'The pins must be configured to work as input pins

'This function works for all PIN registers '-----------------------------------------------------------------------------

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

$regfile = "m128def.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

 

Print "Encoder test"

Dim B As Byte

'we have dimmed a byte because we need to maintain the state of the encoder

Portb = &B11

 

 

 

' activate pull up

registers

 

 

 

 

Do

 

 

 

 

B = Encoder(pinb.0 , Pinb.1 , Links , Rechts , 1)

1 means wait for

'

 

 

^---

change which blocks programflow

^

labels which are

'

 

^--------

called

^

^

 

port PINs

'

 

Print B

 

 

 

 

Waitms 10

 

 

 

 

Loop

 

 

 

 

End

 

 

 

 

'so while you can choose PINB0 and PINB7,they must be both member of PINB 'this works on all PIN registers

Links:

Print "left rotation"

Return

Rechts:

Print "right rotation"

Return

page -480-

© MCS Electronics, 1995-2007

End

END

Action

Terminate program execution.

Syntax

END

Remarks

STOP can also be used to terminate a program.

When an END statement is encountered, all interrupts are disabled and a never-ending loop is generated.

When a STOP is encountered the interrupts will not be disabled. Only a never ending loop will be created.

In an embedded application you probably do not want to end the application. But there are cases where you do want to end the application. For example when you controlsome motors, and you determine a failure, you do not want to use a Watchdog reset because then the failure will occur again. In that case you want to display an error, and wait for service personal to fix the failure.

It is important to notice that without the END statement, your programcan behave strange in certain cases. For example :

Print "Hello"

Note that there is no END statement. So what will happen? The program will print "Hello". But as the compiler places the library code behind the program code, the micro will execute the library code ! But without being called. As most library code are assembler sub routines that end with a RET, your program will most likely crash, or reset and repeat for ever.

See also

STOP

Example

Print "Hello"

'print this

End

'end program execution and disable all interrupts

EOF

Action

Returns the End of File Status.

Syntax

page -481-