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

0 Members and 1 Guest are viewing this topic.

Offline BrianHG

  • Super Contributor
  • ***
  • Posts: 7725
  • Country: ca
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #25 on: March 20, 2017, 07:14:12 pm »
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 20, 2017, 07:20:50 pm by BrianHG »
 
The following users thanked this post: ali6x944

Offline ali6x944Topic starter

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

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #27 on: March 21, 2017, 06:08:57 pm »
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 ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #28 on: March 21, 2017, 06:12:29 pm »
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, 03:04:05 am »
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 ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #30 on: March 23, 2017, 12: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: 12852
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #31 on: March 23, 2017, 12: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, 12: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, 01:35:24 am »
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 ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #33 on: March 24, 2017, 10:08:45 am »
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 ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #34 on: March 24, 2017, 10:22:02 am »
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, 10:29:07 am by ali6x944 »
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #35 on: March 24, 2017, 10:30:51 am »
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 ali6x944Topic starter

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

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12852
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #37 on: March 24, 2017, 10:52: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.

« Last Edit: March 24, 2017, 03:56:44 pm by Ian.M »
 
The following users thanked this post: ali6x944

Offline BrianHG

  • Super Contributor
  • ***
  • Posts: 7725
  • Country: ca
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #38 on: March 24, 2017, 03:45:27 pm »
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

 
The following users thanked this post: ali6x944

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12852
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #39 on: March 24, 2017, 03:55:51 pm »
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

Offline BrianHG

  • Super Contributor
  • ***
  • Posts: 7725
  • Country: ca
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #40 on: March 24, 2017, 04:28:29 pm »
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.

 
The following users thanked this post: ali6x944

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #41 on: March 25, 2017, 02:39: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.


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 ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #42 on: March 25, 2017, 02:50:20 pm »
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 ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #43 on: March 25, 2017, 02:52:20 pm »
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: 12852
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #44 on: March 25, 2017, 03:50:00 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.
 
The following users thanked this post: ali6x944

Offline BrianHG

  • Super Contributor
  • ***
  • Posts: 7725
  • Country: ca
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #45 on: March 25, 2017, 04:10:59 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:
 
The following users thanked this post: ali6x944

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #46 on: March 26, 2017, 03:34:43 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:
No problem m8.
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #47 on: March 26, 2017, 03:38:18 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.
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: 12852
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #48 on: March 26, 2017, 05:35:31 am »
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

Offline lwatts666

  • Supporter
  • ****
  • Posts: 76
  • Country: au
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #49 on: March 26, 2017, 11:02:33 am »
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