i can't assig variable to port

Status
Not open for further replies.

islinux

New Member
hi
i have a problem with my PIC18f4550
the simulator result is as required
but the hardware isn't
Code:
       float r ;
	float temp;
	float P_R[3] = {-17,7,10};	

	ADCON1 = 0xFF;	
	TRISD = 0x00;
	TRISB = 0x00;

			temp = (1.4+P_R[0])/30;
			r = pow( 10,temp );
			r = 1/r;
			
			a = (unsigned short) r;     //a = 3
			b = (unsigned short)((r-a)*10);   //b = 3
			
			PORTB = 5;
			PORTD = b;

the result on PORTB is exactly as simulator
i tried to assign the variable a to PORTB but it didn't work
it works as follows
Code:
           a = 3;
           PORTB = a;
but i need to get the result from equation
the same for PORTD

 
Looks like a problem with type conversion, an unsigned short is 2 bytes, the port's expecting one byte.
 
i tried to use
unsigned char
to match the PORTB type in "p18f4550.h"
Code:
/*p18f4550.h*/
extern volatile near unsigned char       PORTB;

but nothing changed
 
"char" ought to work, did you define it at the top like you did with the floating point variables?
 
Have you tried doing PORTB = (unsigned char) a;
Or define a as unsigned char.

If that doesn't work we need a better description of the problem. "but it didn't work" doesn't really help.

Mike.
 
Do you have a debugger? Break on the variable assignment and check the value of the variable. Check the PORTB value. Step over the assignment and check what the new value of PORTB is.
 
guys
the problem lies in hardware
when i assign value to PORTx directly or through variable
like
Code:
int a = 6;
PORTB = 5;
/* or */
PORTB = a;
both work in simulator and hardware
but the calculated value (from equation ) works in simulator only

Pommie:
i tried unsigned char but nothing happened.
 
As I said,
"If that doesn't work we need a better description of the problem. "but it didn't work" doesn't really help."

How can the calculated value only work in the simulator? What is different? does 2 become 3? How is it wrong?

Mike.
 
the calculated value displayed as ZERO on the seven segment

Then "a" contained ZERO or your hardware doesn't work. Now work out how that can be. Try writing different values to the port and work out where the problem lays.

If you still can't work it out then post all your code and your schematic.

Mike.
 
I was playing with a duel seven segment display when I run the program in sim it worked fine load the pic and it wouldn't lite ZERO two or three but the rest worked fine It was hardware not working I had F and G wired wrong
 
Islinux, this wouldn't be on the Sourceboost compiler, would it? You haven't told us, and I can't tell by your posts.

If it is, then don't use PORTB = a; use portb =a;
 
Islinux, this wouldn't be on the Sourceboost compiler, would it? You haven't told us, and I can't tell by your posts.

If it is, then don't use PORTB = a; use portb =a;

That would have given an error in BoostC as PORTB isn't a legal L-value. It's like writing 5=a;

The upper/lower case stuff in boostC is a little confusing.

Mike.
 
Last edited:
Hi
be80be:
you mean that the code worked in hardware??

Pommie:
i tried this code with my circuit and it works fine
so i think the problem not in my hardware
Code:
void main(){
	char a=1,b=2;
	ADCON1 = 0xFF;	
	TRISB = 0x80;
	TRISD = 0x00;		
	PORTB = a;
	PORTD = b;
	while(1){
		if(!PORTBbits.RB7){
			Delay1KTCYx(0);
			TRISB = 0x00;
			a = a+1;
			b = b+1;
			PORTB = a;
			PORTD = b;
			TRISB = 0x80;
		}	
	}			
}
 
Then the problem is in your code. Why don't you post your circuit and all your code so we have a chance of ......... oh, forget it.

Mike.
 
I was outputting readings from a LDR to a seven segment display when I ran my program
in mplab sim It worked fine. But my hardware I couldn't get a zero it would read from 1 and up after that I test the hardware first then the code.
 
here the whole source code
Code:
#include <p18f4550.h>
#include <math.h>
#include <timers.h>
#include <adc.h>

#pragma config WDT = OFF


void main(){
	
	char a,b;
	float r ;
	float temp;
	int ReadPR;
	float sampledPR;

	ADCON1 = 0x0E;	
	TRISD = 0x00;
	TRISB = 0x80;								

	while(1){
		if(!PORTBbits.RB7){	
			TRISB = 0x00;					
			/*config A/D*/
			OpenADC(ADC_FOSC_4 &
					ADC_RIGHT_JUST &
					ADC_2_TAD,
					ADC_CH0 &
					ADC_INT_OFF &
					ADC_VREFPLUS_VDD,
					0x0E);
			
			/*wait for acquisition time*/
			Delay1KTCYx(1);
			
			/*start A/D conversion*/
			ConvertADC();
			
			/*wait till the conversion completed*/
			while(BusyADC());
			
			/*
			  conversion finished
			  read the converted value
			*/
			ReadPR = ReadADC();
					
			sampledPR = 5*(ReadPR/1023);
			
			temp = (1.4+sampledPR)/30;			
			r = pow( 10,temp );
			r = 1/r;								
			
			a = (char) r;
			b = (char)((r-a)*10);
			
			PORTB =a; 
			PORTD =b; 																			
		TRISB = 0x80;
		CloseADC();
		}		
	}			
}
 
Last edited:
How does the compiler know which analogue pin to use? Don't you need to call SetChanADC?

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…