PDA

View Full Version : Newbie help. PIC18F45k20 pickit debug express



redmobiusv
- 8th August 2011, 16:26
Hi all,

Im a serious newbie at microcontrollers. Trying to get into this as a hobby but I think I am in over my head right now. I need some help.

Im trying to use my PICkit3 programmer with stand along software to program the programs I make in pbp. So far none of my programs work, however if I copy paste a sample program from my pickit3 debug express development kit from microchip. It works fine. The problem is, its in c language and I have no idea what any of it means.
Example: Blink LED

//************************************************** ****************************
//Software License Agreement
//
//The software supplied herewith by Microchip Technology
//Incorporated (the "Company") is intended and supplied to you, the
//Company’s customer, for use solely and exclusively on Microchip
//products. The software is owned by the Company and/or its supplier,
//and is protected under applicable copyright laws. All rights are
//reserved. Any use in violation of the foregoing restrictions may
//subject the user to criminal sanctions under applicable laws, as
//well as to civil liability for the breach of the terms and
//conditions of this license.
//
//THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
//WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
//TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
//PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
//IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
//CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
// ************************************************** *****************
// PIC18F45K20 Starter Kit Lesson 2 - Blink LED
//
// This lesson blinks LED 7 on the demo board by using a delay and
// loop.
//
// ************************************************** *****************
// * See included documentation for Lesson instructions *
// ************************************************** *****************

/** C O N F I G U R A T I O N B I T S ******************************/

#pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF // CONFIG1H
#pragma config PWRT = OFF, BOREN = SBORDIS, BORV = 30 // CONFIG2L
#pragma config WDTEN = OFF, WDTPS = 32768 // CONFIG2H
#pragma config MCLRE = OFF, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC // CONFIG3H
#pragma config STVREN = ON, LVP = OFF, XINST = OFF // CONFIG4L
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF // CONFIG5L
#pragma config CPB = OFF, CPD = OFF // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF // CONFIG7L
#pragma config EBTRB = OFF // CONFIG7H


/** I N C L U D E S **************************************************/
#include "p18f45k20.h"
#include "delays.h"

/** D E C L A R A T I O N S *******************************************/


void main (void)
{

TRISD = 0b01111111; // PORTD bit 7 to output (0) ; bits 6:0 are inputs (1)

while (1)
{
LATDbits.LATD7 = ~LATDbits.LATD7; // toggle LATD

Delay1KTCYx(50); // Delay 50 x 1000 = 50,000 cycles; 200ms @ 1MHz
}

}

but if I program a similar blink sample code from pbp, it doesn't work. Why? Im guessing is has something to do with not defining configurations but I don't know.
Example: Blink LED

' PicBasic program to blink an LED connected to PORTD.0 about once a second

Symbol PORTD = 8 ' Set PORTD address
Symbol TRISD = $88 ' Set TRISD address

Poke TRISD, 0 ' Set PORTD to all output

loop: Poke PORTD, 1 ' Turn on LED connected to PORTD.0
Pause 500 ' Delay for .5 seconds

Poke PORTD, 0 ' Turn off LED connected to PORTD.0
Pause 500 ' Delay for .5 seconds

Goto loop ' Go back to loop and blink LED forever
End

I should also note that I am only using the trial version if PBP right now. I would like to purchase it if I could ever get one of my programs to work. Also, I am using the MicroCode Studio IDE. Please Help. Thanks

HenrikOlsson
- 8th August 2011, 17:41
Hi, welcome to the forum!

