Author Topic: Help! How to program PIC microcontrollers using MPlab X IDE?  (Read 4475 times)

0 Members and 1 Guest are viewing this topic.

Online BrianHG

  • Frequent Contributor
  • **
  • Posts: 899
  • Country: ca
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #25 on: March 21, 2017, 06:14:12 AM »
Maybe... now,,, I had this problem once last year...

  When I've done a auto build & program in MPLabxIDE, I accidentally disconnected the PICkit3 from my 6 pin sip header in the middle of programming.  MPlab did report an error, but after reconnecting the pickit3 and trying to program again, it failed to see my Pickit3, even after closing MPlab and disconnecting the Pickit USB cable and reconnecting.  The way to get it working again was I found in my task manager/processes a second microchip Mplab task frozen there in the background.  After killing it, everything worked again.  Now, I've accidentally disconnected my programmer a number of times without it affecting MPlab, this 1 time was a fluke chance, but if you have another frozen microchip programmer process, which looked like you closed it, but it is still running as a frozen service in the background, it needs to be 'End Process' before the programmer will work again, or, just do a reboot to clean everything.

Note that I was using Win 7 professional 64bit.
« Last Edit: March 21, 2017, 06:20:50 AM by BrianHG »
__________
BrianHG.
 
The following users thanked this post: ali6x944

Offline ali6x944

  • Frequent Contributor
  • **
  • Posts: 501
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #26 on: March 21, 2017, 02:55:05 PM »
Is the operating system important? And could it be a part of the problem?
 

Offline ali6x944

  • Frequent Contributor
  • **
  • Posts: 501
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #27 on: March 22, 2017, 05:08:57 AM »
I think i have uploaded the code to the pic...
but the PIC is not running properly, the output pin RB0 is not flashing at all and the oscillator is giving a sine wave. see pictures:
 

Offline ali6x944

  • Frequent Contributor
  • **
  • Posts: 501
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #28 on: March 22, 2017, 05:12:29 AM »
also to mention that my pickit3 says it is supplying 5v, but testing it with my multimeter it shows around 4.77v to 4.65v
 

Offline Luminax

  • Regular Contributor
  • *
  • Posts: 159
  • Country: my
    • Electronesk
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #29 on: March 22, 2017, 02:04:05 PM »
Can you provide a capture of your breadboard/perfboard/stripboard/etc?

Also provide screenshot of the Project configuration?

A note to point out, is that it is better to disconnect the PICKIT3 after programming as it does draw some current, and in some cases (like LCD driving) can drive your circuit a bit bonker
Jack of all trade - Master of some... I hope...
 
The following users thanked this post: ali6x944

Offline ali6x944

  • Frequent Contributor
  • **
  • Posts: 501
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #30 on: March 23, 2017, 11:05:08 PM »
Can you provide a capture of your breadboard/perfboard/stripboard/etc?

Also provide screenshot of the Project configuration?

A note to point out, is that it is better to disconnect the PICKIT3 after programming as it does draw some current, and in some cases (like LCD driving) can drive your circuit a bit bonker
Do you mean the configuration bits?
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 4832
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #31 on: March 23, 2017, 11:32:26 PM »
Its normal for a crystal oscillator to produce fairly close to a sine wave.  Its also normal for a PICkit 3 to be unable to reach 5V out - it can only do 5V out if the USB Vbus is at its upper limit of 5.25V, as it doesn't contain a Vdd boost circuit.

