# Simple but frustrating C18 code!

Status
Not open for further replies.

#### Wond3rboy

##### Member
Hi i wanted to create a 16 bit unsigned int in to an 8 bit value so i did a test of anding the Lower byte and then giving it on PORT A(LATCH obviously). I ran it in MPLAB SIM, the code is correct(checked the disassembly code) but even thought there is a clear command to show the number on LATCHA it isnt showing the output.

Code:
#include<p18f1320.h>
void main(void)
{
unsigned int j=0xff02;
unsigned char i;
TRISA=0;
i=(j & 0xff);
LATA=i;
LATAbits.LATA4=(j & 0x01);
}

Last edited:

#### Wond3rboy

##### Member
Got it working on LATB.Though why it didnt display on LATA, i mean except the MCLR pin(4) it should be displaying the o/p?

#### Pommie

##### Well-Known Member
It didn't work because PORTA was still digital and the last instruction will have done a RMW and reset all the bits to zero. Try adding ADCON1=0x7f to your code.

Also, try,
i=j;
and
i=(char)j;

BTW, it is not a good idea to let main terminate. Add a while(1); at the end of main.

Mike.

#### Wond3rboy

##### Member
It works now.Thanks.What if one wants to set any char to the upper part of the integer with out rotating it.

Last edited:

#### Pommie

##### Well-Known Member
The most efficient way in C18 is chr=integer/256;

Using chr=integer>>8; uses 11 instructions, the above is only 2.

C18 isn't very good at optimization.

Mike.

Status
Not open for further replies.