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

0 Members and 1 Guest are viewing this topic.

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12852
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #50 on: March 26, 2017, 11:23:24 am »
Always connect all power pins  together and all ground pins together with a 0.1uF decoupling cap for each power/ground pair.  That's Vdd and Vss for this PIC as it doesn't have any dedicated power pins for its ADC.

What's that unholy mess of capacitors round the crystal?   Do you have any single caps within 30% of the value you are trying to get to? If so, use them on their own for now.
Due to the sensitivity of the LP oscillator circuit, you should connect the caps as dorectly as possible between the OSC pins and the adjacent Vss pin.  Long wires are NOT a good idea . . . .
 
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 #51 on: March 26, 2017, 03:27:07 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.
Always connect all power pins  together and all ground pins together with a 0.1uF decoupling cap for each power/ground pair.  That's Vdd and Vss for this PIC as it doesn't have any dedicated power pins for its ADC.

What's that unholy mess of capacitors round the crystal?   Do you have any single caps within 30% of the value you are trying to get to? If so, use them on their own for now.
Due to the sensitivity of the LP oscillator circuit, you should connect the caps as dorectly as possible between the OSC pins and the adjacent Vss pin.  Long wires are NOT a good idea . . . .
i suspected this, also I'm using 10pf caps.
i will fix the connections and upload the photos soon...
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #52 on: March 27, 2017, 06:46:01 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.
it worked see code of main.c:
Code: [Select]
#include "config.h"
#include <xc.h>
#include <stdio.h>


int main()
{
    ADCON1bits.PCFG=0b111;
    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;
}
see output:
Code: [Select]
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'C:/Users/hadaw/MPLABXProjects/BlinkLed.X'
make  -f nbproject/Makefile-default.mk dist/default/production/BlinkLed.X.production.hex
make[2]: Entering directory 'C:/Users/hadaw/MPLABXProjects/BlinkLed.X'
"C:\Program Files (x86)\Microchip\xc8\v1.41\bin\xc8.exe" --pass1  --chip=16F77 -Q -G  --double=24 --float=24 --opt=+asm,+asmfile,-speed,+space,-debug,-local --addrqual=ignore --mode=free -P -N255 --warn=-3 --asmlist -DXPRJ_default=default  --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-osccal,-resetbits,-download,-stackcall,+clib   --output=-mcof,+elf:multilocs --stack=compiled:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s"    -obuild/default/production/main.p1  main.c
"C:\Program Files (x86)\Microchip\xc8\v1.41\bin\xc8.exe"  --chip=16F77 -G -mdist/default/production/BlinkLed.X.production.map  --double=24 --float=24 --opt=+asm,+asmfile,-speed,+space,-debug,-local --addrqual=ignore --mode=free -P -N255 --warn=-3 --asmlist -DXPRJ_default=default  --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-osccal,-resetbits,-download,-stackcall,+clib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s"      --memorysummary dist/default/production/memoryfile.xml -odist/default/production/BlinkLed.X.production.elf  build/default/production/main.p1     
Microchip MPLAB XC8 C Compiler (Free Mode) V1.41
Build date: Jan 24 2017
Part Support Version: 1.41
Copyright (C) 2017 Microchip Technology Inc.
License type: Node Configuration

:: warning: (1273) Omniscient Code Generation not available in Free mode

Memory Summary:
    Program space        used    24h (    36) of  2000h words   (  0.4%)
    Data space           used     4h (     4) of   170h bytes   (  1.1%)
    EEPROM space         None available
    Data stack space     used     0h (     0) of    60h bytes   (  0.0%)
    Configuration bits   used     1h (     1) of     1h word    (100.0%)
    ID Location space    used     0h (     0) of     4h bytes   (  0.0%)


You have compiled in FREE mode.
Using Omniscient Code Generation that is available in PRO mode,
you could have produced up to 60% smaller and 400% faster code.
See http://www.microchip.com/MPLABXCcompilers for more information.

