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 support@ccsinfo.com

32bit value

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
stma



Joined: 16 Feb 2004
Posts: 26

View user's profile Send private message

32bit value
PostPosted: Fri May 20, 2005 5:17 am     Reply with quote

Hi,
I am trying to read in some 8bit values from a Xilinx CPLD. I have no problem reading in the individual bytes and displaying them. What I would like to do is convert the four bytes into a 32 bit integer. The code below only displays 00000000. The individual parts display fine as seen in the second for loop. Im sure its something basic so I apologise in advance but any help is greatly appreciated. Pic is a 16F877A

Code:
#include "C:\MYRTLEIII\MLog3_test2\Mlog3_test2.h"
#byte porta=5
#byte portb=6
#byte portc=7
#byte portd=8
#byte porte=9

void read1();
void read2();
void ShutDown();



#int_EXT
EXT_isr()
{
   read1();
   //read2();
}

void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   enable_interrupts(INT_EXT);
   enable_interrupts(GLOBAL);


   set_tris_d(0xff);
   set_tris_e(0x00);
   set_tris_a(0x00);

   porta=0x00;


   ShutDown();

}



void read1()
{
   int n;
   int8 in;
   int32 pressure=0;

   porta=0x01; //chooses one source from another

// problems with this below

   for(n=4;n!=0;n--)
   {

      porte=n-1; //select bits for individual bytes from cpld
      in=portd;
      pressure=((pressure<<(8*n)) & in);
     
   }

   printf("%lx ",pressure);

//////////////////////////////////

//This below works fine

   for(n=8;n!=4;n--)
   {
      porte=n-1;
      in=portd;
      printf("%2x",in);
   }

   printf("\n\r");



   return;
}




void ShutDown()
{
   printf("\n\r!!SLEEP!! \n\r");
   while(TRUE)
   {
     porta=0x00;
     sleep();
   }
}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri May 20, 2005 5:27 am     Reply with quote

replace
Code:
      pressure=((pressure<<(8*n)) & in);

by
Code:
      pressure= (pressure<<8) | in);
stma



Joined: 16 Feb 2004
Posts: 26

View user's profile Send private message

PostPosted: Fri May 20, 2005 5:53 am     Reply with quote

Thanks a lot. Works perfectly. (and makes sense when I think about it now!!)
Ttelmah
Guest







PostPosted: Fri May 20, 2005 6:06 am     Reply with quote

As a alternative way of doing it, which will be faster, consider this:
Code:

void read1()
{
   int n;
   union {
      int32 pressure;
      int8 b[4];
   } pval;
   porta=0x01; //chooses one source from another
   for (n=4;n!=0;) {
      porte=--n;
      pval.b[n]=portd;
  }
  printf("%lx ",pval.pressure;
}

This just writes the inputs directly into the bytes, instead of rotating the 32bit word multiple times.

Best Wishes
Guest








PostPosted: Mon May 23, 2005 12:58 am     Reply with quote

Thanks very much,
I'll give it a go.
Cheers
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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