CONTR-&-BASIC
     

AVR BASIC interpreter
BASINT

CONTENTS

1. INTRODUCTION
2. AVR BASINT, COMMANDS & FUNCTIONS
2.1 INPUT/OUTPUT CONSOLE
2.2 PROGRAM CONTROL
2.3 DATA
2.4 INTERRUPTS AND OPCODE SUPPORT
2.5 DEFINE AND USE CONTROLLER’S PINS
2.6 INTERNAL RESOURCE ACCESS
2.6.1 16-bit registers access
2.7 MATH ROUTINES
2.8 SOME BASIC EXTENTIONS
2.9 TWO WIRE INTERFACE
2.10 DIAGNOSTIC
3. PRE-PROCESSOR
4. PROGRAM DOWNLOADING

1. INTRODUCTION

BASINT is a Basic interpreter for AVR microcontrollers ATMega16/32/64. It is a firmware to downloadi into the avr micro program flash memory. AVR micro plus the firmware is a logical controller, which allows user to program it in Basic language. Users do not need any special hardware and software tools. For program writing they can use any text editor, NotePad for example. HyperTerminal is enough for program downloading. Both of these programs are standard MS Windows applications. The interpreter BASINT converts the user Basic program to the internal format at the download stage. That allows reduce program size and get fast speed interpreting.

This is starting page on HyperTerminal with controller connected to PC serial port.

This is a schematic for BASINT on ATMega16/32/64

Schematic in PDF format

1

Schematic in PDF format

Design information about one of variants of BASINT housing you can get here


2. AVR BASINT. COMMANDS AND FUNCTIONS

There are some features of BASINT. - Program lines don’t have numbers. Line numbers are using only for label’s mark.
Numbers:
All numbers are the signed integers in a range from -32767 up to +32767.
There are three forms of numbers representation:

- Decimal;
- Hexadecimal, numbers have a prefix &H;
- Binary, numbers have a prefix &B;

Variables:
There are 26 variables denoted by the letters A through Z. These are represented internally as 16-bit, two's-complement integers.
There are up to 26 one-dimensional arrays. Size of arrays must be defined before using. Maximum size depends of current free memory. The array elements are accessible by an index in the parentheses. For example: A(0), B(18). Note that A an A(n) are the different variables.

Operators
Arithmetic:

+ addition
- subtraction.
* multiply
/ integer division (note that 14/5 = 2
MOD - remainder from division (14 MOD 5 = 4).
AND – bit-wise logical AND (3 AND 6 = 2)
OR – bit-wise logical OR
XOR – bit-wise logical XOR

Compare:(using only with operator IF):
= equal
<> (or ><) not equal
> more
< less
>= (=>) more or equal
<= (=<) less or equal

Expressions:
Expressions are formed with numbers and variables with arithmetic operators between them. Operators of comparison cannot be used in expressions. Parentheses can be used to alter the order of evaluation.
Labels:
Label is a number from 0 to 32767. Labels must be set before line that is a point to jump or is a start line of subroutine. It is not recommended to number all of the lines the labels table is limited up to 64 labels.

 

 

 Keyword Description Small
Atmega16
Mini
Atmega32
Maxi
Atmega64

ABS

Returns absolute value of an expression result * * *

ADC

Reads meassured value from the current channel ADC * * *
AT Sets the position of the printing symbol for PRINT operator   * *
ATTR Sets the console output attributes   * *
BCD Puts to the console a value in BCD form   * *
BIN Puts to the console a value in BIN form   * *
CHR Sends symbol to the console by symbol code that is result of expression.
CHR is an element of the output list for PRINT operator
* * *
CLEAR Sets pin logic level to zero * * *
CLS Clears the screen and move cursor to top left corner   * *
DATA Specifies constant values to be read by READ operator * * *
DEFPIN AS Defines the pin function of the controller pins * * *
DELAY Forms delay * * *
DELETEVECTOR Disconnects the a handler code from vector and disables corresponding interrupt   * *
DIM Defines an array size * * *
DISABLE Disables interrupts   * *
ENABLE Enables interrupts   * *
END Finishes the program execution * * *
FOR TO NEXT Loop operator * * *
GETATOMIC Gets value from a variable used in an interrupt   * *
GOTO Unconditional branch * * *
GOSUB Calls subroutine * * *
HEX Puts to the console a value in hexadecimal form   * *
HIGH Returns high byte of expression result * * *
IF THEN ELSE Condition branch operator * * *
INK Sets the symbol color   * *
INKEY Checks the console buffer for the received symbols * * *
INLINE Creates block of machine code   * *
INPUT Waits for input value * * *
INTERRUPT Creates the block of a machine code at a stage of program loading.
Connects a code to a vector at a stage of execution
  * *
LASTERROR Returns last error or critical situation code * * *
LOW Returns low byte of expression result * * *
PAPER Sets the background color   * *
PEEK Reads byte from memory or I/O port * * *
POKE Writes byte to memory or I/O port * * *
PRINT Prints the messages and the values on the console * * *
PULSEIN Returns pulse width on the controller pin * * *
PULSEOUT Forms pulse on the pin * * *
PUTATOMIC Assigns value of variable used in interrupt subroutine   * *
PWM Puts a digital value out to the PWR channel * * *
READ Reads DATA and moves pointer to next point * * *
READEEPROM Reads byte from the address in EEPROM * * *
REM Comments line. Interpreter skips the rest of code in this line * * *
RESTORE Allows read the DATA block more than one time.
Sets the pointer at the needed labeled block of data
* * *
RETURN Returns from subrouting * * *
ROL Shifts value of variable left. The LSB is set to 0 * * *
ROR Shifts value of variable right. The MSB is set to 0 * * *
SCALE Performs the scaling of variable * * *
SETADMUX Sets the analog multiplexer up * * *
SET Sets pin logic level to one * * *
SHIFTIN Returns the incoming sequence of bits * * *
SHIFTOUT Outputs contents of a variable as sequence of bits * * *
STOP Stops the program execution, clears receiving buffer,
starts after getting new symbol in the input buffer
* * *
TEST Reads pins logic level * * *
TOGGLE Switches pin logic level to the inverted state * * *
TWIINIT Iinitializes TWI (I2C) * * *
TWIREAD Reads byte from TWI (I2C) device. ACK/NACK generate * * *
TWISTOP Generates STOP condition on SDA and SCL pins * * *
TWIWRITE Sends byte to TWI (I2C) device * * *
WAITKEY Is waiting for a single symbol. Returns the code of received symbol * * *
WRITEEEPROM Writes byte to the address in EEPROM * * *

 

Small

Mini

Maxi

Base crystal

ATMEGA16

ATMEGA32

ATMEGA64

Flash Size
for user program

3Ê bytes

16Ê bytes

To be defined

 

2.1 CONSOLE INPUT/OUTPUT

CLS

Action:
Clears the screen and move cursor to top left corner
Syntax:
CLS

PRINT

Action:
Prints the message on the console
Syntax:
PRINT [output list]

The elements of the output list can be separated by semicolon that allows output more than one variables or strings at one line without spaces between the elements. Semicolon at the end of the line cancels adding CR and LF.
The elements of the output list can be separated by comma that inserts number of spaces to set output to the next position of tabulation. Comma at the end of the line cancels adding CR and LF.

Examples:

PRINT Line feed
PRINT “This message” printed: This message
PRINT “X=”;X; “ Y=”;Y; printed: X=(value of X) Y=(value of Y)
PRINT X,Y,Z,

(value of X)     (value of Y)      (value of Z)

 

AT

Action:
Sets the position of the printing symbol for PRINT operator
Syntax:
PRINT AT(x,y);[list]
x – row position (from 1 to 80)
y – column position (from 1 to 24)
Example:

PRINT AT(35,12);“This msg”; AT(1,1); Print "This msg" in the center of the screen and set cursor to the left/up corner

 

CHR

Action:
Sends symbol to the console by its code that is result of expression. Using with PRINT operator
Syntax::
PRINT [list1; or ,] CHR(val)[; or , [list2]]
val – symbol code from 0 to 255. All another integer values will be got as a low byte of word
Example:

FOR I=32 TO 127
     PRINT I,CHR(I)
NEXT
Print symbol codes and symbol itself

 

HEX, BCD, BIN

Action:
HEX – puts to the console a value in hexadecimal form,
BCD - puts to the console a value in BCD form,
BIN - puts to the console a value in BIN form.
Functions HEX, BCD, BIN are the elements of output list for PRINT operator.
Ñèíòàêñèñ:
PRINT [list1; or ,] HEX(value, digits)[; or , [list2]]
PRINT [list1; or ,] BCD(value, digits)[; or , [list2]]
PRINT [list1; or ,] BIN(value, digits)[; or , [list2]]

value – value or expression
digits – number of printed digits

Example:

10 INPUT X
     PRINT X,HEX(X,4);BIN(X,16)
GOTO 10
Print value in different formats
ATTR

Action:
Prints attributes setting
Syntax:
ATTR  expression

An expression defines output attributes:

0 – standard brightness
1 – more light brightness
4 – underscore
5 – blinking
7 – color inverting
8 – set color of symbol and background the same

INK, PAPER

Action:
Sets color attributes for console output
INK – sets the symbol color
PAPER – sets the background color
Syntax:
INK  expession
PAPER  expession

An expession defines the color

0 – black
1 – red
2 – green
3 – yellow
4 – blue
5 – magenta
6 – cyan
7 – white

Note. Using AT, CLS, ATTR, PAPER, INK allows change attributes on ANSI terminal, as console. Most terminals program can support this mode.

 

INPUT

Action:
Waits for input value. Puts value to the variable after receiving CR
Syntax:
INPUT ["Invite msg",] variable
Examples:

INPUT X Print “?” and wait for value
INPUT “X=”,X Print X= and wait for value

 

INKEY

Action:
Checks console buffer for the received symbols. Return –1 if empty and symbol’s code if not.
Syntax:
Variable = INKEY()
Examples:

A=INKEY() Read buffer

10  C=INKEY()
      IF C=-1 THEN GOTO 10

Wait for a symbol


10 C=INKEY()
IF C<>-1 THEN GOTO 20
REM Do some background task
GOTO 10
20 REM Do something upon received symbol
GOTO 10

Waiting for symbol and doing something background

 

WAITKEY

Action:
Is waiting for a single symbol. Returns the code of received symbol.
Syntax:
Variable = WAITKEY()
Examples:

     A=INKEY() Wait for symbol

 


2.2 PROCESS CONTROL OPERATORS

IF / THEN / ELSE

Action:
Condition branch operator
Should be written in one line only.
Syntax:
IF condition THEN operator1 [ELSE operator2]
Examples:

IF X<10 THEN X=X+1 If true then expression

IF X=0 THEN GOTO 100 ELSE GOTO 200

 

IF Z=65 THEN GOSUB 200

If true then call subroutine on label 200

 

FOR / TO / NEXT

Action:
Loop operator
Syntax:
FOR Variable = expression1 TÎ expression 2
some repeating actions
NEXT
Note. Step = 1 always
Examples:

FOR I=32 TO 127
     PRINT CHR(I),I
NEXT

Print symbols with code from 32 to 127

 

GOTO

Action:
Unconditional branch
Syntax:
GOTO label
Example:

GOTO 100

 

 

GOSUB

Action:
Calls subroutine
Syntax:
GOSUB label
Example:

GOSUB 100  

 

RETURN

Action:
Returns from subrouting
Syntax:
RETURN
Example:

100 PRINT X
       RETURN

 

 

END

Action:
Finishes the program execution
Syntax:
END
Examples:

IF Z=0 THEN END

 

 

STOP

Action:
Stops the program execution, clears receiving buffer, starts after getting new symbol in the input buffer
Syntax:
STOP
Examples:

IF Z=0 THEN STOP

    

 

REM

 Action:
Comments line. Interpreter skips the rest of code in this line.
Syntax:
REM [any string]
operator : REM [any string]
Examples:

REM any text

Do nothing in this line of program

A=10: REM any text Does only operator before REM

 

DELAY

Action:
Forms delay 1..65535 mSec
Syntax:
DELAY expression
Examples:

DELAY 100

Forms delay 100 mSec

 

2.3 DATA

DIM

Action:
Defines an array size
Syntax:
DIM list
Note:
Maximum size of array depends of available memory and controller type

 

Small

Mini

Maxi

Base crystal

ATMEGA16

ATMEGA32

ATMEGA64

Size of available memory

140 bytes/
70 elements

1000 bytes/
500 elements

To be defined

        
Examples:

DIM A(10),B(20)

Array A – reserved memory for 10 elements
Array B - reserved memory for 20 elements

 

DATA

Action:
Specifies constant values to be read by READ operator. Note that max length of data line cannot be more than 64 symbols per 1 line. Max lines of data limited only of available flash memory.

Syntax:
DATA list
Pointer sets to begin of first data block when program starts
Examples:

DATA 9999, -1, 23456  

 

READ

Action:
Reads DATA and moves pointer to next point
Syntax:
READ list
Example:

READ A,B,C  

 

RESTORE

 Action:
Allows read the DATA block more than one time. Sets the pointer at the needed labeled block of data.
Syntax:
RESTORE [label]
RESTORE without parameters sets pointer to begin of the first data block.
Examples:

100 DATA 1,2,3,4,5,6
       DATA 7,8,9
110 DATA 555,777,888
….
RESTORE 110

 

 

 

Set pointer to DATA value 555

 

 RESTORE Set pointer to DATA value 1

 

WRITEEEPROM

Action:
Writes byte to the address in EEPROM
Syntax:
WRITEEEPROM address, byte

READEEPROM

Action:
Reads byte from the address in EEPROM
Syntax:
Variable = READEEPROM(address)

 

Small

Mini

Maxi

Base crystal

ATMEGA16

ATMEGA32

ATMEGA64

Size of EEPROM, byte

512

1024

2048

 

2.4 INTERRUTS and MACHINE CODE

ENABLE

 Action:
Enables interrupts
Syntax:
ENABLE

DISABLE

 Action:
Disables interrupts
Syntax:
DISABLE

INLINE

 Action:
Creates block of machine code on program downloading stage. A few lines of INLINE in any place of program create consequtive block of a code in special area of memory. If the block of a code is formed by several lines INLINE, it is necessary to take care, that interpreter will look only on first of them. The rest of INLINEs should be hidden after operators RETURN or GOTO
The block of a code should be issued as the subroutine, last operator should be operator RET.
Communication with BASIC variables is simple, registers R16, R17 contain the pointer to variable A. A principle of formation of the address of any of 26 (from A up to Z) scalar variables following:
Address = value of R16R17 + 2 * (symbol code - 65)

Ñèíòàêñèñ:
  INLINE opcode1, opcode2….
Example:

10  INLINE opcode1, opcode2….

Simple case, code block is formed by single operator INLINE

10  INLINE opcode1, opcode2….
      RETURN
      INLINE opcoden, opcoden+1….

Block of code is formed by two operators INLINE, so the second operator is hidden after RETURN.

Note. It is possible to automate process of creating INLINE blocks. Just use bin2line.exe to convert machine code to INLINE operators.
It is shown below:

avrasm32 -fI -l %1.lst -o %1.hex %1.asm
hex2bin %1.hex %1.bin
bin2line -i %1.bin -o %1.bas

This is a command file that starts process of conversion from asm code to INLINE code. Avrasm32.exe is an assembler from AVR STUDIO. This one assembles to code directly. For example, we will create procedure to access to 16-bit registers of AVR micro. Value of variable A is writing to register OCR1A.

.include "m64def.inc"
mov r30,r16
mov r31,r17
ld  r18,z+
ld  r19,z+
cli 
out OCR1AH,R19
out OCR1AL,R18
sei
ret

INLINE block after conversion:

INLINE 12256,12273,37153,37169,38136,48443
INLINE 48426,38008,38152

You need put it in BASIC program, add label before the first INLINE and write RETURN or GOTO (as you need) after the first INLINE

200   INLINE 12256,12273,37153,37169,38136,48443
RETURN
INLINE 48426,38008,38152

INTERRUPT

Action:
Creates the block of a machine code at a stage of program loading.
Connects a code to a vector at a stage of execution.
If code of interrupt handler does not fit in one line, it is possible to continue in next line, but using INLINE operator. The line with INTERRUPT should be separated from other code by one of operators RETURN, GOTO as you need. At a stage of execution the line with INTERRUPT should be interpreted once for interrupti initialization. Requirements on registration of the block of a code same as well as for operator INLINE. Can be simultaneously used up to 10 vectors. It is impossible to set interruptions For UART0 and TIMER0 as they are used by system.
Syntax:
INTERRUPT vector, opcode1, opcode2….
Example:


INTERRUPT  vector, opcode1, opcode2…

 

200  INTERRUPT  vector, opcode1, opcode2…
         RETURN
         INLINE  opcoden, opcoden+1…

The simpliest case is when the handler takes only one line

The operator INTERRUPT gets the start address of the code block and binds it with needed vector.

 

 

DELETEVECTOR

Action:
Disconnects the a handler code from vector and disables corresponding interrupt.
Syntax:
DELETEVECTOR vector

PUTATOMIC

 Action:
Assigns value of variable used in interrupt subroutine. During this action interpreter disables interrupts to provide complete (atomic) updating a two-byte variable. It is necessary, as operation of giving contains a chain of several commands which could be broken off by the interrupt, providing access to the same variable..
Syntax:
PUTATOMIC var, value

GETATOMIC

Action:
Gets value from a variable used in an interrupt. During this operator executing interrupts are disabled.
Syntax:
some_var = GETATOMIC(var)

2.5 CONTROLER’S PINS ASSIGNMENT AND USE


Pin
number

Main function

Alternative
function

0

PA0

ADC0

1

PA1

ADC1

2

PA2

ADC2

3

PA3

ADC3

4

PA4

ADC4

5

PA5

ADC5

6

PA6

ADC6

7

PA7

ADC7

8

PB0

 

9

PB1

 

10

PB2

 

11

PB3

 

12

PB4

 

13

PB5

 

14

PB6

 

15

PB7

 

16

PC0

 

17

PC1

 

18

PC2

 

19

PC3

 

20

PC4

 

21

PC5

 

27

PD3

 

28

PD4

PWM0

29

PD5

PWM1

30

PD6

 

31

PD7

 


DEFPIN

Action:
Defines the pin function of the controller pins.
The controller pins have numbering from zero up to some maximum value that depends on the type of controller. The Sets of additional pin functions depends on type of the controller too. See table to reference.

 

Small

Mini

Maxi

Type

ATMEGA16

ATMEGA32

ATMEGA64

Available pins

26

26

 

Pins allowed to change direction function DIN/DOUT

0..21, 27..31

0..21, 27..31

 

Pins allowed to set analog mode AIN

0..7

0..7

 

Pins allowed to set PWM function

28,29

28,29

 

Syntax:
DEFPIN pin AS type

Note.

type

function

DIN

Discrete input

DOUT

Discrete output

AIN

Analog input

PWM

Pulse width modulation

Example:

DEFPIN 8 AS DOUT

Sets pin #8 to operate as a discrete output

CLEAR

Action:
Sets pin logic level to zero
Syntax:
CLEARBIT pin

SET

Action:
Sets pin logic level to one
Syntax:
SETBIT pin

TOGGLE

Action:
Switches pin logic level to the inverted state
Syntax:
TOGGLE pin

TEST

Action:
Reads pins logic level
Syntax:
var = TESTPIN(pin)

PWM

Action:
Puts a digital value out to the PWR channel
Syntax:
PWR pwm_pin, value

SETADMUX

Action:
Set the analog multiplexer up
Syntax:
SETADMUX value
Note.
value = reference source + channel number

Reference source

 

0

pin AREF

64

pin AVCC

192

internal 2.56 volt

 

ADC

Action:
Reads meassured value from the current channel ADC
Syntax:
var = ADÑ

 


2.6 ACCESS TO INTERNAL RESOURCE OF MICROCONTROLLER

 

POKE

Action:
Writes byte to memory or I/O port
Syntax:
POKE adr, val

PEEK

Action:
Reads byte from memory or I/O port
Ñèíòàêñèñ:
var = PEEK(adr)



Note: You should reference to avr datasheet to get the address of register. Remember there are two addresses: I/O and memory. PEEK and POKE operate with memory address and and you need to add 32 to get I/O address.

2.6.1 Access to the 16-bit registers

Access to the 16-bit registers of avr micro has features. It uses hardware temporary 8-bit register. From the program point of view, there are some rules to access:
1. Use IN and OUT assembler operators.
2. For write: access to the highest register is first.
3. For read: access to the lowest register is first.

This is an example of two subroutines for access register OCR1A using BASIC variable A:

REM .include "m64def.inc"
REM    mov r30,r16
REM    mov r31,r17
REM    ld  r18,z+
REM    ld  r19,z+
REM    cli 
REM    out OCR1AH,R19
REM    out OCR1AL,R18
REM    sei
REM    ret

REM   WRITE to OCR1A
200   INLINE 12256,12273,37153,37169,38136,48443
RETURN
INLINE 48426,38008,38152

REM  .include "m64def.inc"
REM    mov r30,r16
REM    mov r31,r17
REM    cli 
REM    in R18,OCR1AL
REM    in R19,OCR1AH
REM    sei
REM    st  z+, r18
REM    st  z+, r19
REM    ret

REM   READ from OCR1A
300   INLINE 12256,12273,38136,46378,46395,38008
RETURN
INLINE 37665,37681,38152


2.7 MATH ROUTINES

HIGH

Action:
Returns high byte
Syntax:
var = HIGH(expression)

LOW

Action:
Returns low byte
Syntax:
variable = LOW(expression)

ABS

Action:
Returns absolute value
Syntax:
variable = ABS(expression)

ROL

Action:
Shifts value of variable left. The LSB is set to 0.
Syntax:
ROL variable, number of shifts

ROR

Action:
Shifts value of variable right. The MSB is set to 0.
Syntax:
ROR variable, number of shifts

SCALE

Action:
Performs the scaling
Syntax:
SCALE Variable, multiplier, divider
Examples:


SCALE X,100,Y

Calculates percent of number X from number Y. First X * 100 and put result into 32 bit temporary variable M32, then M32 / Y and put result into X

SCALE X,1000,Y

The same, but 10 times more precision


2.8 SOME BASIC EXTENTIONS

PULSEIN

Action:
Returns pulse width on the controller pin unit that are approximately 1 uS at frequency of quartz 14.745 MHz
Ñèíòàêñèñ:
var = PULSEIN(pin, option, timeout)
Note


option

 

0

Waiting for raise edge and measurement pulse width

1

Waiting for fall edge and measurement of pulse width

 

PULSEOUT

Action:
Forms pulse on the pin
Syntax:
PULSEOUT pin, duration

pin – pin number
duration - duration in uSec for resonator 14.745 MHz

SHIFTIN

Action:
Returns the incoming sequence of bits
Syntax:
var = SHIFTIN(data_pin, clock_pin, bits, delay, option, timeout)
Note:


data_pin

incoming data

clock_pin

Synchro, mode defined in option

Bits

number of bits

Delay

Delay in uSec for resonator 14.745 MHz

Option

Mode of synchronization:
clk_pin – output:
0 – the highest bit the first on rising edge
1 – the highest bit the first on falling edge
2 – the lowest bit the first on rising edge
3 – the lowest bit the first on falling edge
clk_pin – input:
option = option + 4

timeout

Time in mSec on which expiration function returns a zero and variable LASTERROR accepts value 1. Value timeout is actual when clk_pin works as synchro_input

 

SHIFTOUT

Action:
Outputs contents of a variable as sequence of bits
Syntax:
SHIFTOUT var, data_pin, clock_pin, bits, delay, option
Note:


Var

Variable contains a value to be converted to consecutive code

Data_pin

Output consecutive data

clock_pin

Synchro, mode defined in option

Bits

number of bits

Delay

Delay in uSec for resonator 14.745 MHz

Option

Mode of synchronization:
clk_pin – output:
0 – the highest bit the first on rising edge
1 – the highest bit the first on falling edge
2 – the lowest bit the first on rising edge
3 – the lowest bit the first on falling edge

 


2.9 TWO WIRE INTERFACE

TWIINIT

Action:
Iinitializes TWI (I2C)
Syntax:
TWIINIT bps, prescale

bps – number for bps needed
prescale - can be defined from equation:

SCL frequency = CPU Clock frequency / (16 + 2(bps) * 4^prescale)

TWISTART

Action:
Generates START condition on SDA and SCL pins
Syntax:
TWISTART

TWIWRITE

Action:
Sends byte to TWI (I2C) device
Syntax:
TWIWRITE áàéò

TWIREAD

Action:
Reads byte from TWI (I2C) device. ACK/NACK generate
Syntax::
var = TWIREAD(ack)
Parameter ack:
0 – NACK
1 – ACK

TWISTOP

Action:
Generates STOP condition on SDA and SCL pins
Syntax::
TWISTOP

 Example:

  print "***************************"
print "*    DS1339U TWI TEST     *"
print "***************************"
DIM A(16)
TWIINIT 16,2
TWISTART
rem DS1339U Slave address to write
TWIWRITE &HD0
rem Trickle Charger Register (10h)
TWIWRITE &H10:TWIWRITE &B10100101
TWISTOP
10    A=0:TWISTART:TWIWRITE &HD0:TWIWRITE A:TWISTOP
TWISTART:TWIWRITE &HD1
FOR i=0 to 5
A(i)=TWIREAD(1)
NEXT
A(6)=TWIREAD(0):TWISTOP
PRINT "Year:";
if (A(5)AND 128)=0 then print "19";else print "20";
print bcd(a(6),2);
print " Month:";bcd(a(5) AND 127,2);
print " Date:";bcd(a(3),2);" Day:";bcd(a(3),2);
print " ";bcd(a(2),2);":";bcd(a(1),2);":";bcd(a(0)
GOTO 10

2.10 DIAGNOSTICS

At execution of the program there can be situations at which the interpreter stops the work and sends the message on an error in which the code and number of a line is underlined.

Error code

Error

0

Syntax error

1

Unbalanced parentheses

2

No expression present

3

Equals sign expected

4

Not a variable

5

Label table full

6

Duplicate label

7

Undefined label

8

THEN expected

9

TO expected

10

Too many nested FOR loops

11

NEXT without FOR

12

Too many nested GOSUBs

13

RETURN without GOSUB

14

Array not defined

15

Duplicatearray definition

16

Out of memory

17

Array index out of range

18

Pointer out of DATA

20

Can not assign more vectors

LASTERROR

Action:
Returns last error or critical situation code
Syntax:
var = LASTERROR()
Note::

Error code

Error

0

No errors

1

Timed out (PULSEIN, SHIFTIN)

2

TWI error

 

 


3. PRE-PROCESSOR

BASIC language is an extremely simple language, but it is difficult to write big programs by BASIC because the variables length is too short. The best case is when the possible length allows to write “selfcomment” variables. But, long variables, labels could be the cause of slow speed interpreting and resource consuming. A special program-converter can help to resolve this coflict. Our interpreter will still working with short names, but we will work with long names by placing the lines with the preprocessor instruction DEFINE in the program text as shown below:

DEFINE some_long_name = D ‘ defines self comment variable
DEFINE one_more_long_name = 100 ‘ defines constant or label

You can use the apostrophes to start a comment line. At the end you will get something like the program shown below:

 

 

'--------------------------------------------------------
'  TBC GROUP                                        2006
'                    SHIFTOUT TEST
'
'--------------------------------------------------------
' Clock options
define  MSB_FIRST_POSITIVE = 0 ' the highest bit first at raise edge
define  MSB_FIRST_NEGATIVE = 1 ' the highest bit first at falling edge
define  LSB_FIRST_POSITIVE = 2 'the lowest bit first at raise edge
define  LSB_FIRST_NEGATIVE = 3 'the lowestest bit first at falling edge

define  TotalBits  = N ' Êîëè÷åñòâî áèò îò 1 äî 16
define  SrcData    = A ' variable with a source wordvalue
define  DataPin    = D ' data pin number
define  ClockPin   = C ' clock pin number
define  Options    = B ' options
define  PulseDelay = 100 ' defines pulse width that is 2 * PulseDelay
define  MainLoop   = 10  ' Just a name of label 10
CLS
    PRINT "*******************************"
    PRINT "*       SHIFTOUT TEST         *"
    PRINT "*******************************"
    input "DATA PIN=",DataPin
           DEFPIN DataPin AS DOUT:SET DataPin
    input "CLOCK PIN=",ClockPin
           DEFPIN ClockPin AS DOUT:CLEAR ClockPin
MainLoop
    input "TOTAL BITS=",TotalBits
        input "options=",options
          input "data=",SrcData
        shiftout SrcData,DataPin,ClockPin,TotalBits,PulseDelay,Options
GOTO MainLoop

 

 

 

This text needs to be cnvereted before downloading into controller. First we save text of program as shift.bas for example . Then in a command line we can type:
BASPREP.EXE –I shift.bas –O shift.out

Note. BASPREP.EXE must be placed in the same catalogue or be visible of a current arrangement. You can put to C:\WINDOWS for example.

We will get something like shown below as a result of conversion.

 

 

CLS
PRINT"*******************************"
PRINT"*       SHIFTOUTTEST         *"
PRINT"*******************************"
INPUT "DATA PIN=",D
DEFPIN D AS DOUT:SET D
INPUT "CLOCK PIN=",C
DEFPIN C AS DOUT:CLEAR C
10 INPUT "TOTAL BITS=",N
INPUT "options=",B
INPUT "data=",A
SHIFTOUT A,D,C,N,100,B
GOTO 10

 

 

The preprocessor eliminates all of the comments and substitute the long names for theirs short values.


4. PROGRAM DOWNLOADING

HyperTerminal or another terminal application must be set up as shown below:

Baud rate 57600 bps
Data/Stop bits 8/1
Parity None
Flow Control Hardware
Terminal emulation ANSII / VT100
Add sent CR with LF

There are two ways to download.
The first one is by using the text file sending option of the terminal.
The second way is downloading using paste from the clipboard if you are using Windows HyperTerminal, for example.

1.Make sure that write protect jumper is open.
2.Connect the controller to PC by RS232 cable.
3.Power it up.
4.Start terminal application.
5.Press the reset button on the controller.
The start page appears in the terminal window.
6. Press menu option 2 to force the controller be waiting for input text.
7. Send a program by using the text file sending option of the terminal or paste text from the clipboard.
8. Press menu option 1 to list the downloaded program.
9. Press menu option 3 to run the downloaded program.
10. Close write protect jumper. It alows:

- protect program;
- diasable start page screen;
- automatically sart program when the controller is powered up.

___________________________________
By
Alexander Kostyuk ua6ann@mail.ru
Evgeny Fadeev rv3bj@hotbox.ru

05/21/2007