make[2]: Leaving directory 'C:/Users/hadaw/MPLABXProjects/BlinkLed.X'
make[1]: Leaving directory 'C:/Users/hadaw/MPLABXProjects/BlinkLed.X'

BUILD SUCCESSFUL (total time: 6s)
Loading code from C:/Users/hadaw/MPLABXProjects/BlinkLed.X/dist/default/production/BlinkLed.X.production.hex...
Loading completed

i think it will work now...
 

Offline Luminax

  • Regular Contributor
  • *
  • Posts: 159
  • Country: my
    • Electronesk
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #53 on: March 28, 2017, 04:54:48 pm »
I wonder how effective the code 'conversion' will be if you actually purchased the XC8 License... hmmmm
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 #54 on: March 30, 2017, 05:45:04 pm »
It finally worked! thanks for all the help  ;D  :-+
I will post a video of it soon in the form of a tutorial so no body have to suffer through this like many others -including me- did,
so if u can or wish post anything that might be helpful to include in the video...
and if anybody have any questions on this topic they can ask it in this thread.
thanks a lot for all who contributed here , this is greatly appreciated :-+
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #56 on: April 09, 2017, 01:32:21 pm »
here is the video I made, A big thanks goes to you guys for the great help which made the video possible ...
also I hope u correct me if I got anything wrong in the video, so I can benefit from this experience too and can deliver better content in the future. :-+
https://youtu.be/eypsp_32Ptw
or


I hope it is informing... sorry if I didn't explain the code thoroughly, or overgeneralised when describing the function of certain registers or calling them  ports all the time... :palm:
give me anything that u guys think might need improving so this thread can deliver the low-level/beginner-level documentation a lot of people may need.
thanks a lot for all support
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #57 on: April 09, 2017, 05:06:24 pm »
so what do u think??
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12852
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #58 on: April 09, 2017, 05:43:07 pm »
lthough its nice to see the face we've been helping, as its a long video few of us will have watched it. If you want people to review a video, best keep it under 5 minutes till you've built up a good reputation, and if you cant cover a topic in such a format, consider blogging it or writing an instructable, with short embedded videos for key points to back-up your text.

You lost me 1 minute in when it became obvious that you hadn't prepared, checked and practised a script.   Dave can get away with unscripted stuff on EEVblog because he knows the field so well, but even so he puts a lot of work into locating technical data before making tutorial, design or review videos.   Its inexcusable NOT to know the part number you chose as an example of an unsupported part, and suggesting that the best way of identifying if a part is supported is to check in MPLAB is unhelpful due to the slowness and difficulty of checking multiple parts, especially as Microchip provide a concise HTML PIC vs tool compatibility chart as part of the MPLAB release notes.
 
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 #59 on: April 10, 2017, 01:19:03 pm »
lthough its nice to see the face we've been helping, as its a long video few of us will have watched it. If you want people to review a video, best keep it under 5 minutes till you've built up a good reputation, and if you cant cover a topic in such a format, consider blogging it or writing an instructable, with short embedded videos for key points to back-up your text.

You lost me 1 minute in when it became obvious that you hadn't prepared, checked and practised a script.   Dave can get away with unscripted stuff on EEVblog because he knows the field so well, but even so he puts a lot of work into locating technical data before making tutorial, design or review videos.   Its inexcusable NOT to know the part number you chose as an example of an unsupported part, and suggesting that the best way of identifying if a part is supported is to check in MPLAB is unhelpful due to the slowness and difficulty of checking multiple parts, especially as Microchip provide a concise HTML PIC vs tool compatibility chart as part of the MPLAB release notes.
true I fully agree, and I will do a "re-do/shot"  of the video and I will upload it to YouTube taking all your points in consideration... 
sorry if the audio quality was bad near the middle of the video for all who watched the hole video and prior to those who didn't watch it in hole...
 I will try to hopefully upload it this week or next week, I would like to thank you all for ur help especially Ian.M for his great constructive comment  :-+