Please stop with the <expletive> screenshots!
For source code or error messages from the Output window, its far easier for us if you paste them in, in code tags (# button in full editor).

In this case we need to see all of your main C file, (assuming that's the one with the #pragma config block in it).

Alternatively attach the source file(s), or if you want us to be able to build your project, maybe because you are struggling with a build error, package it as a zip file (from the project menu) and attach that.

For dialogs, project properties etc. that cant be copied as text, screenshots are unavoidable, but please crop them to remove unwanted background windows etc.
« Last Edit: March 23, 2017, 11:35:36 PM by Ian.M »
 
The following users thanked this post: ali6x944

Offline Luminax

  • Regular Contributor
  • *
  • Posts: 159
  • Country: my
    • Electronesk
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #32 on: March 24, 2017, 12:35:24 PM »
The project dashboard, and the project configuration windows, looks like the two screenshots I attached.
Also, yes, we also need to take a peek at your source codes
Jack of all trade - Master of some... I hope...
 
The following users thanked this post: ali6x944

Offline ali6x944

  • Frequent Contributor
  • **
  • Posts: 501
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #33 on: March 24, 2017, 09:08:45 PM »
Here is my breadboard view:
https://drive.google.com/file/d/0B5vW-k7HbsL4c0hHVk1xYng1VzA/view?usp=drivesdk
also sorry for the screenshots I will post the code in a moment.
 

Offline ali6x944

  • Frequent Contributor
  • **
  • Posts: 501
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #34 on: March 24, 2017, 09:22:02 PM »
Code: [Select]
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF         // FLASH Program Memory Code Protection bit (Code protection off)
#pragma config BOREN = ON      // Brown-out Reset Enable bit (BOR disabled)
#pragma config FOSC = LP
#define _XTAL_FREQ 32768
// #pragma configuration statements should precede project file includes.

#include <xc.h>
#include <stdio.h>


int main()
{
    TRISB = 0xFF;               // PortB is an output
    TRISBbits.TRISB0 = 0;

    while(1)
    {
        PORTBbits.RB0 = 1;
        __delay_ms(500);
        PORTBbits.RB0 = 0;
        __delay_ms(500);
    }

    return 0;
}
this is the code in the main.c file
« Last Edit: March 24, 2017, 09:29:07 PM by ali6x944 »
 

Offline ali6x944

  • Frequent Contributor
  • **
  • Posts: 501
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #35 on: March 24, 2017, 09:30:51 PM »
Code: [Select]
#include <xc.h>
#pragma config FOSC = LP        // Oscillator Selection bits (LP oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF         // FLASH Program Memory Code Protection bit (Code protection off)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#define _XTAL_FREQ 32768
this code is in the config.h file
 

Offline ali6x944

  • Frequent Contributor
  • **
  • Posts: 501
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #36 on: March 24, 2017, 09:44:12 PM »
project config window:
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 4832
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #37 on: March 24, 2017, 09:52:22 PM »
The code in main.c looks OK apart from the misleading comment when you set TRISB to 0xFF.  It should be running and toggling RB0.

config.h isn't doing anything as main.c doesn't include it.  It is a bad idea to put the #pragma config block in a header as it should never be in or included from more than one .c file.  If you want to remove the config block from main.c, save the code generated by the config wizard (or cut/paste it from main.c) as config.c  and add it to the project.

I would suggest enabling PWRTE as it extends power-on reset allowing a bit more time for Vdd to stabilise, and can help improve reliability. Also for the same reasons, if Vdd is guaranteed to be above the BOR threshold during normal running, enable BOREN, to hold the PIC in reset till Vdd is well above the minimum operating voltage.

Sorry cant see the breadboard, you need to either use a PUBLIC
 photosharing site, or drop the resolution and increase the JPEG compression to get it under 500K so you can upload it here.  A resolution of approx 1500 to 2000 pix on the long side is generally acceptable if you started with a good photo on a hi-res camera.  Try to pick a resolution that is an integer factor of the camera resolution for optimal image quality.  If you don't have a photo editor with a save for web function, and are using Windows, consider the free IrfanView as its JPEG save options include the ability to limit the image file size.

Attaching the image here is vastly preferable as it preserves the topic for future readers as linked images tend to vanish after a few years because  external photo sites may go down, break links or you may simply close your account.

« Last Edit: March 25, 2017, 02:56:44 AM by Ian.M »
 
The following users thanked this post: ali6x944

Online BrianHG

  • Frequent Contributor
  • **
  • Posts: 899
  • Country: ca
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #38 on: March 25, 2017, 02:45:27 AM »
You forgot to change PortB0 analog select bit.  By default it is in analog mode.  The PIC is made like this just in case you have analog signals feeding some IOs during powerup.

Place this before the TRIS...
ANSELB = 0;  // make all of port B into digital IOs

__________
BrianHG.
 
The following users thanked this post: ali6x944

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 4832
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #39 on: March 25, 2017, 02:55:51 AM »
True, but that shouldn't stop RB0 toggling as its alternately being set and cleared by BSF and BCF instructions.  However if any other analog capable pins on port B were outputs,, without clearing ANSELB, they'd be unexpectedly reset to 0 with every transition of RB0.
 
The following users thanked this post: ali6x944

Online BrianHG

  • Frequent Contributor
  • **
  • Posts: 899
  • Country: ca
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #40 on: March 25, 2017, 03:28:29 AM »
True, but that shouldn't stop RB0 toggling as its alternately being set and cleared by BSF and BCF instructions.  However if any other analog capable pins on port B were outputs,, without clearing ANSELB, they'd be unexpectedly reset to 0 with every transition of RB0.

Funny, on my last project, where I swapped an ADC input between digital and analog, and do to a bug in my code, after leaving the input as an analog, it ignored any tris setting & bit setting of the IO port.  It had to be configured as a digital IO before it would output data and go high and low.

I was using a PIC18F46K22, MPLAB & XC8 compiler.

__________
BrianHG.
 
The following users thanked this post: ali6x944

Offline ali6x944

  • Frequent Contributor
  • **
  • Posts: 501
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #41 on: March 26, 2017, 01:39:22 AM »
The code in main.c looks OK apart from the misleading comment when you set TRISB to 0xFF.  It should be running and toggling RB0.

config.h isn't doing anything as main.c doesn't include it.  It is a bad idea to put the #pragma config block in a header as it should never be in or included from more than one .c file.  If you want to remove the config block from main.c, save the code generated by the config wizard (or cut/paste it from main.c) as config.c  and add it to the project.

I would suggest enabling PWRTE as it extends power-on reset allowing a bit more time for Vdd to stabilise, and can help improve reliability. Also for the same reasons, if Vdd is guaranteed to be above the BOR threshold during normal running, enable BOREN, to hold the PIC in reset till Vdd is well above the minimum operating voltage.

Sorry cant see the breadboard, you need to either use a PUBLIC
 photosharing site, or drop the resolution and increase the JPEG compression to get it under 500K so you can upload it here.  A resolution of approx 1500 to 2000 pix on the long side is generally acceptable if you started with a good photo on a hi-res camera.  Try to pick a resolution that is an integer factor of the camera resolution for optimal image quality.  If you don't have a photo editor with a save for web function, and are using Windows, consider the free IrfanView as its JPEG save options include the ability to limit the image file size.

Attaching the image here is vastly preferable as it preserves the topic for future readers as linked images tend to vanish after a few years because  external photo sites may go down, break links or you may simply close your account.


sorry for that...
here is the breadboard view:
https://drive.google.com/file/d/0B5vW-k7HbsL4c0hHVk1xYng1VzA/view?usp=sharing

also here is the main.c code:
Code: [Select]
#include "config.h"
#include <xc.h>
#include <stdio.h>


int main()
{
   
    TRISB = 0xFF;               // PortB is an output
    TRISBbits.TRISB0 = 0;

    while(1)
    {
        PORTBbits.RB0 = 1;
        __delay_ms(500);
        PORTBbits.RB0 = 0;
        __delay_ms(500);
    }

    return 0;
}
 

Offline ali6x944

  • Frequent Contributor
  • **
  • Posts: 501
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #42 on: March 26, 2017, 01:50:20 AM »
code in config.h :
Code: [Select]
#include <xc.h>
#pragma config FOSC = LP        // Oscillator Selection bits (LP oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON      // Power-up Timer Enable bit (PWRT enabled)
#pragma config CP = OFF         // FLASH Program Memory Code Protection bit (Code protection off)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#define _XTAL_FREQ 32768
 

Offline ali6x944

  • Frequent Contributor
  • **
  • Posts: 501
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #43 on: March 26, 2017, 01:52:20 AM »
You forgot to change PortB0 analog select bit.  By default it is in analog mode.  The PIC is made like this just in case you have analog signals feeding some IOs during powerup.

Place this before the TRIS...
ANSELB = 0;  // make all of port B into digital IOs
i did, and it displayed an error icon...
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 4832
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #44 on: March 26, 2017, 02:50:00 AM »
Sorry about that - Brian gave you bogus info and I didn't notice.  On a PIC16F77, port B has NO analog capable pins and its ADC module is far too old to have a separate pin configuration for each port, or even to have individual pin configuration bits at all.   To turn off analog mode for all pins on an 'F77', use:
Code: [Select]
ADCON1bits.PCFG=0b111;See datasheet REGISTER 11-2: ADCON1 REGISTER for details.  However it wont make any difference to whether or not your code works, as its only important if you are trying to use ports A or E.

Please post a photo of your board here so we can see how you are testing if RB0 toggles.
Also it would be worth trying a different pin in case you've blown RB0.
 
The following users thanked this post: ali6x944

Online BrianHG

  • Frequent Contributor
  • **
  • Posts: 899
  • Country: ca
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #45 on: March 26, 2017, 03:10:59 AM »
 :palm: Sorry about that error, I saw in 'Luminax' post's screenshot, that a newer device 18F45K22 was selected in the compiler.  My fault, I should have re-checked the original OP for which PIC was being used.  :palm:
__________
BrianHG.
 
The following users thanked this post: ali6x944

Offline ali6x944

  • Frequent Contributor
  • **
  • Posts: 501
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #46 on: March 26, 2017, 02:34:43 PM »
:palm: Sorry about that error, I saw in 'Luminax' post's screenshot, that a newer device 18F45K22 was selected in the compiler.  My fault, I should have re-checked the original OP for which PIC was being used.  :palm:
No problem m8.
 

Offline ali6x944

  • Frequent Contributor
  • **
  • Posts: 501
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #47 on: March 26, 2017, 02:38:18 PM »
Sorry about that - Brian gave you bogus info and I didn't notice.  On a PIC16F77, port B has NO analog capable pins and its ADC module is far too old to have a separate pin configuration for each port, or even to have individual pin configuration bits at all.   To turn off analog mode for all pins on an 'F77', use:
Code: [Select]
ADCON1bits.PCFG=0b111;See datasheet REGISTER 11-2: ADCON1 REGISTER for details.  However it wont make any difference to whether or not your code works, as its only important if you are trying to use ports A or E.

Please post a photo of your board here so we can see how you are testing if RB0 toggles.
Also it would be worth trying a different pin in case you've blown RB0.
I will try to change the pin today and see what happens...
Also what dose this code do I didn't quite get it:
Code: [Select]
ADCON1bits.PCFG=0b111
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 4832
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #48 on: March 26, 2017, 04:35:31 PM »
It turns off analog mode for all analog capable pins (ADC inputs).  While in analog mode, any attempt to read the pin as a digital input returns 0, which messes up bit output instructions on the same port.
 
The following users thanked this post: ali6x944

Online lwatts666

  • Supporter
  • ****
  • Posts: 42
  • Country: au
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #49 on: March 26, 2017, 10:02:33 PM »
I think you have a few problem with power connections on your breadboard.
1) Vdd should connect to pins 11 and 32. You have only connected to pin 32.
2) Vss should connect to pins 12 and 31. The photo shows it connected to pin 33.
3) The right hand vertical ground bus appears to be floating - not connected to ground.
 
The following users thanked this post: ali6x944


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

http://opalkelly.com/