PDA

View Full Version : Beginner- blinking led's on pickit 2 starter kit



Jasonstew
- 20th June 2011, 15:55
I just got a new pickit2 starter kit with a pic16f690 low pin count demo. I had trouble compiling in c, so i decided to switch to basic. How can I learn how to make the led's blink and then i would like to move on to digital and analog inputs?

Heckler
- 20th June 2011, 16:16
Hey Jason,
Wellcom to the forum...

A little more detail about your background in programming and PIC's might help the forum members to know how to help you.

Do you have a PIC Basic Compiler yet?
If not... there is a Demo version available over on the http://melabs.com/pbpdemo.htm web site. I think the Pic Basic Pro version is the way to go if you plan on doing a lot of projects with the PIC microcontrollers (although it is around $250 US) but it's worth it in the long run.

What IDE (integrated development environment) are you using?? I use MicroCodeStudio... which is available free.

I can post a few of my starting programs to help you get started. But I need to know what you have on hand and your background.

For me, starting out, the hardest thing was to understand the configuration registers for the various periperals on the micro's. They can be difficult to get your mind around. But, they make the PIC's very flexible.

I have a couple of LED blinker programs that I can post.

Do you have some example code that you have allready been working on??

were here to help

Jasonstew
- 20th June 2011, 16:49
thank you for all of the help. I am currently using mplab, but i could switch to MicroCode Studio. I have learned Java , and a little bit of Basic and C in the past. My final goal is to be able to create a pic that can read multiple low voltage digital and analog 4-20ma sensors, communicate with me remotely, and if possible control servos as a response to the readings from the analog and digital inputs. To achieve this I originally used National Instrument's DAQ tools and Labview, but I feel that PIC's are more flexible and cost-effective. Thanks again for all of your help.

Heckler
- 21st June 2011, 03:57
Jason,

Well... Here is a small program that will blink the LED's... You should be able to copy and paste...
It's about as simple as it gets :)

I just tested it on my LowPinCount board with a 16F690.

One of the hardest things for me was to understand how to READ the datasheet for what ever PIC I was using. GET IT! and have it handy as a .pdf to refer to. You might also print out certain pages that you refer to alot.

If you run into specific problems... post back here. There are MANY good programmers here that can jump into help.

OH yea, Did I mention MAKE THE DATASHEET YOUR FRIEND.

I am not repeating that to be obnoxious!!

It took me a while and several bouts of knocking my head against the wall. Once you understand that in order to correctly initialize whatever peripheral (adc, pwm, output, input, timer, interupt, etc.) you are wanting to use... you MUST refer to the datasheet for the specific registers, (adcon0, cmcon, TRISA. ETC) and which bits do what. Also each PIN of the PIC has a page in the datasheet that usually details what registers affect that pin and its function.

you didn't mention if you had the PicBasic compiler or not... so I hope this is useful.



' 16F690
'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2007 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 11/11/2007 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
I Var Byte
TRISC = %11110000
ansel = 0
Start:
For I = 0 To 3
PortC.0[I] = 1
Pause 150
Next I
For I=3 To 0 Step -1
PortC.0[I] = 0
Pause 150
Next I
Goto Start


I hope this helps

Jasonstew
- 21st June 2011, 17:22
Thank you very much. I am currently running the demo version PICBasic Pro compiler, but whenever I program the 16f690 with the PICKit 2, it compiles and programs correctly, but nothing happens. Am I supposed to be using another source of power or is the pickit 2 sufficient. The assembly version that they give me works fine without any other source of power.

mister_e
- 21st June 2011, 17:33
When you program the PIC, are you using the PICKIT 2 standalone software? There's some option allowing to supply the target board, and release MCLR pin. worth a look.

Also, you want to set the configuration fuses to use the internal OSC. From what I see there's no Crystal on that specific demo board.