There are several thing "going on" here. First of all it would be nice to know what version of PBP you're using - I'm going to assume (since you're using the now reserved word Loop as a label) that it's not the latest PBP3 but some earlier version.

OK, as far as I can see the C-code you posted is setup to use the internal oscillator of the 46K20 device (I suspect this is how the debug express board is layed out) but the default setting when compiling for the 46K20 with PBP is to use an external x-tal and I suspect this is the main reason for it not to work.

The way to rectify this problem depends on the version of PBP you're using and the following holds true for version prior to PBP3.

In your PBP inatallation folder, look for and open the file named 18F45K20.INC (make sure you open the correct file), in it you'll find the following:

NOLIST
ifdef PM_USED
LIST
"Error: PM does not support this device. Use MPASM."
NOLIST
else
LIST
LIST p = 18F45K20, r = dec, w = -311, w = -230, f = inhx32
INCLUDE "P18F45K20.INC" ; MPASM Header
__CONFIG _CONFIG1H, _FOSC_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
__CONFIG _CONFIG2H, _WDTEN_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _CCP2MX_PORTC_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _HFOFST_ON_3H & _MCLRE_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L
NOLIST
endif
LIST
EEPROM_START EQU 0F00000h
BLOCK_SIZE EQU 32
See that first __CONFIG _CONFIG1H, _FOSC_HS_1H..... that I highlighted? That is what is telling the PIC to run with an external x-tal, which I don't think you have on that board. Try changing from _FOSC_HS_1H to _FOSC_INTIO67_1H - make sure you backup the file before making changes to it! The line should look like:

__CONFIG _CONFIG1H, _FOSC_INTIO67_1H & _FCMEN_OFF_1H & _IESO_OFF_1H

Next, there's no need to make aliases to register names in the context you've done (with the SYMBOL "command"), all the register names and their respective adresses are already defined for you in header-files for all the PICs that the compiler supports and they get included automatically when you compile for a particular device (the one you select in the dropdown list within Micro Code Studio in this case).

Also, Peek and Poke is not needed or even recomended with PBP, you can write directly to the registers referring to them by their name, like:


TRISD.0 = 0 ' Make PortD.0 an output

Main:
PortD.0 = 1
Pause 500
PortD.0 = 0
Pause 500
Goto Main

/Henrik.

mister_e
- 8th August 2011, 18:31
http://www.picbasic.co.uk/forum/content.php?r=389-Beginner-Tutorial-How-to-get-started-with-PBPDEMO-MicroCode-Studio-PICKIT-2

redmobiusv
- 8th August 2011, 19:40
Henrik, Thanks replying.



First of all it would be nice to know what version of PBP you're using

Im using the free demo version of PBP right for now.

Tonight I will make the changes to the INC file like you suggested an see if that fixes it.

I have some questions though. Forgive me if they sound beginner-ish..

Im a little vague on understaning what exactly is a INC file. It seems like the books I have read don't go into detail on this very much for the beginner. What I am gathering is that its a file that is attached to the source program when the program is compiled. This tells the compiler which ports to use and how to setup the ports? Is that what your saying when you said?:


Next, there's no need to make aliases to register names in the context you've done (with the SYMBOL "command"), all the register names and their respective adresses are already defined for you in header-files for all the PICs that the compiler supports and they get included automatically when you compile for a particular device (the one you select in the dropdown list within Micro Code Studio in this case).

redmobiusv
- 8th August 2011, 19:42
http://www.picbasic.co.uk/forum/content.php?r=389-Beginner-Tutorial-How-to-get-started-with-PBPDEMO-MicroCode-Studio-PICKIT-2

Thanks mister_e,

This looks like something I can do. I'll look into this. Do you know if it will work for PICkit3 as well?

HenrikOlsson
- 8th August 2011, 21:35
Hi,

Im using the free demo version of PBP right for now.
OK, it doesn't say much to me I'm afraid... Is it a demo version of 2.4, 2.5, 2.6, 3.0 or what? As long as it's not 3.0 editing the .inc file like I showed should work. In 3.0 Melabs have provided us with a better way to handle the CONFIG bits but lets not get into that if you're not using 3.0.


Im a little vague on understaning what exactly is a INC file. It seems like the books I have read don't go into detail on this very much for the beginner. What I am gathering is that its a file that is attached to the source program when the program is compiled.
Yes, that is correct. For each device there's .INC file in the PBP folder, the file for the device you select in the drop-down list in Micro Code Studio gets included in your source file before the actual compilation process starts.

What happens is that MicroCodeStudio passes the name of the device to the compiler thru its -P parameter (see the manual for details on that) and the compiler then "adds" the content of the .INC file to your source file before compiling.


This tells the compiler which ports to use and how to setup the ports?
Pretty much, it doesn't say how to set them up (ie. inputs, outputs, analog, digital etc) but it tells the compiler at which physical adress in memory that each register is (PORTA, TRISA, TMR0, T0CON, ANSEL, RCON, whatever). Pretty much like you did with the SYMBOL statements but it does it for you for ALL the regsiters in the chip so you don't have to dig that deep in the datasheet. Small chips might have a dozen or so register, large chips have hundreds of registers.

Actually, if you look in the 18F45K20.INC file in the PBP folder you'll see there's another include statement in this file, namely INLCUDE P18F45K20.INC which is located in the MPASM directory. It's in this file that the actual names of the registers and their adresses are located.

In practice it means that you don't have to know the physical adress of the register you want to access, you just need to know its name. The rest is handled for you behind the scenes. So, except for editing the CONFIG bits you rarely need to worry about these files.

/Henrik.

redmobiusv
- 8th August 2011, 22:36
Ahhh, this clears up some confusion. Now I think I fiqured out why I could never understand MPLAB---adding INC and HEADER files. All the beginner tutorials show you how to get your first "Hello World" up and going but doesn't really explain how it all works and why your doing the things your doing. Like:

Open this file, click here, click here, look at datasheet, click here, now click compile then program....viola, your first program yaa!!!!!

It left me scratching my head wondering if I needed to add any INC files to my own program? If so then which one? Theres tons of them.. uhmm.... OWNPRG.INC J/K:)

