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?
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?
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?.
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.
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.
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.
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.