CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Please help me with 16 bit problem
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

Please help me with 16 bit problem
PostPosted: Sun Feb 17, 2008 9:49 am     Reply with quote

I have a 16 bit unsigned long int (as a variable t. so t=16 bit unsigned long int )
and I don't know how I can write this value to two ports.

Using PIC 18f4525
#use fast_io(c)
#use fast_io(d)
set_tris_c(0x00);
set_tris_d(0x00);

Please help

Carl
meereck



Joined: 09 Nov 2006
Posts: 173

View user's profile Send private message

PostPosted: Sun Feb 17, 2008 11:48 am     Reply with quote

considering you want to write lower 8 bits to PORT C and upper 8 bits to PORT D:
unsigned int16 value= 0x77FF;
output_C(value & 0xFF); // mask
output_D(value >> 8); // shift left
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

16 bit
PostPosted: Sun Feb 17, 2008 12:20 pm     Reply with quote

Thanks for replying.

My "value" is a variable called t - (from 0 to 65536)
so i tried your code and changed the value to t:

unsigned long int t;
t= 0x77FF;
output_C(t & 0xFF); // mask
output_D(t >> 8); // shift left

But i have no leds connected to the output to see if 't' is correct.
what should i see??
I have measured voltage with meter but no changes.
Could you also explain the code please - apart from 't' are the hex numbers addresses to write to or actual numbers??

Thanks for your help
Carl
I am using the picdem2 board (v2002)
meereck



Joined: 09 Nov 2006
Posts: 173

View user's profile Send private message

PostPosted: Sun Feb 17, 2008 12:55 pm     Reply with quote

I have just checked the outcome and it worked for me.
You can also try the following:
unsigned int16 value= 0x77FF;
unsigned int8 tempvar;
tempvar=value & 0xFF;
output_C(tempvar);
tempvar=value >> 8;
output_D(tempvar);

Explanation of the code:
value & 0xFF : since 0xFF means b'11111111', the outcome of the function is that only the lower 8 bits of the value remain.

value >> 8 : shifts value by 8 bits to left so that the lower 8 bits disappear and the upper 8 bits remain. In other words, the upper 8 bits replace the lower 8 bits.
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

16 bit
PostPosted: Sun Feb 17, 2008 1:01 pm     Reply with quote

Thankyou so much meereck. I will try the code tonight. where does my "t" go though in your code??

Carl
meereck



Joined: 09 Nov 2006
Posts: 173

View user's profile Send private message

PostPosted: Sun Feb 17, 2008 1:07 pm     Reply with quote

unsigned int16 value= 0x77FF;
that is the variable I put to output ports.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 17, 2008 1:08 pm     Reply with quote

Quote:
output_D(t >> 8); // shift left

value >> 8 : shifts value by 8 bits to left

This is not a "shift left" operator. It's a right shift.
http://www.cs.cf.ac.uk/Dave/C/node13.html
http://irc.essex.ac.uk/www.iota-six.co.uk/c/e5_bitwise_shift_operators.asp
http://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/bit_shift.html
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

16 bit
PostPosted: Sun Feb 17, 2008 1:12 pm     Reply with quote

So your variable is 0x77FF, My variable is t,

Can i then just put
output_C(t & 0xFF); // mask
output_D(t >> 8); // shift left

or does it have to different

Carl
meereck



Joined: 09 Nov 2006
Posts: 173

View user's profile Send private message

PostPosted: Sun Feb 17, 2008 1:13 pm     Reply with quote

PCM programmer wrote:
Quote:
output_D(t >> 8); // shift left

value >> 8 : shifts value by 8 bits to left

This is not a "shift left" operator. It's a right shift.
http://www.cs.cf.ac.uk/Dave/C/node13.html
http://irc.essex.ac.uk/www.iota-six.co.uk/c/e5_bitwise_shift_operators.asp
http://www.space.unibe.ch/comp_doc/c_manual/C/CONCEPT/bit_shift.html

my apologize, thanx for clarifying.
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

16 bit
PostPosted: Sun Feb 17, 2008 1:15 pm     Reply with quote

Thanks to all,

I have to go now - although i really don't want to. Please could you just show me how the t is included,

Upmost thanks

Carl
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

16 bit
PostPosted: Sun Feb 17, 2008 4:47 pm     Reply with quote

First of all thankyou both for all your asistance in this problem. I have been learning as I'm going along.

The code I have tried is:

output_C(t & 0xFF); // mask
output_D(t >> 8); // shift right

(where t is a 16 bit value)


Now because the majority of pins are floating at the moment I am not getting anything out of the ports.Tomorrow i will wire up some LED's (with resistors) from the output pins to pos. Hopefully if t=65536 then all the pins should be high - well thats what i'm hoping.

THanks again for all your help, as well as the links.

