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.

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
    MPLAB.jpg
    232 KB · Views: 193
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..
 
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.
 
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.

Latest threads

New Articles From Microcontroller Tips

Back
Top