EEVblog Electronics Community Forum

Electronics => Microcontrollers => Topic started by: MrT123 on August 20, 2017, 11:10:18 am

Title: [SOLVED] Getting started using PIC16F630 on MPLAB
Post by: MrT123 on August 20, 2017, 11:10:18 am
Hi everyone,
I've been trying to just blink an LED using a PIC16F630 and nothing really works.
I want to start from scratch again after multiple failed attempts.
I'm using an AC162049 development board and MPLAB to code on to it in C.
Would someone be ever so kind as to provide a circuit schematic and code of how you'd go about it? Im quite lost at this point.

Thanks for any advice you'd have to offer.
Title: Re: Getting started using PICF630 on MPLAB
Post by: Bruce Abbott on August 20, 2017, 06:49:40 pm
Code: [Select]
#define _XTAL_FREQ 4000000

#include <xc.h>

// Configuration Bits
#pragma config FOSC  = INTRCIO  // 4MHz internal RC oscillator
#pragma config WDTE  = OFF      // Wathdog timer disabled
#pragma config PWRTE = OFF      // Power-Up Timer disabled
#pragma config MCLRE = OFF      // GP3/MCLR is digital I/O
#pragma config BOREN = ON       // Brown-out Detect enabled
#pragma config CP    = OFF      // Code Protection disabled
#pragma config CPD   = OFF      // Data Code Protection disabled

// LED output pin, connected to Vdd via current-limiting resistor
#define LED PORTAbits.RA1

void main(){
   CMCON = 7;          // disable analog comparator, enable digital inputs
   TRISA = 0;         // set RA0-2, 4-5 to output mode (RA3 is input only)
   TRISC = 0;        // set RC0-5 to output mode
   while(1){
      LED = 0;         // LED on   
      __delay_ms(500);
      LED = 1;         // LED off         
      __delay_ms(500);
   }
}
Title: Re: Getting started using PICF630 on MPLAB
Post by: MrT123 on August 21, 2017, 08:05:06 am
Thanks for your reply, however it comes up with the error that it can't open the include file. No such file or directory
EDIT: Ive changed <xc.h> to a <htc.h> to remove this error.
but am now met with countless 'bad pragma' errors
bad pragma "x"
bad pragma "F"
bad pragma "x"
bad pragma "W"
bad pragma "x"
bad pragma "P"
bad pragma "x"
bad pragma "M"
bad pragma "x"
bad pragma "B"
bad pragma "x"
Title: Re: Getting started using PICF630 on MPLAB
Post by: alexanderbrevig on August 21, 2017, 08:19:51 am
I'm a newcomer to MPLAB, but which compiler are you running? Did you check the project settings that it's the right one?
Right click project -> properties. This should show you some helpful information.

When an include makes an error like "no such file or directory" it means it cannot find that file where it's told to look. This often means a) it's not told to look anywhere or b) it's told to look at the wrong place.
See this: http://www.microchip.com/forums/m802891.aspx (http://www.microchip.com/forums/m802891.aspx)
Title: Re: Getting started using PICF630 on MPLAB
Post by: MrT123 on August 21, 2017, 08:54:40 am
You were correct, after doing some digging into the directories, and finding 'pic16f630.h', the header file for the microchip I'm using, it said to use #include <htc.h> instead.

So as instructed I've changed <xc.h> to a <htc.h>
but am now met with countless 'bad pragma' errors
bad pragma "x"
bad pragma "F"
bad pragma "x"
bad pragma "W"
bad pragma "x"
bad pragma "P"
bad pragma "x"
bad pragma "M"
bad pragma "x"
bad pragma "B"
bad pragma "x"

ps. soz for the repetition, but helps with the flow of the thread i suppose.
Title: Re: Getting started using PICF630 on MPLAB
Post by: Ian.M on August 21, 2017, 08:59:30 am
It looks like you haven't got the Microchip XC8 C compiler for PIC10/12/16 and PIC18 installed correctly.  It is *NOT* bundled with MPLAB X, you have to install it separately.   From the errors you are getting its most likely you have some version of the discontinued and obsolete HiTech C compiler for PIC10/12/16 installed.   Unless you have a paid licence for it I *STRONGLY* recommend upgrading to XC8

