Simple C code for PIC18

Status
Not open for further replies.

Gaby

New Member
Hello,

I have written a simple C program for PIC18F4550, using C18 compiler. The program job is to make PORTB displays the values from 0 to 254.
The problem is that, during the debugging, nothing is shown on PORTB, unless the values: 32, 64, 96, 128, 244.
This is the program:

#include <p18f4550.h>
void main(void)
{
unsigned char z;
TRISB = 0;
for (z = 0; z <255; z++)
PORTB = z;
while(1);
}

Despite the value of z is changing, PORTB has no change.
You can see the attached photo showing the problem.
So what is the problem? Is there anything missing?
 

Attachments

  • MPLAB.jpg
    232 KB · Views: 195
PORTB has several ADC outputs... You need to specify PBADEN = OFF in your config bits..

#PRAGMA config PBADEN = OFF...

Also... When using the port as an out put... use LATB ( prevents RMW )...
 
Hello Ian
Thank you for your help. I tried the program with the configuration you told me about, also I tried adding some other configurations, but nothing worked, same problem.
This is the code I used:
#include <p18f4550.h>
#pragma config WDT = OFF
#pragma config MCLRE = ON
#pragma config DEBUG = ON
#pragma config LVP = OFF
#pragma config FOSC = INTOSCIO_EC
#pragma config PBADEN = OFF

void main(void)
{
unsigned char z;
TRISB = 0;
for (z = 0; z <255; z++)
{
PORTB = z;
}
while(1);
}
Also I tried to put the value in LATB then immediately clear TRISB, but it did not work.
What is the problem ?
 
The for-loop is so fast that you will not see anything. Add (100 ms) delay after "PORTB=z;".
EDIT: Of course you should see the port value changing in the simulator..
 
Something you are doing..... It works fine for me..
 

Attachments

  • Capture.png
    38.9 KB · Views: 162
I think the problem is with the register display, because I wrote another simpler program (PORTB = 0x03), and I was not able to see the this value in portb, but when I simulate the program using proteus isis, it works correctly.
So I think it is a problem with the debugging in MPLAB. what do you think ?
Note that I was debugging step by step, so there is no need for a delay.
 
Another question.
How can I specify where the program should be in the ROM using C, C18 compiler ? like when we write ORG 0x100 in assembly.
 
In C you shouldn't need to... The compiler takes care of any situation...
 
as stated earlier: PORTB = input,. LATB=output,.

your code:

void main(void)
{
unsigned char z;
TRISB = 0;
for (z = 0; z <255; z++)
{
PORTB = z;
}
while(1);
}

Proper usage:
void main(void)
{
unsigned char z;
TRISB = 0;
for (z = 0; z <255; z++)
{
LATB = z;
}
while(1);
}

you could even do:

void main(void)
{
TRISB = 0;
for (LATB = 0;LATB <255; LATB++) {}
while(1){};
}
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…