Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Cannot get complied program to work on 16f877a

Status
Not open for further replies.

kisa72

Newbie level 4
Joined
Jun 26, 2013
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
59
Hi,

I'm hoping this is something simple, I've been at it most evenings this week and I'm slowly going insane.

I just got back into pic programming (well trying to) after a few years and I'm failing epicly.

I'm using a PICkit 2 and a dev boeard I made a while ago that holds a 16f877a and is programmed via an ICD connector.

This is the code I'm compiling and programming onto the pic:-

Code:
//(c)Shane Tolmie, http://www.microchipc.com/, distribute freely for non commercial use on the condition that you include this web link somewhere in your document.
#include <pic.h>  //designed for PIC16F877
#include    <htc.h>

#define _XTAL_FREQ 20000000L            // Used in __delay_ms() functions
#define OUTPUT 0
#define INPUT 1
main()
{	  
	while(1)
	{
	   	TRISB1=OUTPUT; //change port RB1 direction to output
	    	RB1=1;         //produce 5V on RB1, pin 2 of micro
	   	__delay_ms(500);    // delay 500ms
	   	RB1=0;         //produce 0V on RB1, pin 2 of micro
	   	__delay_ms(500);    // delay 500ms
	}
}

The code compiles fine and programs/varifying fine on the PIC and it even runs properly on the PIC Simulator IDE, but when I actually run it on the PIC nothing happens i.e. the LED connected to RB.1 does not get lit (The LED it's held high and should light when RB.1 is pulled low).

I've tried a variety of different code examples, compiled using XC8 and Hi-Tech as well as trying PBP. Nothing seems to work.

So of course I though it might be my test board, but I downloaded and already complied .hex for the 16f877a and programmed it in and it worked fine, all Port B was happily flashing away.

With the code above programmed in, I measured the following voltages across Port B:-
RB.0 - 5V
RB.1 - 3.4V
RB.2 - 3.4V
RB.3 - RB.7 - 0V

Any help would be great.

Thank you.
 

Good day
have you tried compiling the program having TRISB1=OUTPUT; outside the loop ?
 
  • Like
Reactions: kisa72

    kisa72

    Points: 2
    Helpful Answer Positive Rating
yes you have to configure the port outside of while loop.
 

Thanks for the suggestion guys. I moved it out of the loop and into the main, still no luck :(. I even tried it out of the main, but errored while compiling.

So not it looks like:-
Code:
//(c)Shane Tolmie, http://www.microchipc.com/, distribute freely for non commercial use on the condition that you include this web link somewhere in your document.
#include <pic.h>  //designed for PIC16F877
#include    <htc.h>

#define _XTAL_FREQ 20000000L            // Used in __delay_ms() functions
#define OUTPUT 0
#define INPUT 1

main()
{	  
	TRISB1=OUTPUT; //change port RB1 direction to output
	while(1)
	{

	   	RB1=1;         //produce 5V on RB1, pin 2 of micro
	   	__delay_ms(500);    // delay 500ms
	   	RB1=0;         //produce 0V on RB1, pin 2 of micro
	   	__delay_ms(500);    // delay 500ms
	}
}

Are all the following coding commands correct, do they only relate to a specific c compiler?:-
#include <pic.h>
#include <htc.h>
#define _XTAL_FREQ 20000000L
#define OUTPUT 0
#define INPUT 1
TRISB1=OUTPUT;
RB1=1;
__delay_ms(500);
 

from my very modest readings , this looks like HiTech C
 
  • Like
Reactions: kisa72

    kisa72

    Points: 2
    Helpful Answer Positive Rating
You have set the configuration word haven't you? The HEX file you tried may have had the configuration word settings in it. Don't forget to turn the watchdog timer off and set the correct crystal type.
 
  • Like
Reactions: kisa72

    kisa72

    Points: 2
    Helpful Answer Positive Rating
you can turn watchdog off using CLRWDT();
also PJ pointed a good matter , is your hardware set to work with 20Mhz ?
 
  • Like
Reactions: kisa72

    kisa72

    Points: 2
    Helpful Answer Positive Rating
This works fine for me:
Check your hardware!!!
Code:
//(c)Shane Tolmie, http://www.microchipc.com/, distribute freely for non commercial use on the condition that you include this web link somewhere in your document.
#include <pic.h>  				//designed for PIC16F877
#include <htc.h>

__CONFIG ( FOSC_HS & WDTE_OFF & PWRTE_OFF & CP_OFF & BOREN_OFF & LVP_OFF & CPD_OFF & WRT_OFF & DEBUG_OFF );

#define _XTAL_FREQ 20000000L	// Used in __delay_ms() functions
#define OUTPUT 0
#define INPUT 1

main()
{	  
	TRISB1=OUTPUT;				// change port RB1 direction to output
	TRISB2=OUTPUT;				// change port RB2 direction to output
	while(1)
	{
	   	RB1=1;         			// produce 5V on RB1, pin 34 of micro
	   	RB2=0;         			// produce [U]0[/U]V on RB2, pin 35 of micro
	   	__delay_ms(500);    	// delay 500ms
	   	RB1=0;         			// produce 0V on RB1, pin 34 of micro
	   	RB2=1;         			// produce [U]5[/U]V on RB2, pin 35 of micro
	   	__delay_ms(500);    	// delay 500ms
	}
}
 
Last edited:
  • Like
Reactions: kisa72

    kisa72

    Points: 2
    Helpful Answer Positive Rating
Thanks again for all the quick replies and suggestions. It's definately a 20MHz external xtal and here are the Configuration Bits set under Configure in MPLAB v8.15a ("Configuration Bit is set in code" is checked):-

AddressValueCategorySetting
2007FFB2OscillatorHS
Watchdog TimerOff
Power Up TimerOn
Brown Out DetectOff
Low Voltage ProgramEnabled
Data EE Read ProtectOff
Flash Program WriteWrite Protection Off
Code ProtectOff

I just added the line (while typing this :) ):-

__CONFIG ( FOSC_HS & WDTE_OFF & PWRTE_OFF & CP_OFF & BOREN_OFF & LVP_OFF & CPD_OFF & WRT_OFF & DEBUG_OFF );

As suggested, complied, programmed and it worked!

There seems to be some options not set in the Configuration Bits that are in the __CONFIG. Which setting has priority in the programming when the __CONFIG and Configuration Bits set contradict? e.g like the LVP setting is.

I had tried a number of __CONFIG's before (copy and paste), which hadn't worked, I obviously missed some or had something set wrong. I'm intrigued now, I'll have to go through and try and work out what's made the difference.

Thanks again to all who helped out with suggestions and helped get it to work. Now I can move onto my original goal of getting my serial comms working :)
 