in the next video I will cover in order:

1.the programmer  compatibility with the PIC
2.Compiler choosing 
3.IDE installation
4.code explanation (includes a general description of the the 4 main registers -port,tris,ANSEL and ANSELH-)

if anyone wants to add anything I missed or correct any thing wrong feel free to do so because i need to improve the kinds of videos i make  :)
 
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #60 on: June 27, 2017, 08:50:04 pm »
not re-shot yet:

i will re-shot it soon...
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #61 on: July 01, 2017, 08:08:01 pm »
just a quick question:
 are all Pickit2 compatible chips compatible with Pickit3?
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12852
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #62 on: July 01, 2017, 10:31:39 pm »
I just checked the MPLAB 8.92 device support (PICkit 2 device support was frozen somewhat earlier than v8.92, and was then ported to MPLAB X with no further changes), as I have it in a spreadsheet for easy searching, and there are NO PICs that have PICkit 2 programming support but aren't supported by the PICkit 3.
 
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 #63 on: July 02, 2017, 08:00:05 pm »
Thanks  :-+
 

Offline Luminax

  • Regular Contributor
  • *
  • Posts: 159
  • Country: my
    • Electronesk
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #64 on: July 06, 2017, 12:21:48 am »
I think we can safely say that there are NO PICs that are NOT supported by PicKit3... it's their flagship programmer after all
Jack of all trade - Master of some... I hope...
 
The following users thanked this post: ali6x944

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12852
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #65 on: July 06, 2017, 12:47:37 am »
Actually it doesn't support any of the old EPROM program memory PICS*.  They are mature products but many are still in production.   It supports all FLASH program memory PICs.   If you need to program the EPROM PICs with a currently supported programmer, you need a Microchip PM3. However unless you've got deep pockets, it makes more sense to pick up a second hand PICstart Plus and maintain a legacy MPLAB 8.92 install to run it.

* IIRC the algorithms for programming the PIC18Cxxx and PIC16Cxx parts could easily be ported to it, but its Vpp generator isn't beefy enough.   As a minimum it would require an external Vpp buffer and supply for it.
 
The following users thanked this post: ali6x944

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3140
  • Country: ca
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #66 on: July 06, 2017, 01:22:25 am »
It supports all FLASH program memory PICs.

Except PIC18F4539 and family. Not that this matters ...  ;)

More importantly, it supports newer parts, such as PIC18 K42 and will support PICs which appear in the future.
 
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 #67 on: July 19, 2017, 11:24:51 am »
Hi guys,
thanks for all the help, but do anyone knows how to use 8 bit ADC in PIC16F77 using XC8, because I'm going to use it to demonstrate this as an example code implementation of an 8-bit ADC and 10-bit ADC of modern PICs in the upcoming video...
I tried but with no luck:
config.h
Code: [Select]
#include <xc.h>
// include processor files - each processor file is guarded. 
// PIC16F77 Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#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 32786        // Sets the crystal oscillator frequency


void ADC_Init()
{
  ADCON0 = 0b00000001;               
  /*
   *See the the "0b" tells the compiler that this is a binary number,
   * The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
   * Bit 7-6 are A/D Conversion Clock Select bits, which set the sample clock.
   * Bit 5-3 are Analog Channel Select bits, which will select a particular channel out of 8 Analog channel.
   * Bit 2 is A/D Conversion Status bit, which initiates A/D conversion and it will be automatically cleared when the conversion completes.
   * Bit 1 is not used so set to zero.
   * Bit 0 is A/D On bit, which basically turns on/off the ADC
   */
  ADCON1 = 0b00000000; //All pins as Analog Input and setting Reference Voltages
  /*
   *See the the "0b" tells the compiler that this is a binary number,
   * The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
   * Bit 7-3 is not used so set to zero.
   * Bit 2-0 are A/D Port Configuration Control bits, which set the analog pins and refrence.
   */
}