Carl
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 17, 2008 4:54 pm     Reply with quote

Quote:
Now because the majority of pins are floating at the moment

Always post a complete test program, with #include, #fuses, and #use
statements, and a main(). It should be a compilable program, that
can be compiled with no errors. You'll get better and quicker help from
this forum if you do this. Also post your compiler version.
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

16 bit
PostPosted: Sun Feb 17, 2008 5:14 pm     Reply with quote

OK here is my program so far. It doesn't include all the branches, just the ones that this discussion is on.

Main()
code:
Code:
#include <18F4525.h>
#include <math.h>
#include <kbd.c>
#include <lcd.c>
#include <menu.c>
#include <rampup.c>
#include <rampdown.c>
#include <steady.c>
#include <stepup.c>
#include <stepdown.c>
#fuses NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000) 
#use fast_io(a)     
#use fast_io(b)
#use fast_io(c)     
#use fast_io(d)
#use fast_io(e)
     
void main() {
 menu_init();
while (1);
}


Menu_init()
Code:
Code:
#use delay(clock=4000000)
void rampup_init(void);
void rampdown_init(void);
void steady_init(void);
void stepup_init(void);
void stepdown_init(void);
void menu_init(){
char k;
   port_b_pullups(TRUE);
   lcd_init();
   kbd_init();
loop:
   lcd_putc("\fPlease choose\n");
   lcd_putc("Mode");

while (TRUE) {
      k=kbd_getc();
      if(k!=0)
        if(k=='1'){
          lcd_putc("\fFor Steady State\n");
        lcd_putc("Press enter");
while (TRUE) {
      k=kbd_getc();
      if(k!=0)
        if(k=='2'){
          lcd_putc("\fSteady State\n");
        lcd_putc("Chosen");
        delay_ms(1500);
          steady_init();
/////////////////////////////////////goto Steady State
   }         
      else if(k=='1'){
          lcd_putc("\fFor Ramp Up/Down\n");
        lcd_putc("Press enter");
while (TRUE) {
      k=kbd_getc();
      if(k!=0)
        if(k=='2'){
          lcd_putc("\fRamp Up/Down\n");
        lcd_putc("Chosen");
        delay_ms(1500);
         rampup_init();
/////////////////////////////////////goto Ramp Up/Down
            }
      else if(k=='1'){
          lcd_putc("\fFor Ramp Down/Up\n");
        lcd_putc("Press enter");
while (TRUE) {
      k=kbd_getc();
      if(k!=0)
        if(k=='2'){
          lcd_putc("\fRamp Down/Up\n");
        lcd_putc("Chosen");
        delay_ms(1500);
        rampdown_init();
/////////////////////////////////////goto Ramp Down/Up
            }
      else if(k=='1'){
          lcd_putc("\fFor Step Up/Down\n");
        lcd_putc("Press enter");
while (TRUE) {
      k=kbd_getc();
      if(k!=0)
        if(k=='2'){
          lcd_putc("\fStep Up/Down\n");
        lcd_putc("Chosen");
        delay_ms(1500);
          stepup_init();
/////////////////////////////////////goto Step Up/Down
            }
      else if(k=='1'){
          lcd_putc("\fFor Step Down/Up\n");
        lcd_putc("Press enter");
while (TRUE) {
      k=kbd_getc();
      if(k!=0)
        if(k=='2'){
          lcd_putc("\fStep Down/Up\n");
        lcd_putc("Chosen");
        delay_ms(1500);
          stepdown_init();
/////////////////////////////////////goto Step Up/Down
            }
      else if(k=='1')
          goto loop;
}
}
}
}
}
}
}
}
}
}
}
}

