C problems

Status
Not open for further replies.

MrJammin

New Member
I've been trying to execute the simplest code possible to check that my C code is working:


#include <p18f4220.h>

void main(void)
{
TRISB=0x00;
PORTB=0xf0;

}


This sets half of the pins on PORTB high right? MPLAB compiles it fine, but for some reason the chip just retains zero volts over all pins. The only configuration bit I changed was the Watchdog Timer, which I turned off. Am I missing something else?
 

Mr Jammin... i hope you got the solution...

I wanted one favour...

Can you tell me how to use c compiler in MPLAB IDE...

I am having Hi tech C compiler engine for IDE's...

Regards,

Simran..
 
It's also a good idea to include some configuration settings,
Code:
#include <p18f4220.h>

#pragma config WDT = OFF, LVP = OFF, OSC = INTIO67

void main(void)
{
   TRISC = 0x0;
	while(1)
	{
		PORTC = 0xF0;
	}
}

Mike.
 
mikesmixes777 said:
I normally use the configuration menu, but you right, you have to have settings somewhere be it in the code or in the menu.

You should ALWAYS include the configuration settings in the code - if you come back to the code in five years time, how are you going to remember what they were?.
 
I'm new at C18 but the while loop seemed unnecessary so I decided to run this little routine using the hardware debugger.
Code:
#include <p18cxxx.h>
#pragma config OSC=INTIO2, WDT=OFF, LVP=OFF
void main(void)
{
    OSCCON = 0x72;    // 8 MHz
    ADCON1 = 0xFF;    // All I/O digital
    LATA = 0x00;
    TRISA = 0x00;    // Port A all outputs
    LATA = 0xFF;
}
It runs fine, exactly as expected.
PORTA = 0xDF (MCLR RA.5 can only be reset or input)
 
Never Never Land

It is usually a good idea to put something like:

while(1);

At the end. Else the program goes off into Never Never Land. Several of the simulators go nuts without something to "hang things up".

 
Think of it this way. What is the processor going to do after it runs your code? We add the while(1) to ensure that we know what it will do.

 
Ahh it's like GOTO $ in assembly

I was initially confused as I thought the code wouldn't execute without it. Instead it simply repeats the instructions in the {} forever.
 
blueroomelectronics said:
Ahh it's like GOTO $ in assembly

I was initially confused as I thought the code wouldn't execute without it. Instead it simply repeats the instructions in the {} forever.

Yes it is a do nothing forever. Where have I seen that?
 
Ya...

While(1)
{

}

is infinite loop if mentioned in the starting of the program... although in some compilers we dont have to do this...

but when you put this:

while(1) ; at the end of program then it acts like goto $;

Regards,

Simran..
 
More Never Never Land

Mike, et all,

Think of C more as a Low Level Language (sort of like assembler) parading around as a "high" level language.

You gain the structured programming constructs (if then else, while, etc) but really maintain a "low" level view of the world. C really only has a one byte, one word view of the data world. Strings are really arrays of bytes.

But this can be good. You can go look at the .lst or .asm file and follow along what the compiler generated for you and if necessary make adjustments to the code.
 
Thanks for the help, the pins turn on appropriately when I put in the while loop at the end. However, I've encountered a new problem. The pins output a voltage of ~.75V, instead of the 5V I expect. I'm powering the chip with 5V on both sides of the chip.

@Simran
Download C18 from microchip.com. Then, when you start a new project in MPLAB make sure you choose the C18 toolsuite. After that you need to add two files to your project. One is a header file for the chip, another is a linker file. For example, I am using a 18f4420, so I added the files p18f4220.h and 18f4220.lkr. You should be able to find these files in the C18 directory if you installed C18 correctly.
 
Did you disable the A/D? PORTB will power up with some pins in analog mode.

Look for a PBADEN config statement (set it to 0)
 
Last edited:
Turning off the A/D enable for PORTB worked, although neither PBADEN=0 or PBADEN=OFF would compile, so I had to turn it off manually in the configuration bits in MPLAB.

However, now I'm trying to use PORTC, and I've run into similar problems. PORTC only outputs voltages of ~.25V

Here is the code I am currently using

#include <p18f4220.h>
#include <stdio.h>

#pragma config OSC=INTIO2, WDT=OFF, LVP=OFF

void main(void)
{
OSCCON = 0x72; // 8 MHz

TRISC=0x00;
PORTC=0xf0;

while(1){;}
}


Also, what tag do you use to put code in that special box.
 
Seems the syntax is from reading the .h file
_PBAD_DIG_3H

ps try PORTD it generally has the least amount of peripherals, mostly works like an I/O port by default.
 
Last edited:
Unfortunately, when setting the config in C18, you can't look in the .h file for guidance. For this reason Microchip released this **broken link removed** document. In this case the switch is PBAD = DIG.

Mike.
 
Pommie said:
Unfortunately, when setting the config in C18, you can't look in the .h file for guidance. For this reason Microchip released this **broken link removed** document. In this case the switch is PBAD = DIG.

Mike.

Thanks Pommie, that's a keeper.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…