sk getting started with picbasic pro

14
© 2008 Microchip Technology Inc. Page 1 of 18 PICkit™ 2 Starter Kit Getting Started microEngineering Labs PICBASIC PRO™ Compiler Overview This document provides a guide to creating a BASIC language project in MPLAB ® IDE using microEngineering Labs, Inc.’s PICBASIC PRO™ Compiler for PIC ® MCUs. Programming the project with PICkit 2 is also covered. Please note that this document is not intended as an introduction to or tutorial on the BASIC programming language. The PICBASIC PRO programming language is covered in the PICBASIC PRO Compiler manual . Another reference is the introductory book Programming PIC Microcontrollers with PICBASIC by Chuck Hellebuyck. Completing PICBASIC PRO setup for use with MPLAB IDE. 1. Ensure all necessary software and files are installed. This includes the MPLAB IDE, The PICBASIC PRO Compiler demo, and the lessons for the Low Pin Count Demo Board. See the Quick Start link for the Starter Kit on the PICkit Starter Kit CDROM, and click the “Quick Start in BASIC with the PICBASIC PRO Compiler” link. 2. To ensure MPLAB is aware of the microEngineering Labs, Inc.’s PICBASIC PRO Compiler, run the batch file C:\PBPDEMO\PBPPLUG.BAT. This file is installed with PICBASIC PRO Compiler demo. NOTE: In Windows Vista, it may be necessary to right-click the BAT file and select “Run As Administrator.” 3. For the PICBASIC PRO Compiler to work properly, the following directories need to be added to the Windows’ search path: C:\PBPDEMO C:\Program Files\Microchip\MPASM Suite For Windows XP, right-click the “My Computer” icon on the desktop and select Properties . Select the “Advanced Tab” in the System Properties dialog, then click the “Environment Variables” button. In the “System Variables” box, select the “Path” variable and click the “Edit” button. Add the above paths to the end of the “Variable Value” text line, separating each path with a semicolon.

Upload: cirocar

Post on 18-Nov-2014

1.230 views

Category:

Documents


9 download

TRANSCRIPT

Page 1: SK Getting Started With PICBASIC PRO

© 2008 Microchip Technology Inc. Page 1 of 18

PICkit™ 2 Starter KitGetting Started

microEngineering Labs PICBASIC PRO™ Compiler

Overview

This document provides a guide to creating a BASIC language project in MPLAB® IDE using microEngineering Labs, Inc.’s PICBASIC PRO™ Compiler for PIC® MCUs. Programming the project with PICkit 2 is also covered.

Please note that this document is not intended as an introduction to or tutorial on the BASICprogramming language. The PICBASIC PRO programming language is covered in the PICBASIC PRO Compiler manual. Another reference is the introductory book Programming PIC Microcontrollers with PICBASIC by Chuck Hellebuyck.

Completing PICBASIC PRO setup for use with MPLAB IDE.1. Ensure all necessary software and files are installed. This includes the MPLAB IDE, The

PICBASIC PRO Compiler demo, and the lessons for the Low Pin Count Demo Board. See the Quick Start link for the Starter Kit on the PICkit Starter Kit CDROM, and click the “Quick Start in BASIC with the PICBASIC PRO Compiler” link.

2. To ensure MPLAB is aware of the microEngineering Labs, Inc.’s PICBASIC PRO Compiler, run the batch file C:\PBPDEMO\PBPPLUG.BAT. This file is installed with PICBASIC PRO Compiler demo.NOTE: In Windows Vista, it may be necessary to right-click the BAT file and select “Run As Administrator.”

3. For the PICBASIC PRO Compiler to work properly, the following directories need to be added to the Windows’ search path:C:\PBPDEMOC:\Program Files\Microchip\MPASM Suite

For Windows XP, right-click the “My Computer” icon on the desktop and select Properties. Select the “Advanced Tab” in the System Properties dialog, then click the “Environment Variables” button. In the “System Variables” box, select the “Path” variable and click the “Edit” button. Add the above paths to the end of the “Variable Value” text line, separating each path with a semicolon.

Page 2: SK Getting Started With PICBASIC PRO

PICkit™ 2 Getting Started with the PICBASIC PRO Compiler

© 2008 Microchip Technology Inc. Page 2 of 14

For instructions on other Windows operating systems see the microEngineering Labs website at http://melabs.com/support/mplab.htm

4. Restart your computer before continuing.

Page 3: SK Getting Started With PICBASIC PRO

PICkit™ 2 Getting Started with the PICBASIC PRO Compiler

© 2008 Microchip Technology Inc. Page 3 of 14