Steady_init
Code:
Code:
#use delay(clock=4000000)
#use fast_io(a)     
#use fast_io(b)
#use fast_io(c)     
#use fast_io(d)
#use fast_io(e)
void steady_init(){
int var1,var2,var4,var5,var6;
unsigned long int r,s,t;
char k,d,e,f,g,h,j,p,q;
p=7;
q=13;
loop3:
    port_b_pullups(TRUE);
    lcd_init();
    kbd_init();
    lcd_putc("\St    0123456789\n"); 
    lcd_gotoxy(9,2);           
    lcd_putc("**.***mA");   
   lcd_gotoxy(7,1);       
while (TRUE) {
      k=kbd_getc(); 
      if(k!=0)     
       if(k=='6'){ 
         lcd_gotoxy(++p,1); }        
    else if(k=='4'){
          lcd_gotoxy(--p,1); }
    else if(k=='2'){
   d=e=f=g=h=j=lcd_getc(p,1);
     switch  (d)
{
case '0': lcd_gotoxy(9,2);           
          lcd_putc("0*.***mA");
        var1=0;
        break;         
case '1': lcd_gotoxy(9,2);           
          lcd_putc("1*.***mA");
        var1=1;
        break;             
case '2': lcd_gotoxy(9,2);           
          lcd_putc("2*.***mA");
         var1=2;
        break;
default:  goto loop3;
                  }     
while (TRUE) {
      k=kbd_getc(); 
      if(k!=0)     
       if(k=='6'){ 
         lcd_gotoxy(++p,1); }        
    else if(k=='4'){
          lcd_gotoxy(--p,1); }
    else if(k=='2'){
   d=e=f=g=h=j=lcd_getc(p,1);
     switch  (e)   
{
case '0': lcd_gotoxy(10,2);           
          lcd_putc("0.***mA");
        var2=0;
        break;         
case '1': lcd_gotoxy(10,2);           
          lcd_putc("1.***mA");
        var2=1;
        break;             
case '2': lcd_gotoxy(10,2);           
          lcd_putc("2.***mA");
        var2=2;
        break;           
case '3': lcd_gotoxy(10,2);           
          lcd_putc("3.***mA");
        var2=3;
        break;             
case '4': lcd_gotoxy(10,2);           
          lcd_putc("4.***mA");
        var2=4;
        break;             
case '5': lcd_gotoxy(10,2);           
          lcd_putc("5.***mA");
         var2=5;
        break;             
case '6': lcd_gotoxy(10,2);           
          lcd_putc("6.***mA");
         var2=6;
        break;           
case '7': lcd_gotoxy(10,2);           
          lcd_putc("7.***mA");
        var2=7;
        break;           
case '8': lcd_gotoxy(10,2);           
          lcd_putc("8.***mA");
        var2=8;
        break;           
case '9': lcd_gotoxy(10,2);           
          lcd_putc("9.***mA");
          var2=9;
        break;      }

//      if (var1==2)&&(var2>0){
  //    goto loop3;
  //    break;}

while (TRUE) {
      k=kbd_getc(); 
      if(k!=0)     
       if(k=='6'){ 
         lcd_gotoxy(++p,1); }        
    else if(k=='4'){
          lcd_gotoxy(--p,1); }
    else if(k=='2'){
   d=e=f=g=h=j=lcd_getc(p,1);
     switch  (f)   
{
case '0': lcd_gotoxy(12,2);           
          lcd_putc("0**mA");
        var4=0;
        break;         
case '1': lcd_gotoxy(12,2);           
          lcd_putc("1**mA");
        var4=1;
        break;           
case '2': lcd_gotoxy(12,2);           
          lcd_putc("2**mA");
        var4=2;
        break;           
case '3': lcd_gotoxy(12,2);         
          lcd_putc("3**mA");
          var4=3;
        break;             
case '4': lcd_gotoxy(10,2);           
          lcd_putc("4**mA");
          var4=4;
        break;             
case '5': lcd_gotoxy(12,2);           
          lcd_putc("5**mA");
          var4=5;
        break;             
case '6': lcd_gotoxy(12,2);           
          lcd_putc("6**mA");
          var4=6;
        break;           
case '7': lcd_gotoxy(12,2);           
          lcd_putc("7**mA");
          var4=7;
        break;           
case '8': lcd_gotoxy(12,2);           
          lcd_putc("8**mA");
          var4=8;
        break;           
case '9': lcd_gotoxy(12,2);           
          lcd_putc("9**mA");
          var4=9;
        break;      } 
while (TRUE) {
      k=kbd_getc(); 
      if(k!=0)     
       if(k=='6'){ 
         lcd_gotoxy(++p,1); }        
    else if(k=='4'){
          lcd_gotoxy(--p,1); }
    else if(k=='2'){
   d=e=f=g=h=j=lcd_getc(p,1);
     switch  (g)   
{
case '0': lcd_gotoxy(13,2);           
          lcd_putc("0*mA");
          var5=0;
        break;         
case '1': lcd_gotoxy(13,2);           
          lcd_putc("1*mA");
        var5=1;
        break;             
case '2': lcd_gotoxy(13,2);           
          lcd_putc("2*mA");
        var5=2;
        break;           
case '3': lcd_gotoxy(13,2);           
          lcd_putc("3*mA");
          var5=3;
        break;             
case '4': lcd_gotoxy(13,2);           
          lcd_putc("4*mA");
          var5=4;
        break;             
case '5': lcd_gotoxy(13,2);           
          lcd_putc("5*mA");
          var5=5;
        break;             
case '6': lcd_gotoxy(13,2);           
          lcd_putc("6*mA");
          var5=6;
        break;           
case '7': lcd_gotoxy(13,2);           
          lcd_putc("7*mA");
          var5=7;
        break;           
case '8': lcd_gotoxy(13,2);           
          lcd_putc("8*mA");
          var5=8;
        break;           
case '9': lcd_gotoxy(13,2);           
          lcd_putc("9*mA");
          var5=9;
        break;      } 
while (TRUE) {
      k=kbd_getc(); 
      if(k!=0)     
       if(k=='6'){ 
         lcd_gotoxy(++p,1); }        
    else if(k=='4'){
          lcd_gotoxy(--p,1); }
    else if(k=='2'){
   d=e=f=g=h=j=lcd_getc(p,1);
     switch  (h)   
{
case '0': lcd_gotoxy(14,2);           
          lcd_putc("0mA");
          var6=0;
        break;         
case '1': lcd_gotoxy(14,2);           
          lcd_putc("1mA");
          var6=1;
        break;             
case '2': lcd_gotoxy(14,2);           
          lcd_putc("2mA");
          var6=2;
        break;           
case '3': lcd_gotoxy(14,2);           
          lcd_putc("3mA");
          var6=3;
        break;             
case '4': lcd_gotoxy(14,2);           
          lcd_putc("4mA");
          var6=4;
        break;             
case '5': lcd_gotoxy(14,2);           
          lcd_putc("5mA");
          var6=5;
        break;             
case '6': lcd_gotoxy(14,2);           
          lcd_putc("6mA");
          var6=6;
        break;           
case '7': lcd_gotoxy(14,2);           
          lcd_putc("7mA");
          var6=7;
        break;           
case '8': lcd_gotoxy(14,2);           
          lcd_putc("8mA");
          var6=8;
        break;           
case '9': lcd_gotoxy(14,2);           
          lcd_putc("9mA");
          var6=9;
        break;      }
lcd_gotoxy(1,1);
lcd_putc("\St Value OK? Y/N\n"); 
lcd_gotoxy(13,1);               

while (TRUE) {
     
      k=kbd_getc(); 
      if(k!=0)     
       if(k=='6'){ 
         lcd_gotoxy(++q,1); }        
    else if(k=='4'){
          lcd_gotoxy(--q,1); }
    else if(k=='2'){
   d=e=f=g=h=j=lcd_getc(q,1);
     switch  (j)   
{
case 'N': goto loop3;

case 'Y': lcd_gotoxy(1,1);           
          lcd_putc("St Running!      ");
        break;   
default : goto loop3; }   
set_tris_c(0x00);
set_tris_d(0x00);
r=((var1*10000)+(var2*1000)+(var4*100)+(var5*10)+(var6*1));
s=(r-4000);
t=(s*(65536/16000));                     
output_C(t & 0xFF); // mask
output_D(t >> 8); // shift right



//if (t==0){    
//lcd_gotoxy(1,1);   
//lcd_putc("000000000"); }
//else if (t!=0){
//lcd_gotoxy(1,1);   
//lcd_putc("11111111");
//break;}

///printf(lcd_putc,"%,%,%,%,%,%\n",var1,var2,var3,var4,var5,var6);

}
}
}
}
}
}
}
}
}
}
}
}
}


