PDA

View Full Version : [Beginner Tutorial] How tro get started with PBPDEMO, MicroCode Studio & PICKIT 2



mister_e
- 22nd June 2011, 23:11
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 version,
MicroCode Studio
PICKIT 2 programmer
PICkit 2 Low Pin Count Demo Board (DM164120-1) PIC16F690
under Windows 7 64 bits.


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


Go on melabs Website and Download PICBasic Demo & MicroCode Studio (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 Install Microcode Studio


Go at the Pickit 2 page (http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en023805) and download those 2 files
(Version listed as of June 21, 2011)

Pickit 2 v2.61 Standalone (http://ww1.microchip.com/downloads/en/DeviceDoc/PICkit%202%20v2.61.00%20Setup%20A.zip)
PK2CMD v1.20 (http://ww1.microchip.com/downloads/en/DeviceDoc/PK2CMDv1-20.zip)


Extract all .ZIP in different folder to avoid some mess.
Install the standalone utility first.... Again
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"
Create a new folder for the command line application
Let's use C:\Program Files (x86)\Microchip\PICkit 2 cmd
Copy all file extracted from the PK2CMDv1-20.zip archive to that folder
now 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
- 22nd June 2011, 23:44
Open MicroCode Studio. Your milleage may vary but your screen should look like


5691


Now we need to specify few things to the editor, the first one is where the compiler is located. On the Menu bar, click on View, then on Compile and Program option


5692
Make sure you see whats in the red circle, case not, just click on Find Automatically button. For now don't care about the other options... the first LED blink is way more important :)

Now click on Assembler tab.


5693


Again, Make sure you see whats in the red circle, case not, just click on Find Automatically button

Let's configure MicroCode Studio to use PICKIT 2 programmer. In the same Compile and Program Options window


Click on Programmer tab
Click on Add new programmer
from the drop down menu select Microchip Pickit II - Command line
Click on Next


MicroCode Studio is now set to use PICKIT 2 programmer. But now let's make sure everything is properly set


Click Edit...
Programmer filename have to be pk2cmd.exe
Click on Next
Now we need to set the executable path. IUt seems the Find automatically button is not working properly on my machine, again your milleage may vary so let's click on Find Manually
Select the folder we previously choosed C:\Program Files (x86)\Microchip\PICkit 2 cmd
Click on Next
There's quite a few parameter you can pass to the command line utility, Microcode already set some, let's use the default one and click on Finished
Should you need to modify the default one, All options are listed in the Readme For PK2CMD.txt file located in C:\Program Files (x86)\Microchip\PICkit 2 cmd folder


Now we need to specify where to load/store our code.


On the Menu bar click on View
Click on Editor Options
In the General tab, click on the Change... button
Select our C:\PBP_Prog folder
click OK


We're all set. Exit MicroCode Studio and Plug your PICKIT 2 in your USB port. The driver should install without any problem.

mister_e
- 22nd June 2011, 23:48
We are now almost ready to jump on the dark side. BUT before to do so, 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 JUMPERS installed. Let's observe the board schematic (PDF page 41)


5694
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 learn 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.

Let's jump on the dark side,


Connect your LPC Demo board to your PICKIT 2
Open MicroCode Studio
Make sure you have PIC16F690 selected in the PIC dropdown list

5695

mister_e
- 22nd June 2011, 23:50
First code example: the Blinky Led

Copy/paste the following code in Microcode Studio

'-------------------------------< Blinky LED >------------------------------------
'
' File name : BlinkyLed.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) MicroCode Studio
' 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
'
'---------------------------------------------------------------------------------Save the file as BlinkyLed and then press on F10 (Compile & Program) and.... MIIIIIP! Congratulation, you've just experimented your first compilation error!


5696

Now 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, it will compile fine, it will even program the PIC... and... nothing happen.

Why? Use you multimeter and measure the voltage across Vdd & Vss... there's nothing there. That mean the PICKIT 2 is not powering your board. I previously told the pk2cmd allow several parameter, one of those is to power the target board once the PIC is programmed.

Go, open the Readme For PK2CMD.txt file and goto section 9. Command Line Option. There you will find the whole set of available option. The one we want to use right now is the T one.

To add this feature to MicroCode, you can modify the programmer.ini file in MicroCode Studio folder OR ...


Click on View
Click on Compile And program Options
Click on Programmer tab
Select PICKIT II From the drop list
Click on Edit...
Click on Next
Click on Next Again
In the Parameters section add /T totally at the end of the line
Click on Finished
Click on OK


You're done, press F11 to program your PIC again and... VOILA! Your first led Blink program works.

Time to celebrate!

mister_e
- 22nd June 2011, 23:51
Code Example 2: ADC Reading

Start a new project in Microcode studio then Copy/Paste the following code

'-------------------------------< Blinky LED >------------------------------------
'
' 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) MicroCode Studio
' 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
'
'---------------------------------------------------------------------------------
Save the file as ADCToLed and, again, press F10 and 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. It's up to you

Enjoy, you're now a professional :P

ScaleRobotics
- 23rd June 2011, 02:05
Great job Steve! We needed some beginner tutorials! Will promote these to the wiki area tonight.

Thanks!

Jasonstew
- 23rd June 2011, 04:18
This was extremely helpful. I couldn't figure out how to comment the .inc file, so i just edited it. It works great

mister_e
- 23rd June 2011, 05:12
Great! I thought this would be great to have somewhere, now we just need to link to it when someone need guidance.

Glad to hear it helped!

You're all welcome!

MPLAB Version's comming too.

Jasonstew
- 23rd June 2011, 05:32
would you recommend any way of understanding the data sheet or where to go from here?

mister_e
- 23rd June 2011, 08:40
You're out of luck, I wish I could help but, as far as I'm aware of, there's no magical tricks for that. Also I'm a pretty bad teacher... sorry :(

However, you just need to start with small chunks. Do some blinky, chaser stuff, read some push buttons, add the ADC and so forth.

Still, there's a heap of cool code example in here, copy/paste them, modify them and learn from it.

Datasheet may looks hard, but again, practice makes...better.

Should you need ANY help, don't hesitate to start a new thread. We're here to help!

Good luck!

david1800
- 25th June 2011, 04:40
Thanks Steve! I wish this had been available when I started, it can be quite daunting for someone with little relevant experience to get going.

Regards

David

mister_e
- 25th June 2011, 05:03
Thanks David,
Yup, you're right! Since awhile I thought about a thread like that.

Hope this will help a lot of newcomers.

Cheers!