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.

trouble with C18 compiler

Status
Not open for further replies.

Rickt

New Member
Hey,
I'm new to this forum, but hopefully you guys can help me with some questions.
I'm programming a pic18f4550 microcontroller using MPLAB and the C18 C compiler. Im trying to drive a 4 digit 7-seg display, but this C18 compiler is doing some weird things that i don't understand. The MPLAB SIM simulates the code properly, it's only when i run the program on the chip that i get these strange problems. I'm hoping i forgot to check a little box in some obscure setting option somewhere

here is an example of an issue i'm having:
It only seems to properly output to the PORT pins the unsigned char data type (never had this issue with other compilers such as hi-tech C or AVR compilers)-

int x = 8;
PORTB = x; (doesn't produce proper output on the pins)

unsigned char x = 8;
PORTB = x (correct output)

i figure this is because the char data type is 8 bits (same as the PORTB of this PIC) and the int data type is 16 bits? I'd like to manipulate data using int data type and output that to the port directly rather than using chars, and i've tried type casting and it doesn't work- seems to just ignore the type casting. anyone know why?

I've got other baffling problems that i've never had before using other compilers or other microcontrollers, but i figure i'll start small for now. I'd like to be able to use the C18 compiler though because everyone else seems to use it and think its great. So far, i find the compiler very quirky and unpredictable.
any solutions, clues or hints would be appreciated...
Thanks a lot
Rick
ps again, perhaps it's some MPLAB/C18 setting that i didn't setup properly?
 
I just ran this,
Code:
void main(void){
int x=8;
    OSCCON=0x70;        //8 Meg
    ADCON1=0x7f;

    TRISB=0;
    PORTB=x;
and I ended up with 0x88 in PORTB. The top bit being set because I'm using a pickit2. This was on an 18F1320.

Edit, I'm assuming you have turned off the analogue functions. This can be done in the config on that chip.

Mike.
 
Last edited:
Thanks Mike for your reply,

Your code did work, but when i tried to use what you had to make a slightly more complex program, i got some weird results. I'd like to output the numbers 1,2, and then 3 to PORTB on the pic18f4550 using the C18 compiler. When i run the code on the chip, it gives me these values:
for 1: 0b11010010
for 2: 0b11010000
for 3: 0b11010011


[I'm also using a PICKIT 2 to program the chip, so when i switch to PORTD the upper 4 bits are all 0 for each number, but the lower 4 bits are the same and still not what i want]

why doesn't it output the right numbers?


Here is my full code:
Code:
#include <p18f4550.h>
#include <delays.h>
#include <math.h>

#pragma config WDT = OFF
#pragma config PBADEN = OFF

void main(void){
int x = 1;
int y = 2;
int z = 3;
    OSCCON=0x70;        //8 Meg
    ADCON1=0x7f;		

    TRISB=0;
	while(1){
		PORTB=x;  			//set PORTB to 1
   		Delay10KTCYx(50);	//Delay	
		PORTB = y;			//Set PORTB to 2
		Delay10KTCYx(50);	//Delay
		PORTB = z;			//Set PORTB to 3
		Delay10KTCYx(50);	//Delay
	} //end while(1)  	
}
}
 
IT WORKS!

It was the debug configuration bit that must have been giving me trouble.
Though i don't remember setting it high, it must have been set somehow, and stayed that way until i unset it just now. sweet! works beautifully now. i knew my problem must have been something small like this!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top