PDA

View Full Version : [Beginner Tutorial] How to get started with PICBASIC PRO Demo, MPLAB & PICKIT 2



mister_e
- 23rd June 2011, 08:18
So you're new to PBP and want a simple step-by-step guide? This one will show how how to get started with


PICBASIC PRO Demo
MPLAB
PICKIT 2 programmer
PICKIT 2 Low Pin Count Demo Board (DM164120-1) PIC16F690
Windows 7 64 bits.

This tutorial assume a "next to none knowledge" level. Sorry for the "more advanced" user.


Go on Microchip website and download the latest MPLAB version (http://www.microchip.com/Microchip.WWW.SecureSoftwareList/secsoftwaredownload.aspx?device=en019469&lang=en&ReturnURL=http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en019469&part=SW007002#) (As of June 22, 2011, Latest Version is 8.70)
Extract and run the Setup file
If you're running Vista of Win 7, you want to raise your privilege, right click on the file then, click on "run as administrator"
Accept the agreement
If you want to install only the minimum, when prompted for the Setup Type, choose Custom and follow the instructions bellow. Unless, jump to step 7
Use the default Destination Folder
Check the following when you'll be prompted to select the features to install

Microchip Device Support
8 Bit MCU
Microchip Application
MPASM Suite
MPLAB IDE
MPLAB SIM
PICKIT 2


Continue the installation normally, wait 'till the end
Go on Melabs Website and download PBPDemo (http://melabs.com/downloads/PBPDemo6.exe)
Run the install file
If you're running Vista of Win 7, you want to raise your privilege, right click on the file then, click on "run as administrator"

Accept the agreement
Use the default installation folders
Continue the installation and decide if you want to Install Microcode Studio... or not. I suggest you do so, it's a pretty nice tool.


I also suggest you to download and install the PICKIT 2 standalone application (http://ww1.microchip.com/downloads/en/DeviceDoc/PICkit%202%20v2.61.00%20Setup%20A.zip), there's quite a few interesting feature included in. Just Install it in the default folder.
Create a new folder to store & test our new code. It's a good idea to create one close to the root.
Let's use C:\PBP_Prog

That's it for the installation phase

mister_e
- 23rd June 2011, 08:20
Now it's time to configure the programming environment. First thing to do is to add PBP Language Toolsuite to MPLAB


Open C:\PBPDEMO\MPLAB_Plugin folder
Run setup_PBPSUITE
If you're running Vista of Win 7, you want to raise your privilege, right click on the file then, click on "run as administrator"

As simple as that!

Time to configure PBP within MPLAB


Start MPLAB
On the top menu, Click on Project
From this menu select Set Language Toolsuite Locations
in the Registered Tools list, click on the little + on the left side of microEngineering PICBASIC PRO Toolsuite
Then again on the little + on the left side of Executables
Finally click on microEngineering PICBASIC PRO Compiler (PBPMPLAB.BAT
The Location field should indicate C:\PBPDEMO\PBPMPLAB.BAT)
Click on OK

In theory we're done with it... let's try tro start a new project.

mister_e
- 23rd June 2011, 08:25
There's quite different way to start a new project within MPLAB. Here I'll show how to start a new PicBasic project using a PIC16F690 with the Project Wizard.


On the top menu, Click on Project
Select Project Wizard
Click on Next
Choose PIC16F690 from the Device drop down list
Click on Next
Time to tell MPLAB we'll be using PICBASIC PRO
From the Active Toolsuite drop down list, choose microEngineering PICBASIC PRO Toolsuite... obvious I know.
Click on Next
On the right side of Create New Project File field, Click on Browse
Choose the folder we created to store our codes & project C:\PBP_Prog
In the Filename Field type LedBlinky
Click on Save
Click on Next
You'll be prompted to add some source code and files... click on Skip
We're done, Click on Finish



5698


Looks familiar? Great. Now what we need to do, is to create, and add a source code to it.


On the top menu click on File
Choose New, this will open a new window called Untitled. Our source code will go there
Let's give our source code a name.
On the top menu click on File
Choose Save As
In the Filename field, type LedBlinky.bas
Click on Save


Project is created, Source Code file is created, but have a look at the project explorer windows on the left... there's no source code included...yet. There's two way to add/include your source code to it.


In the project explorer, right click on Source Files and then on Add Files
On the top menu, click on Project then choose Add Files to the project


Both do the same job, I prefer the right click thing... it's up to you to decide.


5699


Ok so now we should have everything setup up properly and we are now ready to jump on the dark side. BUT before we jump in, as we're going to use a PIC16F690 and Microchip LPC Demo board, it's a good idea to download some references to make friend with


The PIC16F690 Datasheet (http://ww1.microchip.com/downloads/en/DeviceDoc/41262E.pdf)
Microchip's LPC Demo Board User Guide (http://ww1.microchip.com/downloads/en/DeviceDoc/Low%20Pin%20Count%20User%20Guide%2051556a.pdf)


I assume you have ALL 5 JUMPER installed.

Let's observe the board schematic (PDF page 41)


5700

Is there some gotcha in it? Sure...


There's no crystal attached to the PIC.
Q. And then?
A. We will need to configure the PIC to work with it's own internal OSC. If we don't do it, the PIC will never run...
LEDs are attached to some pin who have some enabled by default analog peripheral.
Q. How do you know that?
A. The PIC pinout gives some hints, but It's always a good to have a look in the datasheet,
Section 4.5 PORTC and TRISC Registers (pdf page 78-82)
Table 4-3 resume it really well, you want to dig the datasheet to see how to set them.

Q. What if I don't care about the datasheet and just go ahead typing some code for...what kind problem it may cause?
A. You'll be the really first one on earth ;) Your milleage may vary, but usually a LED connected to an analog pin doesn't work at all. Have a good look at This thread (http://www.picbasic.co.uk/forum/showthread.php?t=561) for more infos.


While we're at it, bookmark This thread (http://www.picbasic.co.uk/forum/showthread.php?t=11116) in your internet browser. It contains a heap of good info.

mister_e
- 23rd June 2011, 08:27
Code Example #1: Led Blinky

Time to get dirty. Copy/Paste the following code in the source code windows


'-------------------------------< LED Blinky >------------------------------------
'
' File name : LedBlinky.bas
' Version : 1.0
' Company : Mister E
' Programmer : Steve Monfette
' Date : June 22, 2011
' Device : PIC16F690
'
'---------------------------------------------------------------------------------

'---------------------------< Project description >-------------------------------
'
' Beginner code example to blink a Led on PORTc.0 using:
' 1) PBPDEMO version
' 2) MPLAB
' 3) PICKIT 2 Programmer
'
'---------------------------------------------------------------------------------

'----------------------------< Hardware listing >---------------------------------
'
' - Microchip Low Pin Count Demo Board
' - PIC16F690 installed
' - *** All jumper have to be installed ***
' - Target board will be powered by the PICKIT 2
'
'---------------------------------------------------------------------------------

'
' Pic Configuration
' =================
' All PIC configuration fuses (Configuration bits) are listed in the
' C:\PBPDEMO\p16f690.inc file. Also see datasheet section 14.1
'
@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_ON & _PWRTE_ON
' _INTRC_OSC_NOCLKOUT: Using Internal Oscillator
' _WDT_ON: Enable Watch Dog Timer
' _MCLRE_ON: Enable MCLR
' _CP_OFF: Disable Code Protection
' _CPD_OFF: Disable Data Code Protection
' _FCMEN_OFF: Disable Fail Safe Clock Monitor
' _IESO_OFF: Disable Internal/External switchover
' _BOR_ON: Enable Brown out detect
' _PWRTE_ON: Enable Power up Timer
'
OSCCON = %01110000 ' datasheet section 3
' -x-------- n/a
' --111----- IRCF<2:0> Internal Oscillator Frequency Select Bits (8MHz)
' -----xxxx- Read Only
'
DEFINE OSC 8 ' tells PBP we use a 8 MHZ clock

'
' Hardware assignment
' ===================
' Here we assign Aliases to PORT pin
' this makes the code easier to read
' I'll use the Aliases name as written on the schematic
'
' LEDs
' ----
DS1 VAR PORTC.0
DS2 VAR PORTC.1
DS3 VAR PORTC.2
DS4 VAR PORTC.3

'
' Hardware configuration
' ======================
'
' I/Os (Datasheet section 4.0 & above)
' ------------------------------------
TRISA = 0 ' All PORTA I/O set as Output (When capable of)
TRISB = 0 ' All PORTB I/O set as Output
TRISC = 0 ' All PORTC I/O set as Output
'
' ADCs (datasheet section 9)
' --------------------------
ANSEL = 0 ' Disable all
ANSELH = 0 ' ADCs
'
' Comparator (datasheet section 8)
' --------------------------------
' At Power On, the comparator are already disabled
' but let's play safe.
'
CM1CON0 = 0 ' Disable comparator 1
CM2CON0 = 0 ' Disable comparator 2

'
' Software/Hardware initialisation
' ================================
PORTA = 0 ' clear PORTA output pins (0v)
PORTB = 0 ' clear PORTB output pins (0v)
PORTC = 0 ' clear PORTC output pins (0v)

WHILE !OSCCON.2 : Wend ' Wait untill the internal OSC get stable

'------------------------------< Main program >-----------------------------------
'
Start:
ds1 = 1 ' turn ON LED DS1
PAUSE 500 ' wait 500 mSec
ds1 = 0 ' turn OFF LED DS1
PAUSE 500 ' wait 500 mSec
GOTO Start ' do it again
'
'---------------------------------------------------------------------------------
Let's compile it.


From the top menu, click on Project
Choose Compile (F10 also work but that's your job to find some shortcuts ;))


Oh noes...
ERROR: Unable to execute mpasmwin.Error[118] C:\PBP_PROG\LEDBLINKY.ASM 63 : Overwriting previous address contents (2007)

Welcome in real life! Compilation error happen! Deal with it now!

First of, have a good reading at this thread (http://www.picbasic.co.uk/forum/showthread.php?t=543), at least POST #1 & POST #5. Post #5 explain how to fix it. Once you've fixed the 16F690.inc file, hit F10, and it will compile fine.

Loaded C:\PBP_Prog\LedBlinky.COF.
BUILD SUCCEEDED: ...

Aaaaahhhh, feel better now. Now let's program the PIC and do some light show.


On the top menu, click on Programmer
Click on Select Programmer and choose PICKIT 2
The Output window should show you a message like
Initializing PICkit 2 version 0.0.3.63
Found PICkit 2 - Operating System Version 2.32.0
Target power not detected - Powering from PICkit 2 ( 5.00V)
PIC16F690 found (Rev 0x5)
PICkit 2 Ready

There's no real problem with the above message, in fact, it confirm we have the right PIC installed, and the PICKIT 2 WILL provide the 5Volt to our target board. Fabulous!
On the top menu, click on Programmer
Click on Program
OutPut
Programming Target (2011-06-23 01:35:20)
PIC16F690 found (Rev 0x5)
Erasing Target
Programming Program Memory (0x0 - 0x4F)
Verifying Program Memory (0x0 - 0x4F)
Programming Configuration Memory
Verifying Configuration Memory
PICkit 2 Ready


AND we have a really fabulous... not working light show. Why? Use you multimeter and measure the voltage across Vdd & Vss... there's something there. How about MCLR pin? Duh... 0V.. That mean the PICKIT 2 is holding MCLR to GND... now we need to fix it.

How to?


On the top menu, click on Programmer
Click on Settings
Check Run after a sucessful program option
Click on Apply
Click on OK
Still nothing there, just program your PIC again and see what happen


Yeah! LedBlinky rocks!

mister_e
- 23rd June 2011, 08:30
Code Example 2: ADC Reading


Start a new project in MPLAB, name it ADCToLed
Create a new source code file, name it ADCToLed
Add the source code to the project
Copy/Paste the following code to the source code windows




'--------------------------------< ADCToLed >------------------------------------
'
' File name : ADCToLed.bas
' Version : 1.0
' Company : Mister E
' Programmer : Steve Monfette
' Date : June 22, 2011
' Device : PIC16F690
'
'---------------------------------------------------------------------------------

'---------------------------< Project description >-------------------------------
'
' Beginner code example to display the ADC reading from channel AN0 to 4 LEDs
' attached on PORTC<3:0>
' 1) PBPDEMO version
' 2) MPLAB
' 3) PICKIT 2 Programmer
'
'---------------------------------------------------------------------------------

'----------------------------< Hardware listing >---------------------------------
'
' - Microchip Low Pin Count Demo Board
' - PIC16F690 installed
' - *** All jumper have to be installed ***
' - Target board will be powered by the PICKIT 2
'
'---------------------------------------------------------------------------------

'
' Pic Configuration
' =================
' All PIC configuration fuses (Configuration bits) are listed in the
' C:\PBPDEMO\p16f690.inc file. Also see datasheet section 14.1
'
@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_ON & _PWRTE_ON
' _INTRC_OSC_NOCLKOUT: Using Internal Oscillator
' _WDT_ON: Enable Watch Dog Timer
' _MCLRE_ON: Enable MCLR
' _CP_OFF: Disable Code Protection
' _CPD_OFF: Disable Data Code Protection
' _FCMEN_OFF: Disable Fail Safe Clock Monitor
' _IESO_OFF: Disable Internal/External switchover
' _BOR_ON: Enable Brown out detect
' _PWRTE_ON: Enable Power up Timer
'
OSCCON = %01110000 ' datasheet section 3
' -x-------- n/a
' --111----- IRCF<2:0> Internal Oscillator Frequency Select Bits (8MHz)
' -----xxxx- Read Only
'
DEFINE OSC 8 ' tells PBP we use a 8 MHZ clock

'
' Hardware assignment
' ===================
' Here we assign Aliases to PORT pin
' this makes the code easier to read
' I'll use the Aliases name as written on the schematic
'
' LEDs
' ----
DS1 VAR PORTC.0
DS2 VAR PORTC.1
DS3 VAR PORTC.2
DS4 VAR PORTC.3
LED VAR PORTC

'
' Hardware configuration
' ======================
'
' I/Os (Datasheet section 4.0 & above)
' ------------------------------------
TRISA = %00000001
' -xx------- n/a
' ---00000-- Output: When capable of
' --------1- Input: AN0, ADC input

TRISB = 0 ' All PORTB I/O set as Output
TRISC = 0 ' All PORTC I/O set as Output
'
' ADCs (datasheet section 9)
' --------------------------
ANSEL = %00000001
' -0000000-- AN<7:1> = Digital I/O
' --------1- AN0 = Analog

ANSELH = 0 ' AN<11:8> = Digital I/O

DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in
'
' Comparator (datasheet section 8)
' --------------------------------
' At Power On, the comparator are already disabled
' but let's play safe.
'
CM1CON0 = 0 ' Disable comparator 1
CM2CON0 = 0 ' Disable comparator 2

'
' Software variable
' =================
bADC VAR BYTE ' Hold the ADC reading

'
' Software/Hardware initialisation
' ================================
PORTA = 0 ' clear PORTA output pins (0v)
PORTB = 0 ' clear PORTB output pins (0v)
PORTC = 0 ' clear PORTC output pins (0v)

WHILE !OSCCON.2 : Wend ' Wait untill the internal OSC get stable

'------------------------------< Main program >-----------------------------------
'
Start:
ADCIN 0, bADC ' read AN0 and store it to bADC variable
'
' Since we use only 4 LED and the ADC is a 8 Bit result, we
' need to adjust it so the ADC range will fit a 4 BITs LED reading
' To do so we will shift the ADC by 4 bits
bADC = bADC>>4
LED = bADC ' show the result
PAUSE 100 ' wait 100 mSec
GOTO Start ' do it again
'
'---------------------------------------------------------------


Compile
Program


Now see what happen when you play with the pot attached to AN0 input.

Soon you'll discover you can't turn on the 4 LED together. The reason is simple. Inside the PICKIT 2, both PGD & PGC (ICSPDAT & ICSPCLK) pins are pulled down with 4.7K resistor. Now leaving the PICKIT 2 connected to your board, R7 make a voltage divider with those internal internal pull-down. So the voltage never go up to 5 volt on the ADC input...

To solve this problem you could disconnect your PICKIT 2 and use an external power supply OR modify your code to use another ADC channel OR just lift the pin 4 of the ICSP header. Still plausible to trick the ADC by using anothe Vref... oh well it's up to you now.

Enjoy, be creative, have fun!

I know I know, it's almost a copy/paste thread from the MicroCode studio one... so?

bigabbs
- 11th June 2012, 09:28
Hi
I have reached the point of lesson 11and have created a file C:\PBP_Prog,
where do I go to continue the tutorial

Regards Bigabbs

rkotheslayer
- 21st July 2012, 10:40
From the Active Toolsuite drop down list, choose microEngineering PICBASIC PRO Toolsuite... obvious I know.

Hi friend, I have few question:

Is that all source code can be edit,compile and upload into PIC16f877A thru MPlab?
As I know in order to do process that I mentioned above, I need to install MPLAB & PICKIT 2 to complete the process that I mentioned above right?

Note:
If not mistaken, I am using Basic language as my source code for the project

mackrackit
- 21st July 2012, 14:14
Have you purchased and installed PicBasicPro yet?

Archangel
- 21st July 2012, 20:19
PicBasicPro turns English like mnemonics into assembly code. MPLAB turns assembly code into an Intel hex file and the PICKit2 loads that hex into the PIC Micro.

citationjet100
- 18th October 2014, 13:51
I am programming with a 16F628A and can't figure out the code for the PIC configuration. I am using a 20mhz crystal and have configured the program with the DEFINE OSC 20. I am at a loss to get the chip config to be accepted though.
Thanks

HenrikOlsson
- 18th October 2014, 14:38
Hi,
For 20MHz operation you need to set the HS_OSC option. If you're using PBP3 then try

#CONFIG
__config _HS_OSC & _WDT_ON & _MCLRE_ON & _LVP_OFF & _CP_OFF
#ENDCONFIG

/Henrik.

andybarrett1
- 18th October 2014, 17:38
My little bit of advice don't forget:-
:-

MCLRE_ON means that MCLRE acts as a reset pin. It needs to be tied to +5v with a resistor (10K or similar)

Caught me out several times…. Or turn just off if you don't need a reset facility.

//Andy

citationjet100
- 18th October 2014, 20:52
I copied the code to the program file and still got syntax errors.

#CONFIG
__config _HS_OSC & _WDT_ON & _MCLRE_ON & _LVP_OFF & _CP_OFF
#ENDCONFIG

I am using MicroCode Studio Pro to complile the program and PICKIT2 for programming.
Thanks
Dave

Archangel
- 18th October 2014, 23:47
I copied the code to the program file and still got syntax errors.

#CONFIG
__config _HS_OSC & _WDT_ON & _MCLRE_ON & _LVP_OFF & _CP_OFF
#ENDCONFIG

I am using MicroCode Studio Pro to complile the program and PICKIT2 for programming.
Thanks
Dave
. . . and no mention of which version of PBP . . .
Hi Dave,
The configs you are using are only for version 3.xx and later versions to come.
If you are using any earlier version this thread has everything you need to know: http://www.picbasic.co.uk/forum/showthread.php?t=543

ver 3.xx Make sure you have some spaces just before __config as Assembly is fussy that way7479