View previous topic :: View next topic |
Author |
Message |
stma
Joined: 16 Feb 2004 Posts: 26
|
32bit value |
Posted: Fri May 20, 2005 5:17 am |
|
|
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
|
|
Posted: Fri May 20, 2005 5:27 am |
|
|
replace Code: | pressure=((pressure<<(8*n)) & in);
|
by Code: | pressure= (pressure<<8) | in);
|
|
|
|
stma
Joined: 16 Feb 2004 Posts: 26
|
|
Posted: Fri May 20, 2005 5:53 am |
|
|
Thanks a lot. Works perfectly. (and makes sense when I think about it now!!) |
|
|
Ttelmah Guest
|
|
Posted: Fri May 20, 2005 6:06 am |
|
|
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
|
|
Posted: Mon May 23, 2005 12:58 am |
|
|
Thanks very much,
I'll give it a go.
Cheers |
|
|
|