IBD Limited Şirketi - İzmir / Türkiye
379/1 Sokak A-Blok No: 2/101 - AFA Sanayi Çarşısı - II. Sanayi - 35100 Bornova / İzmir
Telefon : 90-232-462 7477-78 Telefax : 90-232-462 7545 - e-mail : [email protected]

ibdline

PicBasic Pro Compiler Sample Programs



' Blink an LED connected to PORTB.0 about once a second



loop:   High 0          ' Turn on LED connected to PORTB.0

        Pause 500       ' Delay for .5 seconds



        Low 0           ' Turn off LED connected to PORTB.0

        Pause 500       ' Delay for .5 seconds



        Goto loop       ' Go back to loop and blink LED forever

        End



' Demonstrate operation of an LCD in 4-bit mode

'

' LCD should be connected as follows:

'       LCD     PICmicro

'       DB4     PORTA.0

'       DB5     PORTA.1

'       DB6     PORTA.2

'       DB7     PORTA.3

'       RS      PORTA.4 (add 4.7K pullup resistor to 5 volts)

'       E       PORTB.3

'       RW      Ground

'       Vdd     5 volts

'       Vss     Ground

'       Vo      20K potentiometer (or ground)

'       DB0-3   No connect



        Pause 1500      ' Wait 1.5 seconds for LCD to reset



loop:   Lcdout $fe, 1   ' Clear lcd screen

        Lcdout "Hello"  ' Display Hello

        Pause 500       ' Wait .5 second



        Lcdout $fe, 1   ' Clear lcd screen

        Lcdout "World"

        Pause 500       ' Wait .5 second



        Goto loop       ' Do it forever



' Access PIC16C7x A/D



        Include "modedefs.bas"



SO      var     PORTB.0                 ' Serial output on PORTB, pin 0



        ADCON1 = 0                      ' Set PORTA 0-3 to analog inputs

        ADCON0 = %01000001              ' Set A/D to Fosc/8, Channel 0, On

        Pause 1                         ' Wait 1ms for setup



loop:   ADCON0 = %01000101              ' Start Conversion

        Pause 1                         ' Wait 1ms for conversion



        Serout SO,N2400,[#ADRES,10]     ' Send variable to serial out



        Goto loop



' Write to the first 16 locations of an external serial EEPROM

' Read first 16 locations back and send to serial out repeatedly



        Include "modedefs.bas"



SO      con     0                       ' Serial Output

DPIN    var     PORTA.0                 ' I2C data pin

CPIN    var     PORTA.1                 ' I2C clock pin

B0      var     byte

B1      var     byte

B2      var     byte



        For B0 = 0 To 15                ' Loop 16 times

                I2CWRITE DPIN,CPIN,$A0,B0,[B0]  ' Write each location's address to itself

                Pause 10                ' Delay 10ms after each write

        Next B0



loop:   For B0 = 0 To 15 step 2         ' Loop 8 times

                I2CREAD DPIN,CPIN,$A0,B0,[B1,B2]        ' Read 2 locations in a row

                Serout SO,N2400,[#B1," ",#B2," "]       ' Print 2 locations

        Next B0



        Serout SO,N2400,[10]            ' Print linefeed



        Goto loop



' LCD clock program using On Interrupt

'  Uses TMR0 and prescaler.  Watchdog Timer should be

'  set to off at program time and Nap and Sleep should not be used.



hour    var     byte    ' Define hour variable

minute  var     byte    ' Define minute variable

second  var     byte    ' Define second variable

ticks   var     byte    ' Define pieces of seconds variable

update  var     byte    ' Define variable to indicate update of LCD



        hour = 0        ' Set initial time to 00:00:00

        minute = 0

        second = 0

        ticks = 0



        update = 1      ' Force first display



' Set TMR0 to interrupt every 16.384 milliseconds

        OPTION_REG = $d5        ' Set TMR0 configuration

        INTCON = $a0            ' Enable TMR0 interrupts

        On Interrupt Goto tickint





' Main program loop - in this case, it only updates the LCD with the time

mainloop: If update = 1 Then

                Lcdout $fe, 1   ' Clear screen



                ' Display time as hh:mm:ss

                Lcdout dec2 hour, ":", dec2 minute, ":", dec2 second



                update = 0      ' Screen updated

        Endif



        Goto mainloop





' Interrupt routine to handle each timer tick

        disable         ' Disable interrupts during interrupt handler

tickint: ticks = ticks + 1      ' Count pieces of seconds

        If ticks < 61 Then tiexit       ' 61 ticks per second (16.384ms per tick)



' One second elasped - update time

        ticks = 0

        second = second + 1

        If second >= 60 Then

                second = 0

                minute = minute + 1

                If minute >= 60 Then

                        minute = 0

                        hour = hour + 1

                        If hour >= 12 Then

                                hour = 0

                        Endif

                Endif

        Endif



        update = 1      ' Set to update LCD



tiexit: INTCON.2 = 0    ' Reset timer interrupt flag

        Resume



' PICcalc serial calculator



sopin   var     PORTB.0         ' Serial output pin

sipin   var     PORTB.1         ' Serial input pin



bmode   con     4               ' Serial baud mode to N2400



a0      var     word            ' Define our variables

a1      var     word

char    var     byte

i       var     byte

op1     var     byte

op2     var     byte





start:

        Serout sopin, bmode, ["Ready"]

        a0 = 0

a0loop: Gosub getchar

        If (char >= "0") and (char <= "9") Then

                a0 = (a0 * 10) + (char - "0")

                Goto a0loop

        Endif



opent:  op1 = char

        op2 = 0

oploop: Gosub getchar

        If (char >= "0") and (char <= "9") Then opout

        If op2 = 0 Then

                op2 = char

        endif

        Goto oploop



opout:  a1 = char - "0"

a1loop: Gosub getchar

        If (char >= "0") and (char <= "9") Then

                a1 = (a1 * 10) + (char - "0")

                Goto a1loop

        Endif



        Gosub doop              ' Do the operation

        If char = "=" Then resout       ' Send out the result and start over

        Goto opent              ' Else chain the operations





doop:

        i = 255

        Lookdown op1,["+-*/<>&|^~admnrs"],i

        Branch i,[doadd,dosub,domul,dodiv,doshl,doshr,doand,door,doxor,donot,doabs,dodcd,domax,doncd,dorev,dosqr]



        Goto error



doadd:  a0 = a0 + a1

        Return



dosub:  a0 = a0 - a1

        Return



domul:  If op2 = "*" Then domuh

        If op2 = "/" Then domum

        a0 = a0 * a1

        Return



domuh:  a0 = a0 ** a1

        Return



domum:  a0 = a0 */ a1

        Return



dodiv:  If op2 = "/" Then domod

        a0 = a0 / a1

        Return



domod:  a0 = a0 // a1

        Return



doshl:  a0 = a0 << a1

        Return



doshr:  a0 = a0 >> a1

        Return



doand:  If op2 = "/" Then donand

        a0 = a0 & a1

        Return



door:   If op2 = "/" Then donor

        a0 = a0 | a1

        Return



doxor:  If op2 = "/" Then donxor

        a0 = a0 ^ a1

        Return



donot:  a0 = ~a1

        Return



donand: a0 = a0 &/ a1

        Return



donor:  a0 = a0 |/ a1

        Return



donxor: a0 = a0 ^/ a1

        Return



doabs:  a0 = abs a1

        Return



dodcd:  If op2 = "i" Then dodig

        a0 = dcd a1

        Return



dodig:  a0 = a0 dig a1

        Return



domax:  If op2 = "i" Then domin

        a0 = a0 max a1

        Return



domin:  a0 = a0 min a1

        Return



doncd:  a0 = ncd a1

        Return



dorev:  a0 = a0 rev a1

        Return



dosqr:  a0 = sqr a1

        Return





getchar: Serin sipin, bmode, char       ' Get one character

        If char = 27 Then error         ' Escape

        If char = 13 Then               ' Turn cr into "="

                char = "="

        endif

        Serout sopin, bmode, [char]     ' Echo it back

        Return



resout: Serout sopin, bmode, [#a0, 13, 10]  ' Display result and cr

        Goto start



error:  Serout sopin, bmode, ["Error", 13, 10]

        Goto start


Home | PicBasic Pro Compiler | PicBasic Compiler | EPIC Plus Programmer | PICProto Boards | Bundles | Books | Ordering Information


IBD Ltd. Şti.
Back to the IBD Ltd. Şti. home page.
Please send your comments about our website to the Dipl.-Ing. Faruk DERİ (VDI)
© IBD - Ltd. Şti.

Last update 3/30/99