unsigned int ADC_Read(unsigned int channel)
{
  if(channel > 7) //Channel range is 0 ~ 7
  {             
    return 0;
  }
  ADCON0 &= 0xC5;              //Clearing channel selection bits
  ADCON0 |= channel<<3;        //Setting channel selection bits
  __delay_ms(2);               //Acquisition time to charge hold capacitor
  GO_nDONE = 1;                //Initializes A/D conversion
  while(GO_nDONE);             //Waiting for conversion to complete
  return ((ADRES));
}
main.c
Code: [Select]
#include "config.h"

void main(void) {
    TRISAbits.TRISA0=1; // Analog Input pin AN0
    TRISAbits.TRISA1=1; // Analog Input pin AN1
    TRISBbits.TRISB7=0; // Digital Output pin RB7
    ADC_Init(); //Initialize ADC
    unsigned int ADC; // value of first analog channel
    unsigned int ADC1; // value of second analog channel
    ADC1=ADC_Read(1); //Initialize channel (1)
    ADC=ADC_Read(0); //Initialize channel (0)
    while(1){
    if (ADC==ADC1){
        PORTBbits.RB7=1; //if both channels are equal turn RB7 to high
    }
    else{
        PORTBbits.RB7=0;//else turn RB7 to low
    }
    }
   
   
    }
 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2850
  • Country: se
  • Country: Broken Britain
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #68 on: July 19, 2017, 12:36:24 pm »
ADCON0 = 0b00000001;
* Bit 7-6 are A/D Conversion Clock Select bits, which set the sample clock.


If the XTAL_FREQ is only 32,768, you'll need to use the ADC's internal RC oscillator.

ADCON0 = 0b11000001;
« Last Edit: July 19, 2017, 12:37:55 pm by StillTrying »
.  That took much longer than I thought it would.
 
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 #69 on: July 19, 2017, 02:22:34 pm »
 will try it in a second....
but I want to know why do I have to use the internal oscillator?
is the ADC in PIC micros need specific exact frequencies to work at properly?
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #70 on: July 19, 2017, 02:44:31 pm »
it dose not work...
I connected both AN0 and AN1 to ground, and RB7 didn't go high...
I did the same thing but connected the inputs to VDD with no luck too.
 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2850
  • Country: se
  • Country: Broken Britain
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #71 on: July 19, 2017, 03:22:20 pm »
Yep, the ADC clock speeds are limited ~250kHz - ~1MHz.

In ADC_Read(),  is ADCON0 &= 0xC5; (b11000101) is starting a conversion before you've set the channel number ?

In main(void) should ADC1=ADC_Read(1); & ADC=ADC_Read(0);  be inside the while(1) loop ?
« Last Edit: July 19, 2017, 03:25:17 pm by StillTrying »
.  That took much longer than I thought it would.
 
The following users thanked this post: ali6x944

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #72 on: July 19, 2017, 03:50:29 pm »
You may be expecting a bit too much from the A/D converter. You only read each channel once, and if they are not identical your code will fail. A/D conversion is inherently noisy and (especially at the extremes) channels may not track precisely.

Try connecting one channel to a pot which you can turn to vary the voltage from Vss to Vdd, and the other to a midpoint voltage (eg. using a voltage divider with 2 equal resistors). Then read the channels continuously inside the while() loop, and set the output according to which voltage is higher, like this:-

 
Code: [Select]
   while(1){
       ADC1=ADC_Read(1); //Initialize channel (1)
       ADC=ADC_Read(0); //Initialize channel (0)
       if (ADC > ADC1){
          PORTBbits.RB7=1; //if channel 0 > channel 1 then turn RB7 to high
       }
       else{
        PORTBbits.RB7=0; //else turn RB7 to low
       }


The LED should turn on/off as the pot is turned through its center position.   
 
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 #73 on: July 24, 2017, 11:15:53 am »
thanks a lot!! :-+
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf