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

Rotate_L_R

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



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

Rotate_L_R
PostPosted: Tue Dec 23, 2014 6:14 am     Reply with quote

Sorry if this is off topic,but I can't find how to rotate left, right more than one bit position in byte?
Thanks, Smile
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Dec 23, 2014 6:19 am     Reply with quote

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: 345

View user's profile Send private message

PostPosted: Tue Dec 23, 2014 6:43 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Tue Dec 23, 2014 7:11 am     Reply with quote

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 Very Happy Very Happy Very Happy
kmp84



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

PostPosted: Tue Dec 23, 2014 7:32 am     Reply with quote

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 Very Happy Very Happy Very Happy


Can you put your "professional example" Smile
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Dec 23, 2014 11:15 am     Reply with quote

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: 19244

View user's profile Send private message

PostPosted: Tue Dec 23, 2014 11:59 am     Reply with quote

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: 345

View user's profile Send private message

PostPosted: Tue Dec 23, 2014 2:25 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Dec 23, 2014 2:56 pm     Reply with quote

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: 345

View user's profile Send private message

PostPosted: Tue Dec 23, 2014 3:32 pm     Reply with quote

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!! Smile
kmp84



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

PostPosted: Tue Dec 23, 2014 3:35 pm     Reply with quote

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 Smile
kmp84



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

PostPosted: Tue Dec 23, 2014 3:42 pm     Reply with quote

Thanks for all help about this topic.Smile
"asmboy" : the CCS manual is not marvel ! Smile
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