Jasonstew
- 21st June 2011, 19:09
I am using the mplab software. I see that in assembly and c you can use the _config command to configure the chip. How do you do it in Basic?

Jasonstew
- 21st June 2011, 19:10
and which option for the internal OSC? I think there are 2 options: INTOSC and INTOSCIO.

johnbond
- 21st June 2011, 19:19
Hi Jason

Check the low pin count demo board for power. With MPLAB, it gets its power from the PICKit 2 dongle. With PICBasic, you seem to have to feed it. It eats only 5 volts! (the two holes at the top left of the board are suitably marked...) :)

Kind regards from Kwa Dukuza, lost somewhere in darkest Africa
John Bond

Heckler
- 21st June 2011, 19:49
Jason...
Look at the screen shot from my pickit programmer... notice each of the red ovals... this is where you can turn on/off DC pwr from the pickit to your LPC demo bd. You can also adjust the voltage supplied by the pickit2 to see how your hardware behaves under different voltages. also you can click the "configuration" oval and manually set the configuration fuses. The fuses can ONLY be set at time of programming. They can also be configured in your program to be set at the time of programming. I will try and upload a program tonight that shows how to set these fuses in your program.

If the pickit does not auto detect the type of PIC you are using you can set that from one of the ovals... I run my pickit programmer from two different computers, one seems to auto detect the pic model. the other seems I have to set it through the dropdown in the pickit program window. That may be due to different versions of pickit software.

Be sure you understand the difference between the configuration "fuses" or register. and the many other configuration registers that can be changed on the fly by your program.

5677

You might also want to try MicroCodeStudio... I like it for its simplicity.

dwight

Jasonstew
- 21st June 2011, 20:11
how do you import basic code to the pickit software. I only see how to import hex code. MPlab seem much easier than that. Also I would seem to have to buy the microstudio plus software version because the normal version does not support 16f690. Could you tell me the correct configuration options to select in MPlab and how to select using the Pickit2 as the power source in MPlab.

Heckler
- 21st June 2011, 20:31
I have never used MPlab... BUT I do know that the free version of MicroCodeStudio does support the 16F690.

You are correct that the pickit software imports HEX code. So you write your software in the MCS environment. Compile using PBP and then import the hex into the pickit software and program your pic.

I (and others) can help you get MCS (free version) up and running, not much to it really. Other than pointing it to PBP for the compiler.

I then just goto the pickit window and manually import the hex file and program.

That is about all I know to help you with. No experience with MPlab here.

---------------------------

Below is a snippit of code showing how to set the configuration fuses from within your program. look toward the top for the line that begins with @.

Also lower down you will see several configuration registers and some typical settings with comments to explain.




' 16F690
'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2007 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 11/25/2007 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
@ device pic16F690, intrc_osc_noclkout, BOD_OFF, PWRT_OFF, wdt_off, mclr_off,protect_off

INCLUDE

"Temp_Convert_CtoFonly.pbp"


' -----[ I/O Definitions ]------------------------------------------


' DEFINE DEBUG_REGG PORTA 'set debug port to porta

' DEFINE DEBUG_BIT 0 'use pin a0 of porta for debug

' DEFINE DEBUG_BAUD 2400 'set baud rate to 2400
' DEFINE DEBUG_MODE 0 'communicate in true mode
' DEFINE DEBUGIN_BIT 1 'use pin a0 of porta for debug
' -----[ Variables ]------------------------------------------------



x88 VAR BYTE[9] 'array to hold the 8 column bytes



msg VAR BYTE[30] 'array to hold current message
col VAR BYTE 'byte to hold fetched coulmn
row VAR BYTE 'byte to hold current row being scanned via walking "1"

r VAR BYTE 'row counter
i VAR BYTE 'col counter
char VAR BYTE 'current character to fetch
view VAR BYTE 'delay to allow eye to see
ltr VAR BYTE 'number of letters in current message
pos VAR BYTE 'scrolling column counter
len VAR BYTE 'length of current char or msg being scrolled
now VAR BYTE 'holds current position for storing data to matrix
pix VAR BYTE
done VAR BYTE
car VAR BYTE ' holds position of car, "0" is the car
crash VAR BYTE
speed VAR BYTE ' scrolling speed, lower is faster
BtnTmr VAR BYTE 'counter for decreminging
LOn VAR WORD 'time for leds to be on
LOff VAR WORD 'time for leds to be off
wface VAR BYTE
mode VAR BYTE
C VAR WORD ; Celsius also used as Car race Lap counter

F VAR WORD ; Fahrenheit


'-------------[variables and constants for temperature routine]--------------
DS18B20_9bit CON %00011111 ' set resolution on DS18B20 93.75ms, 0.5°C
Comm_Pin VAR PortA.4 ' One-wire Data-Pin "DQ" on PortA.4
Busy VAR BIT ' Busy Status-Bit
Raw VAR WORD ' RAW Temperature readings
TempF VAR WORD ' Temp in deg F
Cold_Bit VAR Raw.BIT11' Sign-Bit for +/- Temp. 1 = Below 0 deg C
Real_Cold CON 1 ' Define Real_Cold = 1
Sign VAR BYTE ' +/- sign for temp display



' -----[ Initialization ]--------------------------------------------



Reset:


TrisA = %00011000

'Port A3,A4 inupts rest outputs (A3 cannot output)

TrisB = %00000000 'Port B all outputs
TrisC = %00000000 'Port C all outputs
ansel = 0 'and turn off analog
AnselH = 0 'turn of rest of analogs
CM1CON0 = 0 'turn off comparators
CM2CON0 = 0 'turn off comparators
SSPCON.BIT5 = 0 ' disable serial port, pins are I/O
OPTION_REG = %10000000 '1 turn off weak pull ups
INTCON = %00000000


' -----[starting Constants ]------------------------------------------------

mister_e
- 22nd June 2011, 02:56
Everything can be done within MPLAB (Code, Programmin etc). You just need to start a new project, select the right toolsuite, and start from there. not sure where you stuck with it.

If PIbasic toolsuite is properly installed you should be able to do everything in a snap


About the configuration fuses, check the one used in the assembler code, and check the FAQ. There's a whole thread explaining how to set them. Within MPLAB, the @ Device line shouldn't work, use MPASM @ _CONFIG, still explained in the FAQ.

Bellow, a compilation of few handy things
http://www.picbasic.co.uk/forum/showthread.php?t=11116

HTH

Jasonstew
- 22nd June 2011, 03:03
how do you compile to hex with microstudio and would you recommend i just use another power supply instead of the pickit 2? thanks again

mister_e
- 22nd June 2011, 03:32
I ALWAYS recommend to use external power supply, just for safety sake.

Since I don't remember of any, I think I should do some step-by-step guide for beginners. 1 for Microcode studio, and one for MPLAB using PBPDemo.

Hows that sound?

Heckler
- 22nd June 2011, 03:41
Hey Jason,

Firstly, I can't say wether MPLab is more or less capable than MCS. It's just the only one I know and use.

In order to compile in MCS I believe you only need to go to VIEW, COMPILE AND PROGRAM OPTIONS and tell MCS where to find PBP.
(Mine is just C:\pbp)

For Me I just use the compile button on the upper left of the mcs window (below NEW) I have not bothered to get "Compile Progarm" to work.

Below is a post I made in another thread that might help...




I use MicroCode Studio...
I just compile the program in MCS, then move over to the pickit2 window and do a "File", "Import Hex" and go to the directory where MCS put the .hex file and load it and "Write" to the PIC. Mine are under C:\Program Files\Mecanique\MCS.

Just open up MCS and Pickit2 and leave them open for the duration of your programming / editing session.

Once you locat the directory that MCS stores the .hex file, Pickit2 will find it the next time you compile. If you are testing the same program repeatedly you will find the .hex file you are working on, listed as a recent file, under the "File" drop down in the Pickit2 window. You can just double click on the existing link under the "File" menu and you are ready to "Write" to your PIC.

I have not bothered to figure out how to get MCS to automatically compile and burn.

Sometimes I will manually modify the PIC "fuses" in the Pickit2 window just before burning. I'm not that good at setting the "fuses" correctly in my program yet. (look below the "Help" drop down menu, in the Pickit2 window, and you will see a "Configuration" link which will allow you to change the "fuse" bits.

Hope this addresses your question
good luck
Dwight

Personally I think the PICkit2 is ideal to supply power for your test circut as long as you arent pulling too much current.

I can, literally, make a change to a program, compile, import to pickit and program a PIC to test the code in less than 15 seconds. I just leave the pickit window open along side of the MCS window.

NOTE: here is a GOTCHA to watch out for with the LPC demo board...
If you are trying to read the onboard POT into RA0 and send the results to a DEBUG window... there is a confilict as RA0 is also used for DEBUG... so what I did is to cut the trace from the POT to RA0 and put a loose wire instead. That way I can take the output from the POT to any of the PINS that I want to, not just RA0. (See the LPC schematic in its documentation. (this one drove me nuts for a while)

Hope you are making progress :)

Archangel
- 22nd June 2011, 15:50
how do you compile to hex with microstudio and would you recommend i just use another power supply instead of the pickit 2? thanks again
PICKit2 works great with MCS, you would start the pickit software and start MCS, set the PICKit2 to auto import the hex file named for your project, then each time you recompile in MCS it will automatically reload the hex and reprogram the PIC. I use those Demo boards sometimes and they work well within their limitations. I doubt the Demo version of PBP will allow you to do very much in the way of programming a pic unless you are very clever to optimize your code. BTW for the demo board use the PICKit2 power is adequate, it is short tolerant in that it will crash and protect your USB, at least it does mine. It allows you to select the PIC's operating voltage too.

Jasonstew
- 22nd June 2011, 15:52
I get an illegal opcode error when I run that configuration command and the 16f690 device in microstudio dissapeared

Archangel
- 22nd June 2011, 15:56
I get an illegal opcode error when I run that configuration command and the 16f690 device in microstudio dissapeared Which configuration command do you refer to? Which opcode ?

Jasonstew
- 22nd June 2011, 16:56
the @ device pic16F690, intrc_osc_noclkout, BOD_OFF, PWRT_OFF, wdt_off, mclr_off,protect_off
command and the pic16F690 opcode error

ScaleRobotics
- 22nd June 2011, 18:01
Here is some info on how to set your configuration fuses. http://www.picbasic.co.uk/forum/content.php?r=157-Presetting-Configuration-Fuses-%28PIC-Defines%29-into-your-Program They are case sensitive, so changing them all to capitals will help. Here is one way to do it, if you wanted it in your code space.


@ __CONFIG _INTRC_OSC_NOCLKOUT & _BOD_OFF & _PWRTE_OFF & _WDT_OFF & _MCLRE_OFF & _CP_OFFYou may notice that there is no mclr_off or protect_off options in the P16F690.INC in the MPLAB directory. That is causing some of the errors.

Jasonstew
- 22nd June 2011, 20:11
thanks for your help, but now I am getting this error ERROR: Unable to execute mpasmwin.Error[118] C:\TEST.ASM 52 : Overwriting previous address contents (2007)
any ideas?

mackrackit
- 22nd June 2011, 21:32
When setting the configs in code space you need to comment out the configs lines in the chip's *.inc file.
http://www.picbasic.co.uk/forum/showthread.php?t=543

mister_e
- 22nd June 2011, 22:54
Check this out
http://www.picbasic.co.uk/forum/showthread.php?t=15062

I'm working on one for MPLAB.