and as we can see near the bottom is the point in question:

output_C(t & 0xFF); // mask
output_D(t >> 8); // shift right

This is the end of one branch of this project which is designed to output a static 16bit parallel value over 2 ports. will conect the led's to,morrow and see if it works.

Carl
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 17, 2008 5:37 pm     Reply with quote

That's not a test program. That's your entire program. A test program
is a small program that tests only the problem area. You said you had
a problem with the output pins floating, even though you had set the TRIS
to "all outputs", and were writing values to the ports.

The program below tests this problem. That's all it does. If it doesn't
work, then you look closely at the program for errors. If you don't see
any, then you look at the .LST file for problems. If it looks OK, then
look at your board for problems. If there's no problem with the board,
then you look at the Errata documentation from Microchip for the PIC.
And so on.

Now, looking at your code below, the first thing I see is that there is no
oscillator fuse. With no fuse, the compiler sets the oscillator config bits
to use the LP oscillator. But that's for a 32.768 KHz watch crystal.
I doubt that it will run a 4 MHz crystal. So the PIC will never start running
and the outputs will remain in the power-up state, which is floating.
Problem solved. Add the XT fuse.

That's why a test program is so important. It's small enough so that
you are forced to concentrate on the problem, and not get lost in
hundreds of lines of code.
Code:

#include <18F4525.h>
#fuses NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000) 

#use fast_io(c)     
#use fast_io(d)

unsigned long int t;

//=====================
void main()
{
set_tris_c(0x00);
set_tris_d(0x00);

t = 0x55AA;

output_C(t & 0xFF);
output_D(t >> 8);

while(1);
}
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

16 bit
PostPosted: Mon Feb 18, 2008 2:53 am     Reply with quote

Ok I understand the posting procedure now. Thanks PCM Programmer, I will do it like this from now on. I will try out several things tonight and hope that it works.

Carl
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group