Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Code not executing on PIC18F4520

Status
Not open for further replies.

haxan

New Member
Hi. I have written a small test program which burns successfully on the controller (have verified after read) but the code doesn't execute.

Has anyone faced this problem before?

I have LEDs attached to PORTB but nothing is on the output (The LEDs work with 5volts direct hence not burnt :p)

Code:
#include <p18cxxx.h>
#include <delays.h>
#include <stdlib.h>
#include <stdio.h>

#pragma config WDT = OFF, LVP = OFF, OSC = HS, DEBUG = OFF


void main(void)
{
	ADCON1	= 0x0f;						// make all AN (analog) pins digital (for bit ref)
	TRISA	= 0x00;
	TRISD = 0;
	TRISB = 0;
	TRISC = 0;
	PORTA = 0;
	PORTB = 0x55;
	PORTC = 0;
	PORTD = 0x55;
	while(1)
	{
		Delay10KTCYx(100);
		PORTAbits.RA0 ^= 1;
		PORTB ^= 0xFF;
		PORTD ^= 0xFF;
		Delay10KTCYx(100);
	}
}
 
You have to start by putting in some instructions that turn on an output. Put a LED and resistor on the output.


Then add a delay and make the LED blink.

Then come back to us.
 
I actually have Proteus installed on my system and it work fine on it. I have tried to use MPLAB simulator but cannot seem to understand its working.

It just keeps on saying Running... in the bottom left corner and nothing happens.
 
In order to see what the program does in MPLAB, you have to go through the program step by step. When it says running down in the bottom corner, MPLAB is just running the program as fast as it can. If you step through the program (by using 'step into'), then you can watch each register and see what it does in each part of your program.
 
Hi. I have written a small test program which burns successfully on the controller (have verified after read) but the code doesn't execute.

Has anyone faced this problem before?

I have LEDs attached to PORTB but nothing is on the output (The LEDs work with 5volts direct hence not burnt :p)

Code:
#include <p18cxxx.h>
#include <delays.h>
#include <stdlib.h>
#include <stdio.h>

#pragma config WDT = OFF, LVP = OFF, OSC = HS, DEBUG = OFF


void main(void)
{
	ADCON1	= 0x0f;						// make all AN (analog) pins digital (for bit ref)
	TRISA	= 0x00;
	TRISD = 0;
	TRISB = 0;
	TRISC = 0;
	PORTA = 0;
	PORTB = 0x55;
	PORTC = 0;
	PORTD = 0x55;
	while(1)
	{
		Delay10KTCYx(100);
		PORTAbits.RA0 ^= 1;
		PORTB ^= 0xFF;
		PORTD ^= 0xFF;
		Delay10KTCYx(100);
	}
}

First the Disclaimer: Since I'm a glutton for punishment, I mostly program in Assembly so I'm not good with PIC C.

Since you're doing an XOR to flip the bits which operates on the present state of the register, you might be running into the read-modify-write problem. It doesn't look like it would be a problem in your case at first glance but you might try doing your bitwise operations on a shadow register first, then write the contents of that register to the port pins when you're done.

If stepping through the code with the simulator like another poster suggested appears to work fine, then that's probably it.

If that's not the problem and I missed something obvious, see the disclaimer above. :)

- Phil
 
Last edited:
Thank you Phil :) Actually the code works in simulation software such as Proteus VSM perfectly.

I have even tested simpler codes such as making the whole portB on and then off after a certain delay but even that doesn't execute.

I have a PIC trainer which runs code for 16F877a perfectly so since the datasheet implies same pin configuration, the PIC18 should also work.

P.S i was having problems initially with PIC16F877a aswell but was settled after i programmed it with XT OSC. I tried same thing with PIC18 but couldnt make it execute.

Can you give me a test hex file (maybe written in assembly) i could burn on controller and test :)
 
As mentioned, check that the oscillator is working with a scope, or simply use the internal oscillator (Instead of OSC = HS, use OSC = INTOSC or whatever to select the internal oscillator). You may have to also set the OSCCON register if you want something other than the default 1Mhz clock speed.
As for MCLR, place a 10k resistor between the MCLR pin and Vdd. Also, ALL Vdd pins must be connected together. Same goes for the Vss pins.
Also place a 0.1uF capacitor between the Vdd and Vss pins of the PIC. Place it close as possible to the PIC.
 
Last edited:
To switch to the internal oscillator you can just add the line OSCCON=0x72; and this will force it to use the internal oscillator. Note that selecting the internal oscillator this way stops the PLL working.

Mike.
 
Hi, sorry to bother again

kchriste Said:
Also place a 0.1uF capacitor between the Vdd and Vss pins of the PIC. Place it close as possible to the PIC
Do i have to add this cap on both sides (11,12 and 31,32 pins) or is one side ok. Also i should you polar cap right ?

Also, when i try to bring the cap closer to the controller, the oscillator crystal has to be pushed away, is that alright?
 
The 0.1uf cap has to be placed between Vdd and Vss, you are powering both sides of the IC from the same supply as advised above right? The cap is non electrolytic.

Why would you need to push away the oscillator? Its just a filter capacitor. When done, check the voltages at MCLR and the power supply pins with of course the internal oscillator configuration.

If you are still using an external oscillator, check it on an oscilloscope.
 
Last edited:
Are you building a debug build and then trying to run it without a debugger?
 
Hi Smanches, i always port my code using Debug build Configuration, haven't encountered a problem once , although the code size is said to be a bit larger then the release build (which is of course recommended when programming your device).

Also Haxan just read you test file thing, here you go..

A advice: Dont go blindly relying on Proteus for proving your code error free.
 

Attachments

  • pintest..zip
    654 bytes · Views: 120
Be sure to check that your Xtal is hooked up correctly. Can't run in HS mode without a working xtal.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top