Thanks your help. I'll give these suggestions a go and see how it all turns out.

Thanks again

redmobiusv
- 9th August 2011, 17:29
Hi,

Is it a demo version of 2.4, 2.5, 2.6, 3.0 or what? As long as it's not 3.0 editing the .inc file like I showed should work. In 3.0 Melabs have provided us with a better way to handle the CONFIG bits but lets not get into that if you're not using 3.0.

/Henrik.

Im using PBP3.0 the latest release.

I still ween into the INC file for PIC18f45k20 and made the changes like you stated, however it still didn't work. I tryed some other PBP example codes that came with the installation files and those didn't work either so I feel like theres something else in the INC file that needs to be changed because as I said before, all the hex example codes that are for the pic18f45k20 debug kit work fine. Only the PBP example ones don't. I scanned over the datasheet last night to try and see if there was any other config bits that looked like they might need to be changed but alot of them are over my head right now and I don't really understand them. Got any other suggestions?

Thanks

mister_e
- 9th August 2011, 17:36
with PBP3 you no longer need to edit the .INC files. you need to use the new method stated in the migration document.(#CONFIG, #ENDCONFIG)

http://www.pbp3.com/downloads/Migrating%20existing%20programs%20to%20PBP3.pdf

mister_e
- 9th August 2011, 17:57
Thanks mister_e,

This looks like something I can do. I'll look into this. Do you know if it will work for PICkit3 as well?
It should be really close. I don't have PICKIT 3 myself, maybe It's time for me to grab one of those translucent red box just to sit aside the other dust-gathering device i have on my shelf and bench :D

ScaleRobotics
- 9th August 2011, 19:32
Normnet has the settings information for the Pickit3 here: http://www.picbasic.co.uk/forum/showthread.php?t=12108&p=80537#post80537

If you are powering the device from the pickit3, then you can do something like this:
-P$target-device$ -F$hex-filename$ -E -M -L /V3.250 ;(powers at 3.25 volts)

redmobiusv
- 9th August 2011, 22:36
with PBP3 you no longer need to edit the .INC files. you need to use the new method stated in the migration document.(#CONFIG, #ENDCONFIG)



What is the configuration block. Is that a insert of the program code?

For example

;***************header

#CONFIG
__config _XT_OSC & _WDT_ON & _LVP_OFF & _CP_OFF
#ENDCONFIG

Main:

bla bla bla bla bla

Goto Main
End

????

ScaleRobotics
- 9th August 2011, 23:02
Yes, that's how you do it, as long as it's indented. Looks like the forum takes out the formatting.... so you probably had it perfect to begin with...


#CONFIG
__config _XT_OSC & _WDT_ON & _LVP_OFF & _CP_OFF
#ENDCONFIG
But the rest is perfect!

Except the 45K20 will want it in this format:


#CONFIG
__CONFIG _CONFIG1H, _FOSC_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
__CONFIG _CONFIG2H, _WDTEN_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _CCP2MX_PORTC_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _HFOFST_ON_3H & _MCLRE_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L
#ENDCONFIG

mister_e
- 10th August 2011, 00:00
OR anything else list in the p18F45k20.inc file with the new CONFIG directive


#CONFIG
CONFIG FOSC = INTIO7, FCMEN = OFF, IESO = OFF, PWRT = OFF, BOREN = OFF
CONFIG BORV = 18, WDTEN = OFF, WDTPS = 1, MCLRE = ON, HFOFST = ON
CONFIG LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC, STVREN = OFF
CONFIG LVP = OFF, XINST = OFF, CP0 = OFF, CP1 = OFF, CP2 = OFF
CONFIG CP3 = OFF, CPB = OFF, CPD = OFF, WRT0 = OFF, WRT1 = OFF
CONFIG WRT2 = OFF, WRT3 = OFF, WRTB = OFF, WRTC = OFF, WRTD = OFF
CONFIG EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
CONFIG EBTRB = OFF
#ENDCONFIG

mister_e
- 10th August 2011, 00:50
All config are now also available in
C:\PBP3\DEVICE_REFERENCE
for each device, you have a .INFO file

Slick!

Well, Section 4.9 of the manual will explain...

redmobiusv
- 11th August 2011, 15:49
Just wanted to think you guys for your help. I never did get it to work, however I think I have decided to not purchase the PBP3. My trial is about to expire anyway.

The main reason for not purchasing it is due to the price. I also just learned that the PBP3 gold edition is the only one that will support my pic18f45k20 debug kit and I feel like the gold edition is really more then I need. I think the product is outstanding though, but for a hobbiest, its more then I want to spend. I have some other hobbys that I need to use the money for. I have decided to try and find another solution.

Im sure I can take what Iv learned here and apply it.
Thanks again

ScaleRobotics
- 11th August 2011, 16:43
I don't know if you're up for it, but if you are, let's try one more thing:

Getting the config settings from the manual for your hardware, as you showed in your first example:

/** C O N F I G U R A T I O N B I T S ******************************/

#pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF // CONFIG1H
#pragma config PWRT = OFF, BOREN = SBORDIS, BORV = 30 // CONFIG2L
#pragma config WDTEN = OFF, WDTPS = 32768 // CONFIG2H
#pragma config MCLRE = OFF, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC // CONFIG3H
#pragma config STVREN = ON, LVP = OFF, XINST = OFF // CONFIG4L
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF // CONFIG5L
#pragma config CPB = OFF, CPD = OFF // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF // CONFIG7L
#pragma config EBTRB = OFF // CONFIG7H

We can get those into the format needed by removing the #pragma's. Below is some code that hopefully should blink one of the LEDs



#CONFIG
config FOSC = INTIO67, FCMEN = OFF, IESO = OFF
config PWRT = OFF, BOREN = SBORDIS, BORV = 30
config WDTEN = OFF, WDTPS = 32768
config MCLRE = OFF, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC
config STVREN = ON, LVP = OFF, XINST = OFF
config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
config CPB = OFF, CPD = OFF
config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
config WRTB = OFF, WRTC = OFF, WRTD = OFF
config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
config EBTRB = OFF
#ENDCONFIG

OSCCON.6 = 1 'default at reset for OSCCON is 1 mhz, we are changing it here to 16 mhz
DEFINE OSC 16
TRISD = %00000000 'set LED's to outputs
LED0 var portd.0 'set LED0 as PORTD bit 0

main:
pause 500
toggle LED0
goto main

end

HenrikOlsson
- 11th August 2011, 17:06
Hi,
That TRIS register setting seems backwards to me, at least if the LED indeed is on PortD.0 and not on PortD.7 - in which case the alias is backwards.

/Henrik.

ScaleRobotics
- 11th August 2011, 17:12
Thanks Henrik! I corrected that.

redmobiusv
- 11th August 2011, 17:56
Okay, I'll try that when I get a chance. I'll post the results whenever I get to it. Hopefully tonight.