|
|
View previous topic :: View next topic |
Author |
Message |
kmp84
Joined: 02 Feb 2010 Posts: 354
|
Rotate_L_R |
Posted: Tue Dec 23, 2014 6:14 am |
|
|
Sorry if this is off topic,but I can't find how to rotate left, right more than one bit position in byte?
Thanks, |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Dec 23, 2014 6:19 am |
|
|
the CCS manual is a marvel.
suggest you read about
rotate_left()
and rotate_right()
though the doc is misprinted:
the second argument is NOT bytes but number of BITS
you will rotate through
the pointed-to structure, byte or word. |
|
|
kmp84
Joined: 02 Feb 2010 Posts: 354
|
|
Posted: Tue Dec 23, 2014 6:43 am |
|
|
Code: |
void rotate_l (int8 *data_buff,int8 size)
{
int8 i;
for(i=0;i<size;i++)
{
rotate_left (&data_buff[i],i);
}
}
|
Invalid parameter to build function -> rotate_left (&data_buff[i],"i"); |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Dec 23, 2014 7:11 am |
|
|
BTW: what useful purpose does your example failed code represent?
the variable bit shift you are attempting seems nonsensical to this old timer.
ANYWAY-
the second parameter expects a constant not a variable argument.
the forum search function brings up dozens of posts that show proper useage
of the function. And the reason for constants only?
TTWIW |
|
|
kmp84
Joined: 02 Feb 2010 Posts: 354
|
|
Posted: Tue Dec 23, 2014 7:32 am |
|
|
asmboy wrote: | BTW: what useful purpose does your example failed code represent?
the variable bit shift you are attempting seems nonsensical to this old timer.
ANYWAY-
the second parameter expects a constant not a variable argument.
the forum search function brings up dozens of posts that show proper useage
of the function. And the reason for constants only?
TTWIW |
Can you put your "professional example" |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Dec 23, 2014 11:15 am |
|
|
Quote: | void rotate_l (int8 *data_buff,int8 size)
{
int8 i;
for(i=0;i<size;i++)
{
rotate_left (&data_buff[i], i);
}
}
|
The compiler doesn't like a variable for the 2nd parameter. It wants a
constant for the "number of bytes to rotate".
Also, I'm not sure what your program above is supposed to do.
The rotate_left() function will rotate the specified number of bytes in
an array, to the left by 1 bit. Tell us what you actually want to do. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Tue Dec 23, 2014 11:59 am |
|
|
First, asmboy is wrong. The manual is correct. The rotate functions rotate multiple bytes by one bit. The chip itself only has single bit rotation. If you want to rotate by larger numbers, then you just repeat the rotations.
Remember that C has the inbuilt 'shift' functions >> and <<. These accept variables. Remember too, that a rotation of 8 bits, is just a byte move (the compiler will optimise the inbuilt functions for larger shifts).
You can build your own multi-byte rotates very simply:
Code: |
#define RR(locn,bytes,bits) {for (int8 ctr=0;ctr<bits;ctr++) rotate_right(locn,bytes);}
|
Which follows the same syntax as 'rotate_right', but with an added 'bits' value. |
|
|
kmp84
Joined: 02 Feb 2010 Posts: 354
|
|
Posted: Tue Dec 23, 2014 2:25 pm |
|
|
PCM programmer wrote: | Quote: | void rotate_l (int8 *data_buff,int8 size)
{
int8 i;
for(i=0;i<size;i++)
{
rotate_left (&data_buff[i], i);
}
}
|
The compiler doesn't like a variable for the 2nd parameter. It wants a
constant for the "number of bytes to rotate".
Also, I'm not sure what your program above is supposed to do.
The rotate_left() function will rotate the specified number of bytes in
an array, to the left by 1 bit. Tell us what you actually want to do. |
I have for example buffer with 8 bytes and want to rotate_left(right) in this scheme:
byte[0]->rotate 1 bit position left(right);
byte[1]->rotate 2 bit position left(right);
and etc. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Dec 23, 2014 2:56 pm |
|
|
Quote: | I have for example buffer with 8 bytes and want to rotate_left(right) in this scheme:
byte[0]->rotate 1 bit position left
byte[1]->rotate 2 bit position left
|
The program below gives this output in MPLAB simulator:
Quote: |
02
04
08
10
20
40
80
01
|
Code: |
#include <18F4520.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
//===================================
void main()
{
int8 i;
int8 data_buff[8] = {1,1,1,1,1,1,1,1};
rotate_left (&data_buff[0], 1);
rotate_left (&data_buff[1], 1);
rotate_left (&data_buff[1], 1);
rotate_left (&data_buff[2], 1);
rotate_left (&data_buff[2], 1);
rotate_left (&data_buff[2], 1);
rotate_left (&data_buff[3], 1);
rotate_left (&data_buff[3], 1);
rotate_left (&data_buff[3], 1);
rotate_left (&data_buff[3], 1);
rotate_left (&data_buff[4], 1);
rotate_left (&data_buff[4], 1);
rotate_left (&data_buff[4], 1);
rotate_left (&data_buff[4], 1);
rotate_left (&data_buff[4], 1);
rotate_left (&data_buff[5], 1);
rotate_left (&data_buff[5], 1);
rotate_left (&data_buff[5], 1);
rotate_left (&data_buff[5], 1);
rotate_left (&data_buff[5], 1);
rotate_left (&data_buff[5], 1);
rotate_left (&data_buff[6], 1);
rotate_left (&data_buff[6], 1);
rotate_left (&data_buff[6], 1);
rotate_left (&data_buff[6], 1);
rotate_left (&data_buff[6], 1);
rotate_left (&data_buff[6], 1);
rotate_left (&data_buff[6], 1);
for(i = 0; i < 8; i++)
printf("%x \r", data_buff[i]);
while(TRUE);
}
|
|
|
|
kmp84
Joined: 02 Feb 2010 Posts: 354
|
|
Posted: Tue Dec 23, 2014 3:32 pm |
|
|
Ttelmah wrote: | First, asmboy is wrong. The manual is correct. The rotate functions rotate multiple bytes by one bit. The chip itself only has single bit rotation. If you want to rotate by larger numbers, then you just repeat the rotations.
Remember that C has the inbuilt 'shift' functions >> and <<. These accept variables. Remember too, that a rotation of 8 bits, is just a byte move (the compiler will optimise the inbuilt functions for larger shifts).
You can build your own multi-byte rotates very simply:
Code: |
#define RR(locn,bytes,bits) {for (int8 ctr=0;ctr<bits;ctr++) rotate_right(locn,bytes);}
|
Which follows the same syntax as 'rotate_right', but with an added 'bits' value. |
Thank you very much Ttelmah, the problem was solved!! |
|
|
kmp84
Joined: 02 Feb 2010 Posts: 354
|
|
Posted: Tue Dec 23, 2014 3:35 pm |
|
|
PCM programmer wrote: | Quote: | I have for example buffer with 8 bytes and want to rotate_left(right) in this scheme:
byte[0]->rotate 1 bit position left
byte[1]->rotate 2 bit position left
|
The program below gives this output in MPLAB simulator:
Quote: |
02
04
08
10
20
40
80
01
|
Code: |
#include <18F4520.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
//===================================
void main()
{
int8 i;
int8 data_buff[8] = {1,1,1,1,1,1,1,1};
rotate_left (&data_buff[0], 1);
rotate_left (&data_buff[1], 1);
rotate_left (&data_buff[1], 1);
rotate_left (&data_buff[2], 1);
rotate_left (&data_buff[2], 1);
rotate_left (&data_buff[2], 1);
rotate_left (&data_buff[3], 1);
rotate_left (&data_buff[3], 1);
rotate_left (&data_buff[3], 1);
rotate_left (&data_buff[3], 1);
rotate_left (&data_buff[4], 1);
rotate_left (&data_buff[4], 1);
rotate_left (&data_buff[4], 1);
rotate_left (&data_buff[4], 1);
rotate_left (&data_buff[4], 1);
rotate_left (&data_buff[5], 1);
rotate_left (&data_buff[5], 1);
rotate_left (&data_buff[5], 1);
rotate_left (&data_buff[5], 1);
rotate_left (&data_buff[5], 1);
rotate_left (&data_buff[5], 1);
rotate_left (&data_buff[6], 1);
rotate_left (&data_buff[6], 1);
rotate_left (&data_buff[6], 1);
rotate_left (&data_buff[6], 1);
rotate_left (&data_buff[6], 1);
rotate_left (&data_buff[6], 1);
rotate_left (&data_buff[6], 1);
for(i = 0; i < 8; i++)
printf("%x \r", data_buff[i]);
while(TRUE);
}
|
|
Thanks PCM programmer this also work |
|
|
kmp84
Joined: 02 Feb 2010 Posts: 354
|
|
Posted: Tue Dec 23, 2014 3:42 pm |
|
|
Thanks for all help about this topic.
"asmboy" : the CCS manual is not marvel ! |
|
|
|
|
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
|