Last edited:

RB3 will not work with LVP enabled, but your code says you are using RB1. I can't see any reason why the configuration settings that worked are functionally any different to the ones that didn't. Normally the configuration settings in the source code set the configuration word in the programmer when the program is loaded, these settings can be over ridden by the user in the programmer.
 
  • Like
Reactions: kisa72

    kisa72

    Points: 2
    Helpful Answer Positive Rating
So just to clarify, if the Configuration Bit is set under the Configure menu in MPLAB, the associated Configuration settings will override whatever had been set in the code?
 

No the configuration bits are set when you load the code, these can be overridden by setting the config menu in MPLAB after you have load the code. If you set the configuration bits in MPLAB before you load the code, then these will be overwritten when you load the code.
 
  • Like
Reactions: kisa72

    kisa72

    Points: 2
    Helpful Answer Positive Rating
Sorry I don't quite understand how MPLAB can change the PICs configuration after it's been programmed. What is done in MPLAB (after the code has been loaded onto the PIC) to actually force the configuration changes onto the PIC?
 

Look sharp. pjmelect talks about loading the code into MPLAB, then changing fuses, finally programming it to the PIC.

Nevertheless, the programmer can overwrite parts of the code, e.g. the fuses.
 
  • Like
Reactions: kisa72

    kisa72

    Points: 2
    Helpful Answer Positive Rating
:), got it. I'm used to the term of "loading code" referring to programming the PIC. Thanks for the clarification.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top