Creating a PICBASIC PRO project in MPLAB IDE

1. Connect the PICkit™ 2 Microcontroller Programmer to the PC with the included USB cable. Plug the 6-pin header on the demo board into the PICkit 2 ICSP™ connector.

2. Start MPLAB IDE from the shortcut icon on the desktop.

3. From the MPLAB IDE menu bar, select Project > Project Wizard…

4. This opens up the Project Wizard. Click Next to continue.

Page 4: SK Getting Started With PICBASIC PRO

PICkit™ 2 Getting Started with the PICBASIC PRO Compiler

© 2008 Microchip Technology Inc. Page 4 of 14

5. Wizard Step One: The project target device is the PIC16F690 included on the Low Pin count Demo Board. In the wizard, select the “PIC16F690” from the drop-down box and click Next.

6. Wizard Step Two: Here we select that we want to use the “microEngineering PicBasic Pro Toolsuite” for the project language toolsuite from the Active Toolsuite drop-down box.

Page 5: SK Getting Started With PICBASIC PRO

PICkit™ 2 Getting Started with the PICBASIC PRO Compiler

© 2008 Microchip Technology Inc. Page 5 of 14

7. The first time PICBASIC PRO is selected as the toolsuite, it is necessary to tell the MPLAB IDE where the compiler executable is. In the “Toolsuite Contents” box, select the “microEngineering PicBasic Pro Compiler” then click the Browse… button. In the file dialog, browse to C:\PBPDEMO and select the PBPWDEMO.EXE file. Click Open.

The “Location” box will now show the full path to the executable. Click Next to continue.

8. Wizard Step Three: Create a new project. The BASIC code file that we will use for this project is installed with the Low Pin Count Demo Board Lesson Files. Select the Browse…button and navigate to the folder C:\Pk2 Lessons\LPC Demo Board\PICBASICPRO as the project directory. In the Save Project As dialog, type the project name “PICBasicDemo” and click Save. Click Next.

Page 6: SK Getting Started With PICBASIC PRO

PICkit™ 2 Getting Started with the PICBASIC PRO Compiler

© 2008 Microchip Technology Inc. Page 6 of 14

9. Wizard Step Four: Add the existing BASIC code demo file to the project. The file tree view box on the left should already be expanded to the project directory, where the demo file pk2_tutorial.bas was installed as part of the Starter Kit Lessons. Select the file and click the Add >> button to add it to the project. Click Next when done.

10. Wizard Summary: Click the Finish button. A new workspace and project are created in the MPLAB IDE. A “workspace” contains information on the selected PIC MCU device, the active programmer and/or debugger, open windows and their location, and other IDE configuration settings. The workspace is also associated with a “project”, which contains the files needed to build an application (source code, include files, linker scripts, etc.) along with associated language (compiler) tools and build options.

Page 7: SK Getting Started With PICBASIC PRO

PICkit™ 2 Getting Started with the PICBASIC PRO Compiler

© 2008 Microchip Technology Inc. Page 7 of 14

11. One last step before the project is ready to be compiled. The tutorial BASIC program uses the PIC16F690 pin 4 as the input RA3 instead of as the /MCLR external reset. This requires changing the settings in the 16F690.INC include file for PICBASIC PRO. To simplify things, an edited version is included. Copy it into the PBPDEMO directory.Copy file

C:\Pk2 Lessons\LPC Demo Board\PICBASICPRO\16F690.INCover the existing file in

C:\PBPDEMO\

IMPORTANT

The 16F690.INC file included with the Low Pin Count Demo Board lessons must be copied into the C:\PBPDEMO\ directory over the existing file as described in step 11. If this is not done the tutorial program will not work!

Compiling and Programming the PICBASIC PRO Demo Program

1. Open the project window by selecting View > Project. The project window displays the project files and is shown below on the left with the source file we added in the Project Wizard, pk2_tutorial.bas.Select menu item View > Output to open the output window, shown below on the left. The output window displays the results from various associated tools, including the PICBASIC PRO Compiler under the “Build” tab and the PICkit 2 under the “PICkit 2” tab (not yet shown).

Page 8: SK Getting Started With PICBASIC PRO

PICkit™ 2 Getting Started with the PICBASIC PRO Compiler

© 2008 Microchip Technology Inc. Page 8 of 14

2. Open the BASIC language program file by double-clicking the file name pk2_tutorial.bas in the Project window. The program file will open in an editor window on the MPLAB IDE workspace. The tutorial will go over the code itself in a later section.

3. To compile and build project code, select Project > Compile from the MPLAB IDE menu bar.

The results of the Compile will appear under the “Build” tab of the Output Window, and the compiled code is now ready to program in the PIC16F887.

Page 9: SK Getting Started With PICBASIC PRO

PICkit™ 2 Getting Started with the PICBASIC PRO Compiler

© 2008 Microchip Technology Inc. Page 9 of 14

4. Ensure the PICkit 2 Microcontroller Programmer and Low Pin Count Demo Board are connected as shown on Page 1 ofthis document. From the MPLAB IDE menu bar select Programmer > Select Programmer > PICkit 2.

The Output Window shows connection to the PICkit 2 Microcontroller Programmer, and that the target microcontroller (a PIC16F690 on the LPC Demo Board) was found.

If the PICkit 2 or demo board was not plugged in, connect them and select the MPLAB IDE menu Programmer > Connect to try connecting again.

5. On the PICkit 2 Low Pin Count Demo Board, the pushbutton (SW1) signal goes into the PIC16F690 at pin 4, which is being used as the RA3 input pin in the demo program. This pin may also be used as an external reset for the PIC16F690, and the PICkit 2 is connected to this pin to control the MCLR function if it were being used. To prevent the PICkit 2 from interfering with the SW1 pushbutton signal, a few settings need to be changed in MPLAB. Select Programmer > Settings in the MPLAB IDE. This will open the “PICkit 2 Settings” dialog. On the “Settings” tab, check the box next to “Run after a successful program”. This will cause the PICkit 2 to release the MCLR signal when you program it. Also check the box next to “3-State on ‘Release from Reset’”. This instructs the PICkit 2 to tri-state the MCLR signal instead of driving it to VDD. This allows SW1 to change the signal and be used by the program. Click OK to close the dialog.

Page 10: SK Getting Started With PICBASIC PRO

PICkit™ 2 Getting Started with the PICBASIC PRO Compiler

© 2008 Microchip Technology Inc. Page 10 of 14

6. Select menu Programmer > Program to program the compiled code into the PIC16F690.

The results of the programming operation will appear in the Output Window under the PICkit 2 tab.

7. The demo code is now executing in the PIC MCU on the Low Pin Count Demo Board! The potentiometer RP1 may be turned to change the number of LEDs turned on, and the button SW1 pressed to reverse the direction they turn on in.

Page 11: SK Getting Started With PICBASIC PRO

PICkit™ 2 Getting Started with the PICBASIC PRO Compiler

© 2008 Microchip Technology Inc. Page 11 of 14

Exploring the Demo BASIC Code

The demonstration BASIC program pk2_tutorial.bas creates a “bar graph” display of the 4 LEDs on the Low Pin Count Demo Board, with the number of lighted LEDs corresponding to the position of the potentiometer RP1. Pressing the switch SW1 reverses the LED display direction.

The program demonstrates configuring and using IO port pins, converting an analog voltage from the potentiometer with the PIC16F690 ADC peripheral, and reading the SW1 pushbutton state.

The schematic for the Low Pin Count Demo Board may be found in the Appendix of the Low Pin Count Demo Board Users Guide. It will also be helpful to reference the PIC16F690 Data Sheet for PIC16F690 operation, peripheral, and register definitions.

The pk2_tutorial.bas program file begins with a title block and description of the program requirements. These are comment lines, as is any text that follows a single quote. The program itself begins with setting up the Analog to Digital (A/D) converter. The project only requires eight bits of resolution and setting the clock source for the A/D conversion. Two simple DEFINE statements in PICBASIC PRO handle this setup. The PIC16F690 internal oscillator is used as the clock source (known as the Fosc) for both the microcontroller and the A/D peripheral. The internal oscillator defaults to 4 MHz on reset and this is divided down by four to create a 1 MHz internal instruction clock. The clock for the A/D converter has different optionsto choose from and the Fosc/8 source is selected with the second DEFINE statement.

' Define ADCIN parametersDefine ADC_BITS 8 ' Set number of bits in resultDefine ADC_CLOCK 1 ' Set clock source (1=Fosc/8)

Variables are created to store information in the program. A variable for the A/D value and the number of LEDs are created as byte variables. A single bit variable is created to indicate which direction to drive the LEDs based on a switch press.

' Establish variablesadval var byte ' Create adval to store resultbars var byte ' Variable to store the number of LEDs to lightswitchstate var bit ' Bit variable to indicate if the switch was pressed

The next section presets all the necessary PIC16F690 internal registers. The ANSEL register used to select IO pins for digital or analog mode. It is set to a one because only the AN0 pin needs to be an analog input. The rest of the A/D pins are setup as digital I/O.

' Initialize Internal Registers

ANSEL = 1 ' Intialize AN0 to A/D only

Page 12: SK Getting Started With PICBASIC PRO

PICkit™ 2 Getting Started with the PICBASIC PRO Compiler

© 2008 Microchip Technology Inc. Page 12 of 14

The internal comparators are shut off by clearing the CM1CON0 and CM2CON0 registers.

CM1CON0 = 0 ' Initialize Comparator 1 offCM2CON0 = 0 ' Initialize Coparator 2 off

The TRISx registers control the direction (input or output) of port I/O pins. All of PORTC is set to outputs by clearing the TRISC register. All of PORTA is set to inputs by setting all the bits in the TRISA register.

TRISC = 0 ' PORTC all outputs for LEDsTRISA = $FF ' PORTA all inputs for switch

The PORTx registers set the output pin states when written to, and return input pin states when read. The PORTC register is cleared so all the LEDs are off.

PORTC = 0 ' Preset LEDs off

Just before the main loop of code the “switchstate” bit variable is preset to one. This will make the LEDs scroll from left to right initially.

'*** Main Program Loop ****switchstate = 1 ' Preset Switch State to Drive LEDs to Right

The main loop starts with the “Main” label. Then the ADCIN command is used to measure the voltage at the AN0 pin and store the eight bit result in the variable “adval”.

Main'*** Read A/D value *****

ADCIN 0, adval ' Read channel 0 to adval

The result value is then shifted to the right six bits so the two most significant bits (MSB) are remaining giving a result of 0,1,2 or 3 that is stored in the variable “bars”.

'*** Drive LEDs *********bars = (adval >> 6) 'Shift for 2 MSB for values 0 to 3

The value of “bars” is then used as an index to select a value to send to the PORTC register using the LOOKUP command. Each selection lights a number of LEDs that matches the value of “bars” plus one.There are two LOOKUP commands though and each is selected based on the value of the “switchstate” value using the IF-THEN-ELSE command.

if switchstate = 1 then 'Test for LED directionlookup bars,[1,3,7,15], PORTC 'Drive LEDs to Rightelselookup bars,[8,12,14,15], PORTC 'Drive LEDs to Leftendif

The LEDs will light immediately because the LOOKUP command is placing the value directly into the PORTC register. This outputs the value on the PORTC pins.

Page 13: SK Getting Started With PICBASIC PRO

PICkit™ 2 Getting Started with the PICBASIC PRO Compiler

© 2008 Microchip Technology Inc. Page 13 of 14

Then the program then monitors the switch SW1. If the logic level at the PORTA pin 3 pin is low then a switch press is sensed and a list of commands is executed. If it’s not low then the rest of the commands are skipped and the program jumps to the “goto main” command line.

'*** Read State of Switch with 100 msec Debounceif PORTA.3 = 0 then 'Switch is pressed

When the switch is pressed, the program delays for 100 milliseconds and checks it again. This forms a very basic debounce on the switch. If the switch is still pressed then the value of the “switchstate” variable is toggled.

pause 100 'Delay 100 msecif PORTA.3 = 0 then 'Test switch againswitchstate = ~switchstate 'Toggle Switch State Flag

The switch could still be pressed after the toggle is complete so the program waits for the button to be released before moving on. The program loop is so short, without this step the switch could be sensed a second time even though it was only pressed once.

holdif PORTA.3 = 0 then hold 'Wait for switch to be released

endifendif

Finally the program loops back to the “Main” label using a GOTO command to start the process all over again.

goto main 'Loop back for new A/D sample

Page 14: SK Getting Started With PICBASIC PRO

PICkit™ 2 Getting Started with the PICBASIC PRO Compiler

© 2008 Microchip Technology Inc. Page 14 of 14

Quick Reference

Compiling and programming with PICkit 2 in MPLAB IDEProgrammer > Select Programmer > PICkit 2 (Not needed if already selected)Project > CompileProgrammer > Program

Next Steps

To find out more about developing code in the MPLAB IDE, see the MPLAB IDE Quick Start Guide included in the “Reference” section of the PICkit Starter Kit CD-ROM. The most recent version of this document is also available on the included MPLAB IDE CD-ROM.

The PIC16F690 Data Sheet is the best resource for learning about the operation of the microcontroller and its peripherals. The schematic diagram of the demo board is found in the Appendix of the Low Pin Count Demo Board User’s Guide.

Microchip’s Online Discussion Groups at forum.microchip.com are a good place to ask questions and get information on developing with PIC MCUs. The [Development Tools] –Programmers sub-forum is the best place for PICkit 2 related topics.