Also, are you sure about that development board number?
AC162049 is the old version of Microchip's Universal Programming Module.
(http://www.microchip.com/forums/download.axd?file=0;770621) (http://www.microchip.com/forums/m770536.aspx)

It is only intended for *PROGRAMMING* DIP package PICs that you are then going to move to a different board to run them.  Its got no switches or LEDs and no provision for external power.   

N.B. Please don't abbreviate PIC part numbers as it causes great confusion.  Fortunately this time there are no other devices with '630' in their part number to cause confusion so we know you mean the PIC16F630.

P.S. you can copy the error messages direct from the MPLAB output window and paste them here in BBcode code tags (https://www.bbcode.org/examples/?id=15).  Start by clearing the output window, do a build and copy the result.  You may wish to first paste it into a text editor so you can replace your user name in file paths with a string of ***** before pasting the result here.  If we have the *original* error message(s) we can often provide far more specific help.
Title: Re: Getting started using PICF630 on MPLAB
Post by: alexanderbrevig on August 21, 2017, 09:00:12 am
Are you using a HI TECH compiler?

Which compiler are you using?
Did you check project properties? Toolchain, and maybe also linker.

EDIT: Ian.M beat me to it.
Title: Re: Getting started using PICF630 on MPLAB
Post by: Wilksey on August 21, 2017, 09:00:37 am
Are you using MPLAB or MPLABX?

If you have the XC8 compiler you should be able to use "#include <xc.h>".

Sounds like you are trying to use the older HiTec C compiler?
Title: Re: Getting started using PIC16F630 on MPLAB
Post by: MrT123 on August 21, 2017, 09:10:56 am
I am using MPLAB IDE v8.70 and I am using the HI-TECH Universal Toolsuite. The development board that I am using is the one shown in the picture provided by Ian. For the purposes of this project I am unfortunately stuck with this.

EDIT: with regards to the AC162049 development board, I was under the impression that I could supply external power through pins 9 and 10 on the board itself and have Vdd, Vss and GND connected as seen in your picture
Title: Re: Getting started using PIC16F630 on MPLAB
Post by: Ian.M on August 21, 2017, 09:13:44 am
You would be better off putting your PIC  in a solderless breadboard and wiring the programmer direct to its ICSP pins.   What programmer are you using?

Also, what HiTech C compiler version are you using?
Title: Re: Getting started using PIC16F630 on MPLAB
Post by: MrT123 on August 21, 2017, 09:16:40 am
How would I find out which HiTech C compiler version im using?
Title: Re: Getting started using PIC16F630 on MPLAB
Post by: Ian.M on August 21, 2017, 09:27:31 am
When you do a build, it will print the version number in the output window on the line immediately after the one that invokes the compiler (picc.exe).
Title: Re: Getting started using PIC16F630 on MPLAB
Post by: MrT123 on August 21, 2017, 09:30:46 am
Right, ok,
What I get is
HI-TECH Compiler for PIC10/12/16 MCUs (Lite Mode) V9.81
Copyright (C) 2010 Microchip Technology Inc.
(1273) Omniscient Code Generation not available in Lite mode (warning)

followed by the errors
Title: Re: Getting started using PIC16F630 on MPLAB
Post by: Ian.M on August 21, 2017, 09:51:51 am
Ok, that's post the Microchip buyout of HiTech, and has got *some* of the newer features found in XC8, but it doesn't support #pragma config.

Rip out all those lines and to replace them, try:
Code: [Select]
__CONFIG(FOSC_INTRCIO &WDTE_OFF & PWRTE_ON &MCLRE_OFF &BOREN_ON);If you can provide a 10K pullup for /MCLR its better to use:
Code: [Select]
__CONFIG(FOSC_INTRCIO &WDTE_OFF & PWRTE_ON &MCLRE_ON &BOREN_ON);as MCLRE_OFF (+ making RA0 and/or RA1 outputs) can make it very difficult to reprogram your PIC if you are using a programmer that doesn't properly support Vpp First mode.

If you've got admin rights on the PC you may wish to upgrade to MPLAB v8.93 and XC8.  The install is a little kludged as XC8 dropped toolsuite registration for MPLAB 8 a few versions back so ask if you want to do that.
Title: Re: Getting started using PIC16F630 on MPLAB
Post by: MrT123 on August 21, 2017, 10:22:52 am
Ah thanks,it compiles with no warnings and errors now.
I will use the circuit suggested by bruce above but with a 10k pullup resistor attached to MCLR (as Im using the MCLRE_ON method as suggested by Ian)
Title: Re: Getting started using PIC16F630 on MPLAB
Post by: MrT123 on August 21, 2017, 11:14:42 am
Works perfectly, thanks for our time and help folks.  :phew: :clap: :clap: