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

0 Members and 1 Guest are viewing this topic.

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Help! How to program PIC microcontrollers using MPlab X IDE?
« on: March 20, 2017, 09:45:53 am »
hello everyone,
UPDATE:
if anybody have any questions on this topic they can ask it in this thread.
-------------------------------------------------------------------------------------
I have been trying to program PIC16F77 ; see datasheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/30325b.pdf

I'm programming this device using a pickit3 and XC8 compiler toolchain in Mplab X IDE, and so far I'm not able to make the PIC do anything, not even blinking LEDs or reading inputs from a switch...

this is a picture of the code in the Main.c file and below it is what i saw at the output window:
« Last Edit: March 31, 2017, 04:23:28 am by ali6x944 »
 

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #1 on: March 20, 2017, 10:41:38 am »
It says that the IDE can't connect to the programmer.

Did you try running the MPLABX Driver switching tool?

It should have been installed along with the IDE.

If no shortcut exists you can find it:
C:\Program Files (x86)\Microchip\MPLABX\v3.XX\Switcher

Where 3.XX is the version of the IDE installed.

Inside there will be a 32 or 64 bit folder, and inside there will be a file called MPDDSwitch64.exe (or 32 depending on version).

Note, this only applies if you have both MPLABX and MPLAB IDE installed.
 
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 #2 on: March 20, 2017, 10:58:50 am »
It says that the IDE can't connect to the programmer.

Did you try running the MPLABX Driver switching tool?

It should have been installed along with the IDE.

If no shortcut exists you can find it:
C:\Program Files (x86)\Microchip\MPLABX\v3.XX\Switcher

Where 3.XX is the version of the IDE installed.

Inside there will be a 32 or 64 bit folder, and inside there will be a file called MPDDSwitch64.exe (or 32 depending on version).

Note, this only applies if you have both MPLABX and MPLAB IDE installed.
is it this; see picture below:
also what should i do with it?
 

Offline jmag999

  • Contributor
  • Posts: 41
  • Country: us
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #3 on: March 20, 2017, 11:22:49 am »
It looks like you have both the IDE and IPE open at the same time.  Try closing the MPLAB IPE as it may be connected to the debugger.
 
The following users thanked this post: ali6x944

Offline mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 13748
  • Country: gb
    • Mike's Electric Stuff
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #4 on: March 20, 2017, 11:26:24 am »
I don't think the driver switcher is needed for PICKit 3
Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 
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 #5 on: March 20, 2017, 11:38:35 am »
It looks like you have both the IDE and IPE open at the same time.  Try closing the MPLAB IPE as it may be connected to the debugger.
I closed Both the IDE & IPE
NOTE: 
that was to open the switcher.
« Last Edit: March 20, 2017, 11:48:12 am by ali6x944 »
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #6 on: March 20, 2017, 12:03:23 pm »
did i make a mistake in the code?
i have to say I'm  a newbie in the PIC microcontollers stuff, but i would really want to know how to use them... :)
« Last Edit: March 20, 2017, 12:13:25 pm by ali6x944 »
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #7 on: March 20, 2017, 01:19:42 pm »
You haven't configured the oscillator, and your _XTAL_FREQ #define indicates you probably are attempting to use a 32.768KHz watch crystal, so I'd bet the oscillator isn't running.  See datasheet FIGURE 12-3: RC OSCILLATOR MODE
and try it with a 100pF capacitor and a 10K resistor, which should get it runing at approximately 2MHz.   Once you have satisfied yourself that you can run code on it, you can reconfigure it for LP mode and try the watch crystal again, in the FIGURE 12-1: CRYSTAL/CERAMIC RESONATOR OPERATION (HS, XT OR LP OSC CONFIGURATION) circuit.

To get the configuration right use the MPLAB  X wizard: View and Set Configuration Bits.
 
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 #8 on: March 20, 2017, 01:38:35 pm »
You haven't configured the oscillator, and your _XTAL_FREQ #define indicates you probably are attempting to use a 32.768KHz watch crystal, so I'd bet the oscillator isn't running.  See datasheet FIGURE 12-3: RC OSCILLATOR MODE
and try it with a 100pF capacitor and a 10K resistor, which should get it runing at approximately 2MHz.   Once you have satisfied yourself that you can run code on it, you can reconfigure it for LP mode and try the watch crystal again, in the FIGURE 12-1: CRYSTAL/CERAMIC RESONATOR OPERATION (HS, XT OR LP OSC CONFIGURATION) circuit.

To get the configuration right use the MPLAB  X wizard: View and Set Configuration Bits.
see picture below:
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #9 on: March 20, 2017, 01:41:36 pm »
 I will change it to a RC oscillator, as u suggested...
just give me a few minutes...
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #10 on: March 20, 2017, 01:51:24 pm »
i have changed it but still the code out of the config bit did not change.
see picture:
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #11 on: March 20, 2017, 01:57:21 pm »
i will remove the #define _XTAL_FREQ 32768...
 

Offline mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 13748
  • Country: gb
    • Mike's Electric Stuff
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #12 on: March 20, 2017, 01:58:06 pm »
From memory I think on that part there is no internal RC osc, so if you config as RC you need to add an external RC.

Any reason you're using such an old part?

Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 
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 #13 on: March 20, 2017, 02:18:04 pm »
From memory I think on that part there is no internal RC osc, so if you config as RC you need to add an external RC.

Any reason you're using such an old part?


true, it probly dose not have an internal RC type oscillator despite  it saying in 5.0 TIMER0 MODULE the following:
The Timer0 module timer/counter has the following
features:
• 8-bit timer/counter
• Readable and writable
• 8-bit software programmable prescaler
• Internal or external clock select
• Interrupt on overflow from FFh to 00h
• Edge select for external clock
   

i think there is no relation with whither the chip has an internal oscillator or not,  however this is rather confusing  :-//
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #14 on: March 20, 2017, 02:19:56 pm »
If setting the oscillator in the CONFIG wizard didn't generate a matching #pragma config line, its probably a bug and you should add:
Code: [Select]
#pragma config FOSC = LPmanually to your program if you want to use the watch crystal.
IIRC it will default to ext. RC mode, but you should still add a line for it, if you are using it:
Code: [Select]
#pragma config FOSC = RC
It will do sweet F.A if you don't provide external oscillator components as per fig 12-1, 12-2 or 12-3 with corresponding FOSC setting,  and also a pullup to Vdd on /MCLR - 10K is a good choice.
 
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 #15 on: March 20, 2017, 02:33:41 pm »
Any reason you're using such an old part?
It is one of the cheapest and powerful chips in the one of the few electronics in the eastern province...
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 567
  • Country: sa
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #16 on: March 20, 2017, 03:22:42 pm »
i add it give me a couple of minuets to test the circuit...
see code below:
 

Offline mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 13748
  • Country: gb
    • Mike's Electric Stuff
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #17 on: March 20, 2017, 03:30:31 pm »
You need to fix that programmer issue before even looking at code issues.
Try a reboot.

Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 
The following users thanked this post: ali6x944

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 7733
  • Country: ca
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #18 on: March 20, 2017, 03:59:58 pm »
I don't think his PC is even seeing the PICkit3 programmer.  Are you sure your PICkit3 is directly connected to your PC's USB 2.0 Port?  No USB extension cable, no USB hub.
 
The following users thanked this post: ali6x944

Offline mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 13748
  • Country: gb
    • Mike's Electric Stuff
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #19 on: March 20, 2017, 04:01:47 pm »
I don't think his PC is even seeing the PICkit3 programmer.  Are you sure your PICkit3 is directly connected to your PC's USB 2.0 Port?  No USB extension cable, no USB hub.
Does it appear in Device manager?
Does it work with MPLABX IPE ?
I've seen issues with PICKit3 on hubs

Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 
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 #20 on: March 20, 2017, 04:30:31 pm »
I don't think his PC is even seeing the PICkit3 programmer.  Are you sure your PICkit3 is directly connected to your PC's USB 2.0 Port?  No USB extension cable, no USB hub.
Does it appear in Device manager?
Does it work with MPLABX IPE ?
I've seen issues with PICKit3 on hubs


well it works in the IPE, but i can not see it in the device manager...
 

Offline mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 13748
  • Country: gb
    • Mike's Electric Stuff
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #21 on: March 20, 2017, 04:34:19 pm »
That confirms that the hardware and connection to chip is OK.
Looks like some sort of USB driver issue stopping MPLABX talking to it.
Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 
The following users thanked this post: ali6x944

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 826
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #22 on: March 20, 2017, 04:35:55 pm »
BlinkLed Dashboard - click Refresh Debug Tool Status (second icon down) - will show various pickit3 info if connected
or click Project Properties icon (first one) - if pickit3 listed with no serial number- not connected
(also make sure project set to use the pickit3)

It looks like it has been connected properly at one time, otherwise there would be no pickit3 serial number listed.
 
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 #23 on: March 20, 2017, 04:52:57 pm »
BlinkLed Dashboard - click Refresh Debug Tool Status (second icon down) - will show various pickit3 info if connected
or click Project Properties icon (first one) - if pickit3 listed with no serial number- not connected
(also make sure project set to use the pickit3)

It looks like it has been connected properly at one time, otherwise there would be no pickit3 serial number listed.

see what i saw:
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 826
Re: Help! How to program PIC microcontrollers using MPlab X IDE?
« Reply #24 on: March 20, 2017, 05:36:54 pm »
If pickit3 serial# is listed in project properties, it is seen by MPLABX (you can verify- disconnect pickit3, open project properties- pickit3 will be listed with no serial#).

Your previous errors indicate MPLABX is not communicating with the pickit3, but also note there is a setting in the project properties for the pickit3 to provide power to your circuit- if you are using the pickit3 to provide power and have not enabled that setting, MPLABX will communicate with the pickit3 ok but the pickit3 will complain that it can't talk to the chip (which does not appear to be happening to you, yet )

I would disconnect the pickit3 connections on the 6pin header, then try to program- it will fail of course, but you should see communications happening between MPLABX and pickit3. (I'm not sure what, if anything could go wrong  if connecting the pins incorrectly, but this would eliminate that possibility).

You may need to Google 'PICkit3PlatformTool' (part of your error message)- I'm sure you are not the first with this error.
« Last Edit: March 20, 2017, 06:55:04 pm by cv007 »
 
The following users thanked this post: ali6x944

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 7733
  • 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?
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
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:
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
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

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 7733
  • 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

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
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

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 7733
  • 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...
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
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

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 7733
  • 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
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
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

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
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: 12860
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: 12860
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: 12860
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: 3146
  • 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