ME Labs, Inc.
719-520-5323
 
Home:
  Developer Resources:

Programming Clues
    Sample Programs
   
    PICBASIC PRO™
Compiler Manual
    PICBASIC™ Compiler
Manual
    Serin2/Serout2 Modes
    ASCII Character Set
    Number Conversion
    Floating Point
Routines
    PBP Debug Monitor
    Articles and Tutorials

Hardware Clues
    Parts / Vendor List
    PICPROTO™ Boards
    LAB-X1 Docs
    LAB-X2 Docs
    LAB-X20 Docs
    LAB-X3 Docs
    LAB-X4 Docs
    LAB-XUSB Docs
    LAB-XT Docs
     
 

ME Labs, Inc. | 1-719-520-5323 | Example Program - BLINK1934X.pbp

PICBASIC PRO program to cycle bargraph LEDs on PORTD in different patterns, with speed control & oscillator switching from external to internal. Uses POT1 on RA0 to set cycle speed, and keypad button #13 to alternate between LED patterns & oscillator selection.
' Name        : BLINK1934X.pbp
' Compiler    : PICBASIC PRO Compiler 2.6
' Assembler   : MPASM
' Target PIC  : 40-pin 16F1934 or similar
' Hardware    : LAB-X1 Experimenter Board
' Oscillator  : 4MHz external crystal
' Keywords    : ADCIN, DO LOOP, EXIT, FOR NEXT, REPEAT UNTIL, STEP
' Description : PICBASIC PRO program to cycle bargraph LEDs on PORTD in different 
' patterns, with speed control & oscillator switching from external to internal.
' Uses POT1 on RA0 to set cycle speed, and keypad button #13 to alternate between
' LED patterns & oscillator selection.
'

' Define ADCIN parameters
Define  ADC_BITS     10  ' Set number of bits in result
Define  ADC_CLOCK    4   ' Set clock source Fosc/4
Define  ADC_SAMPLEUS 50  ' Set sampling time in uS

i     Var Byte           ' Define loop variable
Delay Var Word           ' LED delay time
LEDS  Var PORTD          ' Alias PORTD to LEDS
 
   Gosub Init            ' Hardware initialization routine
   
mainloop:
   LEDS = 1              ' First LED on
   Do
     For i = 1 To 7      ' Go through For..Next loop 7 times
       LEDS = LEDS << 1  ' Shift on LED to the left
       Gosub GetDelay    ' Get new Delay value
       Pause Delay       ' Pause for Delay period
     Next i
     
     ' Exit allows us to exit the loop mid-stream
     IF !PORTB.4 Then Exit ' Exit Do Loop if key pressed
     
     For i = 7 To 1 Step-1 ' Go through For..Next loop 7 times
       LEDS = LEDS >> 1  ' Shift on LED to the right
       Gosub GetDelay    ' Get new Delay value
       Pause Delay       ' Pause for Delay period
     Next i 
   Loop Until PORTB.4 = 0  ' Loop until press on keypad button #13
   
   ' Flipping OSCCON.1 to 1 selects the internal oscillator.
   ' Flipping OSCCON.1 to 0 selects the external oscillator.
   
   OSCCON.1 = 1        ' Switch from 4MHz external to 16MHz internal oscillator

   Repeat              ' Change LED pattern on keypad button #13 press
     Gosub GetDelay
     LEDS = %00011000
     Gosub GetDelay    ' Get new Delay value
     Pause Delay
     LEDS = %00100100
     Gosub GetDelay    ' Get new Delay value
     Pause Delay
     LEDS = %01000010
     Gosub GetDelay    ' Get new Delay value
     Pause Delay
     LEDS = %10000001
     Gosub GetDelay    ' Get new Delay value
     Pause Delay
     LEDS = %01000010
     Gosub GetDelay    ' Get new Delay value
     Pause Delay
     LEDS = %00100100
     Gosub GetDelay    ' Get new Delay value
     Pause Delay
   Until PORTB.4 = 1   ' Repeat above until keypad button #13 is released
   
   OSCCON.1=0          ' Switch back to external 4MHz oscillator
   Goto mainloop       ' Blink LEDs forever
   
GetDelay:
   Adcin 0,Delay       ' Read POT1 on RA0 input for 10-bit Delay value
   Return

Init:
   OSCCON = %01111000  ' Internal osc is 16MHz, but using external for now
   TRISD = 0           ' Set PORTD to all outputs
   TRISB = %11110111   ' RB3 to output, rest inputs 
   PORTB.3 = 0         ' RB3 low for keypad button #13 low input
   ANSELA = 1          ' Set PortA 0 to analog POT1 input, rest digital
   ANSELB = 0          ' portb all digital
   ANSELD = 0          ' portd all digital
   ANSELE = 0          ' porte all digital  
   ADCON1 = %11000000  ' Right justify for 10-bit, Fosc/4, +Vref/-Vref = Vdd/gnd
   OPTION_REG.7 = 0    ' Internal pull-ups on (RB4 is held high until key #13 is pressed)
   Return              ' Setup complete, return to caller
   
   End

           

Download the program file.