What is PICBASIC?

PPICBASIC is a micro-controller developed by COMFILE Technology and is programmed in a form of the BASIC language. Because that most of micro-controllers is programmed in the Assembly (or C) language, even though you spend lot of time to learn how to use it, you still need much more time to obtain . This is why we introduce PICBASIC to market. PICBASIC has BASIC interpreter inside of a microcontroller on board so that you can program micro controller in a form of the BASIC language with ease.

 

Hardware

A PICBASIC module has two major components, a PICmicro microchip and an EEPROM. User's program is stored in the EEPROM after being converted into Áß°£ÇüÅÂ. the EEPROM is non-volatile storage and can retain memory even without electric power. The PICmicro microchip contains PICBASIC interpreter translating and executing user's program inside of the EEPROM.

<< PICBASIC PB-1S >>

<< PICBASIC PB-2S >>

 

Programming PICBASIC module

To program a PICBASIC module, you should connect the PICBASIC module to your PC first.
Then, you should make a source file in PICBASIC studio, which is Integrated Development Environment for PICBASIC module.
After completing the source file, click the RUN button in the tool bar of PICBASIC studio. Then, PICBASIC studio will execute Compile, Download and Run in order automatically.

 

MAINTENANCE

When you need to revise a program which is already written in PICBASIC module, you just simply re-connect the PICBASIC cable to PC, and revise the program on PICBASIC studio. The program is stored at EEPROM, which keeps its data even at power-off state.

  

Integrated Development Environment

PICBASIC studio is Integrated Development Environment for PICBASIC. It includes Compiler, Editor, Debugger and etc and has similar usage as a word processor and is compatible with Microsoft Windows 98/ME/NT/2000/XP. You can get it from COMFILE program CD provided with PICBASIC products or download from download section of www.comfile.co.kr

  

PICBASIC MODULE

PICBASIC series ha various type of PICBASIC moudes and have PBM series and PB series. PBM series is advanced PICBASIC modules which have extra instruction, floating point operation, handing string etc.

 

PB series

EEPROM

SRAM

I/O ports

Function

PB-1B

2K

96 Byte

16

22 PIN

PB-1S

 4K

96 Byte

16

22 PIN, A/D 8chennel

PB-2S

8K

96 Byte

27

34 PIN, DIP package

PB-2H

 16K

96 Byte

27

Equivalent with PB-2S except 20Mhz of execution speed

PB-3B

4K

80 Byte

21

One chip type

PB-3H

4K

80 Byte

29

One chip type

 

PBM series

EEPROM

SRAM

I/O ports

Function

PBM-R1

62K

8K Byte

32

Floating point operation, String, Hardware RS232C, Mathematical function

PBM-R5

 64K

32K Byte

34

12bits A/D, Built-in RTC and battery, Floating point operation, String, Hardware RS232C, Mathematical function

 

INSTRUCTIONS
Instructions in PICBASIC is very similar with that in common BASIC language such as QBASIC and consists of Command and Function. Function is used as part of expression and has round brackets at the end.

Command : PRINT, GOTO, RETURN
Function : ADIN(0), EEREAD(0)

The followings are how to express constants in PICBASIC.

-Decimal: 10, 20, 32, 1234
-Hexadecimal: &HA, &H1234, &HABCD
-Binary: &B10001010, &B10101

** Make certain that PICBASIC recognize all Lower-case letter as Capital letter in compiling.


VARIABLES
There are five types of variable in the PICBASIC language. PB series can use only two types of variable among them, Byte and Integer type, while PBM series use all five types of variable.

- Byte : 8bit number without sign (0~255)
- Integer : 16bit number without sign (0~65535)
- Long : 32bit number with sign (-2,147,483,648~2,147,483,647)
- Single : 32bit floating point number with sign
- String : 8bit character (extended up to 92 byte at most)

Byte variable is a type of 8bit number without sign and reserves 8bits (1byte) of memory space. Therefore, its expression range is 0~225.
Integer variable is a type of 16bit number without sign and reserves 16bits (2bytes) of memory space. So, its expression range is 0~65535.
Long variable is a type of 32bit number with sign and reserves 32bits (4bytes) of memory space. So, its expression range is -2,147,483,648~2,147,483,647.
Single variable is a type of 32bit of floating point number with sign. In accordance with recommendation of IEEE 754, it consists of seven bits of exponential part, twenty three bits of point part and one sign bit.
String variable is available in PBM series only. It can be 93byte at most and must be used with value indicating its maximum bytes for reserving memory space. Refer to the following example.

The name of variable must be start with English character. the name of Command and Function cannot be used as variable name.
ex) naming variable
valid: A, B0, I, J, TH, BF1
Invalid: 23, 3A, INPUT, GOTO

Variable definition must be placed in the first part of program according to the following format.
DIM   I AS  BYTE  ' Define I as Byte type variable
DIM  J  AS  INTEGER ' Define J as Integer type variable
DIM  I  AS  BYTE, J AS BYTE 'Used in a row with the rest mark. 
DIM  ST  AS  STRING * 16 ' When String variable, you should note the maximum bytes. 


DEFINING CONSTANT
Sometimes, it can be convenient to express particular numbers in the form of characters in maintaining a program. PICBASIC language provides CONST instruction to define Constant.
CONST RELAYPORT = 5 ' Hereafter, RELAYPORT is substituted for 5¡°.
OUT RELAYPORT, 1 ' You can understand at once that RELAY turns ON.


ARRY

You can also define Byte type one-dimensional array up to 65535 elements in PICBASIC.

DIM A(20) AS BYTE ¡® Define 20 of A array
DIM B(200) AS INTEGER ¡® Integer array
DIM C(200) AS LONG ¡® Long array
DIM D(200) AS SINGLE ¡® Single array

Parameter of array starts from 0. Therefore, when 20 elements are defined, you can use from 0 to 19. Because that PB modules have limited size of data memory, maximum definition range of array is same as size of data memory. i.e., PB-1S has 96bytes of data memory. Therefore, it is possible to use array up to 96bytes. You cannot use array as parameter of array as follows.

I = ARRAY1 (K(J))


CONSTANT ARRAY

Constant is invariant value while a program operates. PICBASIC provides a function allowing user to definite several constants like array. This function is very useful to handling a lot of data. You can use data defined as constant array as array in program. The following example shows how to definite/use constant array.

CONST BYTE DATA1 = (31, 25, 102, 34, 1, 0, 0, 0, 0, 0, 65, 64, 34)
I = 0
A = DATA1 (I) ¡®Return 31
I = I + 1
A = DATA1 (I) ¡®Return 25


Other data type of constant array is also available.

CONST INTEGER DATA1 = (6000, 3000, 65500, 0, 3200)
CONST LONG DATA2 = (12345678, 356789, 165500, 0, 0)
CONST SINGLE DATA3 = (3.14, 0.12345, 1.5443, 0.0, 32.0)


You can extend data used for constant array in several lines.

CONST BYTE DATA1 = (31, 25, 102, 34, 1, 0, 0, 0, 0, 0, 65, 64, 34,
12, 123, 94, 200, 0, 123, 44, 39, 120, 239,
132, 13, 34, 20, 101, 123, 44, 39, 12, 39)


You can use string data as constant array.

CONST BYTE DATA1 = (¡°I LOVE PICBASIC 2000¡±, 13, 10, 0)

The difference between common array and constant array is that data defined as constant array shall be recorded in program memory when it is downloaded into a PICBASIC module. General array is recorded in data memory (SRAM) and is not conserved after RESET. But, constant array remains in program memory even after RESET.


EXPRESSION 

Expression in PICBASIC forms as follows.

I = 0  ' Input 0 to variable I
I = I + 1  ' Increase variable I by 1
I = J * 12 / K  ' Multiply variable J by 12 and, divide by variable K

You can use Byte/Integer variable, constant and function as an operator.If a destination variable is Byte type, the result of an operation is stored in the form of Byte type even though the result exceeds 255. (Make sure that value exceeding 8bits is cut.) However, if a destination variable is Integer type, the result of an operation is stored in the form of Integer type.

The following table shows operators available in PICBASIC.

Arithmetical operation
(Available in all variable types)
Logical operation and shift
(Available in Integer variable only)
+
Addition AND Logical operation AND
-
Subtraction OR Logical operation OR
*
Multiplication XOR Logical operation XOR
/
Division << Left shift
>>
Right shift
 
MOD remainder of division

FLOATING POINT OPERATION
PBM series provide Floating point number operation. Operation in each types of variable must be done with corresponding variables and constants. For instance, Integer operation must consist of Integer variables and constants. If you want to use an Integer variable in floating point number operation, you should convert the Integer variable into a Single variable by CSNG instruction.

DIM S1 AS SINGLE
DIM I AS LONG
S1 = S1 + 1 ¡®Valid expression
S1 = S1 + 1.0 ¡®Invalid expression.All constant in floating point expression must have point.
S1 = S1 + I ¡®Invalid expression (Used Integer variable and Single variable together)
S1 = S1 + CSNG(I) 'Valid expression (Used CSNG instruction for converting variable type)

Logical/MOD operation is not available with Single variable/constant.

PB series module doesn't provide Floating point number operation.
Operation priority is in order of multiplication, division, bit-operation, addition and subtraction.

I = J + 12 * K ' Multiply 12 by K, and add J
I = J + I AND &HF ' Add AND and &HF, and add the result and J

Complicated operation has possibility of occurring errors during compiling. In that case, it is recommended to divide the operation into several simple operations.

I = (J * K) + L / 4 * K


Instructions with * mark is available with PBM module only

DECLARATION

DIM Declare variable and array
CONST Declare constant
CONST BYTE Declare Byte constant array (0~255)
CONST INTEGER Declare Integer constant array (0~65535)
CONST LONG Declare Long (32bit) constant array
CONST SINGLE Declare Single constant array


FLOW CONTROL

IF ... THEN Evaluate a condition and, if it is true, go to appointed line
FOR ... NEXT Repeat execution of instructions
GOTO Branch to specific line unconditionally
GOSUB ...RETURN Call a subroutine and Return


DIGITAL I/O

IN() Read status of port
BYTEIN()   Read eight ports simultaneously
OUT Set status of port HIGH or LOW
BYTEOUT Change eight ports simultaneously
OUTSTAT() Read value being outputted from a port
TOGGLE Reverse current value being outputted
PULSE Reverse output state during specific period (PULSE out)


RS232C

SERIN Receive RS232C transmission by software
SEROUT Sent RS232C transmission by software
GET Receive RS232C transmission
PUT Sent RS232C transmission by hardware
SET RS232 Initialize RS232C by hardware 
BLCR Initialize receiving buffer of RS232C
BLEN () Return number of data received at receiving buffer of RS232C


SHIFT I/O

SHIFTIN Shift Input (for I2C, SPI communication)
SHIFTOUT Shift Output ( for I2C, SPI communiation)


ANALOG I/O

ADIN() Read value of AD conversion
PWM Output PWM wave
PWMOFF Stop outputting PWM wave
DACOUT Output PWM wave for D/A conversion during certain period


SOUND

SOUND Generate semplice sound
BEEP Generate key-touch sound
PLAY Play music


LCD CONTROL

LCDINIT Initialize a LCD
CLS Clear LCD screen
LOCATE Appoint cursor's location
PRINT Display Captial letter or Digit
CSRON Turn a cursor ON
CSROFF Turn a cursor OFF
BUSOUT Transmit specific code to PICBUS
SET PICBUS Change transmission speed of PICBUS into 19200/4800 baud rate


EEPROM ACCESS

EEWRITE Write data at specific location of EEPROM
EEREAD Read data from specific location of EEPROM


INTERRUPT

ON TIMER GOSUB Execute specific routine periodically
ON INT GOSUB Execute specific routine by generating edge at port8
ON RECV GOSUB Execute specific routine by interrupt of hardware RS232C
SET ONTIMER ON/OFF Timer interrupt ON/OFF
SET ONINT ON/OFF  Edge interrrupt of port8 ON/OFF
SET ONRECV ON/OFF  RS232C receiving interrupt ON/OFF


KEY-INPUT

ADKEYIN() Receive key-input by A/D conversion
PADIN() Read keypad (4x4 key-matrix)
EPADIN() Read keypad (8x8 key-matrix)
KEYIN()   Read status of port
KEYDELAY() Adjust value of delay and repeat onkey-input


LOCATION CONTROL

FREQOUT Generate random frequency (Using PWM port)
CAPTURE Measure cycle of outer wave
STEPOUT Output pulse for controlling STEP motor
SERVO Control position of RC SERVO motor


REAL-TIME

TIME() Read real-time data
TIMESET Set time


SPECIAL

RND() Generate random value
TABLE() Transform a table by parameter
ON ... GOTO Branch by parameter
ON ... GOSUB Execute subroutine by parameter
BREAK Generate break
COUNT() Read value of count from CLKIN port
PEEK() Read content of specific address(RAM) on main microprocessor
POKE Change content of specific address(RAM) on main microprocessor
RESET  Reset
DELAY Set 1~65535 mS of delay
CAPTURE Measure cycle of outer wave


STRING

DEC() Convert a constant/variable into decimal string
HEX() Convert a constant/variable into hexadecimal string
FLOAT() Convert a Real constant/variable into real string
LEFT() Cut characters from left part of string
RIGHT()  Cut characters from right part of string
MID() Cut characters in the middle of string
ASC() Return ASCII code of first character in string
CHR() Convert a constant/variable to character
VAL() Convert digit character of string into value
VALSNG() Convert real digit character of string to value


CONVERSION

CINT() Convert into Integer type
CLNG() Convert into Long type
CSNG() Convert into Single type


MATHEMATICAL FUNCTION

SIN() Return value of SIN
COS() Return value of COS
ABS() Return absolute value
EXP() Return exponential function eX
LOG()    Return value of natural logarithm
LOG10() Return 10 based natural logarithm
SQR()    Return value of square root
POW() Compute XY

 

Temperature-sensing with Digital themistor DS1620

The first example of application with PICBASIC is temperature-sensing with digital themistor DS1620.
DS1620 has themistro inside and can send data from the temistor through serial communication (SPI protocol) to a controller (PICBASIC module). Because it sends real value of temperature, you can use it without conversion.
You can control DS1620 with easy by SHIFT instruction in PICBASIC becuase of using SPI protocol.
The following picture is Pin-Out of DS1620.

<< Temperature-sensing circuit with DS1620 >>

Description of circuit
Internally, the chip is accessed through CLK, DQ and RST. This is little different from original SPI protocol but is stil a kind of SPI protocol. (D1 and D0 pins are shorted internally)
When &HAA, 8 bits of instruction code, is sent, the DS1620 returns 9 bits of current temperature value in Binary code.

Temperature

Binary code

Hexadecimal code

+125

0 11111010

00FA

+25

0 00110010

0032

+0.5

0 00000001

0001

0

0 00000000

0000

-0.5

1 11111111

01FF

-25

1 11001110

01CE

-55

1 10010010

0192

 

Source program

               DIM TH AS INTEGER
               DIM J AS BYTE
               LCDINIT
LOOP:     OUT 0,1
               SHIFTOUT 1,2,0,&
               TH = SHIFTIN(1,2,0,9)
               OUT 0,0
               LOCATE 0,1
               PRINT HEX(TH)
               GOTO LOOP

' Declaration?br>


' Send instruction code to DS1620
' Read 9 bits of temperature data


' Display the data on LCD

 

Interfacing with SGN module

SGN module has dedicated module for Dynamic interfacing method, which is commonly used in handling Seven Segmnets. Therefore, users can display data on SGN by sending data to Dynamic moule through RS232C.SGN can display character, special mark, etc as well as digit.

<< SGN(Seven Segment Network) ¸ðµâ >>

<<Circuit for connection between PICBASIC module and SGN module>>

 

Instruction Format for interfacing with SGN

Because a SGN module operates in 5V level of RS232C, there's no need of extra conversion circuit to interface with a PICBASIC module at 9600 baud rate. Each SGN module has unique ID code. The ID code, which can be set with DIP S/W on the back of SGN module, is useful in using several SGN modules on one line.
The following example is for displaying "1234" on SGN.

Source Porgram

 LOOP: SEROUT 8,30,0,1,[&HE0,1,&H31]    ' Digit 1
            SEROUT 8,30,0,1,[&HE0,2,&H32]    ' Digit 2
            SEROUT 8,30,0,1,[&HE0,3,&H33]    ' Digit 3
            SEROUT 8,30,0,1,[&HE0,4,&H34]    ' Digit 4
            SEROUT 8,30,0,1,[&HE0,5,&H35]    ' Digit 5
            GOTO LOOP

 

Real Time Clock DS1302

DS1302 is Clock & Calender chip revising time data such as second, minute, hour, date, month and year (including leap year) automatically.
The following picture shows Pin-out of DS1302.

DS1302 has internally several data areas. Each area contains and revises each of time data (second, minute, hour, year, month, day, date).
The following picture shows the lay-out of internal data areas.

This is order of transmission. Transmission will perform in order of command, address and data by SHIFTOUT instruction. (SHIFTIN instruction is used to read data while SHIFTOUT is used to write data.)

The following example program is for initializing SECOND area (address 0) by supplying power, and reading data of SECOND area, and displaying the data on a LCD.
When you write into DS1302, you must turn "PROTECT" off by using command, &H8E.

                DIM  I AS BYTE
                DIM ADR AS BYTE
                OUT 10, 1
                SHIFTOUT 8,9,0,&H8E,8   
       ' PROTECT OFF
                SHIFTOUT 8,9,0,0,8
                OUT 10,0
                OUT 10,1
                SHIFTOUT 8,9,0,&H80,8  
        ' Clear SECOND area
                SHIFTOUT 8,9,0,0,8
                OUT 10,0
 LOOP:      OUT 10,1
                ADR = &H81
                SHIFTOUT 8,9,0,ADR,8
                I = SHIFTIN(8,9,2,8)                 ' Read SECOND data
                I = ( I << 1 ) OR I.7
                OUT 10,0
                LOCATE 0,0
                PRINT HEX(I)        
                  ' Display the SECOND data on LCD
                GOTO LOOP

 

Copyright ¨Ï 2000 COMFILE Technology All rights reserved.
104-5, Guro 5-dong, Guro-ku, Seoul, 152-842, South Korea
Tel.: +82-2-711-2592 Fax: +82-2-711-2593