| View previous topic :: View next topic |
| Author |
Message |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Jan 25, 2005 5:44 pm |
|
|
Well the problem is his original post
| Quote: | | hi can anyone help me please, can you please commentate the following codes? everything works now. im now able to read the voltage and hex via rs232. but i need to understand what the codes exactly do. thank you very much |
Everything is working and he was just trying to understand it. Well it now sounds like:
Everything is working per the example but I want the output in decimal form instead of hex. I tried this..... but the results were not as expected. I am using a PICXXXX and compiler version XXXX.
Got to ask the right questions to get the right answer! |
|
 |
Guest_Me Guest
|
|
Posted: Thu Jan 27, 2005 3:36 am |
|
|
hello.
i have another problem to solve.
how can i reverse the hex data. example:
3A20 to 02A3 ?
greetz |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Jan 27, 2005 7:57 am |
|
|
1. If you have converted it to a string, then just print it backwards.
2. Another option is to swap nibbles and then swap bytes. 3A20 -> A302 -> 02A3 |
|
 |
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Thu Jan 27, 2005 9:03 am |
|
|
| Guest_Me wrote: | hello.
i have another problem to solve.
how can i reverse the hex data. example:
3A20 to 02A3 ?
greetz |
This trick has been around for a while. The code below is for a 32-bit integer but you can modify it for 16-bit ints.
| Code: |
n = ((n >> 1) & 0x55555555) | ((n << 1) & 0xaaaaaaaa) ;
n = ((n >> 2) & 0x33333333) | ((n << 2) & 0xcccccccc) ;
n = ((n >> 4) & 0x0f0f0f0f) | ((n << 4) & 0xf0f0f0f0) ;
n = ((n >> 8) & 0x00ff00ff) | ((n << 8) & 0xff00ff00) ;
n = ((n >> 16) & 0x0000ffff) | ((n << 16) & 0xffff0000) ;
|
It is doing suggestion #2 from Mark's post (but on a 32-bit int). _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Jan 27, 2005 9:23 am |
|
|
Wow! Now that is an equation. Here's something a bit simplier
| Code: |
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
void main(void)
{
long int value;
long int reversed;
value = 0x3A20;
#asm
swapf value,0
movwf &reversed+1
swapf &value+1,0
movwf reversed
#endasm
while(1);
}
|
|
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Jan 27, 2005 9:39 am |
|
|
Any if you don't like asm:
| Code: |
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
void main(void)
{
long int value;
union
{
long int word;
int8 b[2];
} reversed;
value = 0x3A20;
reversed.b[1] = make8(value,0);
reversed.b[0] = make8(value,1);
swap(reversed.b[0]);
swap(reversed.b[1]);
while(1);
}
|
|
|
 |
Guest_Me Guest
|
|
Posted: Thu Jan 27, 2005 10:04 am |
|
|
hey guys... i believe, u misunderstood me..
3A20 to 02A3 ? -> thats was just an example..
when i use this ltc1298.c, the values that appear in Hyperterminal are somehow reversed....
so when it should show me 08A1 (dec. 2209) the program makes it reversed.. 1A80 (6784)... for my purpose its relevant if the programm just shows the decimal values..
i hope i've expressed myself clearly =)
thanks |
|
 |
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Thu Jan 27, 2005 11:01 am |
|
|
| Guest_Me wrote: | hey guys... i believe, u misunderstood me..
3A20 to 02A3 ? -> thats was just an example..
when i use this ltc1298.c, the values that appear in Hyperterminal are somehow reversed....
so when it should show me 08A1 (dec. 2209) the program makes it reversed.. 1A80 (6784)... for my purpose its relevant if the programm just shows the decimal values..
i hope i've expressed myself clearly =)
thanks |
Change the driver code to fill the 16-bit from the "other" end. Also you can get the data in either MSbit first or LSbit first from the LTC1298 itself by manipulating the MSBF bit in the control word or you can keep clocking more data out after the 12th bit without cycling the CS- pin at which time the LTC1298 will start sending out the last sample again but in reversed bit order. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
 |
Guest
|
|
Posted: Thu Jan 27, 2005 3:23 pm |
|
|
| Code: | | Also you can get the data in either MSbit first or LSbit first from the LTC1298 itself by manipulating the MSBF bit in the control word |
where exactly? |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Jan 27, 2005 3:34 pm |
|
|
I thought you read the datasheet?
http://www.linear.com.cn/pdf/128698fs.pdf
Look at pages 11 and 12. When the MSBF bit is set, you only get the data MSB to LSB. If MSBF is clear, then you get the data MSB to LSB and if you keep clocking you get LSB to MSB. |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Jan 27, 2005 3:41 pm |
|
|
Looking at this code
| Code: | long int read_analog( BYTE channel ) {
int l;
long int h;
delay_us(200);
output_low(ADC_CLK);
output_high(ADC_DIN);
output_low(ADC_CS);
if(channel==0)
channel=0x1b;
else
channel=0x1f;
write_adc_byte( channel, 5);
h=read_adc_byte(8);
l=read_adc_byte(4)<<4;
output_high(ADC_CS);
return((h<<8)|l);
}
|
The channel value is the config byte or input data word. The word is actually only 3 bits but the code is writing 5 bits! This is why:
The first bit is actually a start bit and should always be a 1
The next 3 bits are the data word. Two for the address and 1 for the MSBF
The last bit or 5th bit is actually just a dummy clock cycle which would be the "NULL" bit. |
|
 |
Guest
|
|
Posted: Fri Jan 28, 2005 4:42 pm |
|
|
| Quote: | | The channel value is the config byte or input data word. The word is actually only 3 bits but the code is writing 5 bits! This is why: |
where exactly in the code should i change this? |
|
 |
Guest_Me Guest
|
|
Posted: Fri Jan 28, 2005 4:52 pm |
|
|
| oh now i understand, i have to change the 1b to 1a? |
|
 |
Guest_Me Guest
|
|
Posted: Fri Jan 28, 2005 4:59 pm |
|
|
its me again =)
its not 1a but rather 19..am i right =)
but whats the 5th bit again? i can use either 1 or 0 ? |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Jan 28, 2005 11:05 pm |
|
|
Bits are clocked out LSB first. So 0x1B would be 0x13. The fifth bit is still the same as what I said before. It is the NULL bit. Read the datasheet and look at the timing diagrams and you will see it.
PS: Changing the config word isn't going to get you what you want unless you change the code to clock an additional 12 bits out which would be the reverse order. Read the datasheet, you will see it in there. |
|
 |
|