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 CCS Technical Support

Shift a byte with carry to another

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



Joined: 11 Oct 2017
Posts: 142

View user's profile Send private message

Shift a byte with carry to another
PostPosted: Sun Jul 25, 2021 6:08 am     Reply with quote

I'm using 8x8 modules from China that are already connected to each other (4).
I want to do the scroll effect, but these modules have different connections.
I came up with a simple solution, but I depend on some expressions.
How do I shift a byte to the left, take the MSB bit of another byte, along with the shifted first, and shift the other one?

I hope you understand me, English is not my first language.
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Sun Jul 25, 2021 6:55 am     Reply with quote

Look at the shift_left function.,

For example:
Code:

char buffer[4];
int1 bit_to_add=1;

   buffer[0]=0x80;
   buffer[1]=0xAA;
   buffer[2]=0;
   buffer[3]=0;

   shift_left(buffer,4,bit_to_add);


This shifts the 4 bytes of 'buffer' left one bit, and shifts 'bit_to_add' into the
rightmost bit. So you would have:
0x00015501

You can specify how many bytes it is to move (this is the 4), and what is
in 'bit_to_add' gets shifted in to the bottom of the sequence..
vtrx



Joined: 11 Oct 2017
Posts: 142

View user's profile Send private message

PostPosted: Sun Jul 25, 2021 7:20 am     Reply with quote

follows a hypothetical example.

Code:

letter x:
1000 0000
1010 1010
0000 0000
1000 0000

letter y:
1000 0101
1111 1111
0101 0101
1001 1001

The expected result:

letter x:
0000 0001
0101 0101
0000 0000
0000 0001

letter y:
0000 1010
1111 1110
1010 1010
0011 0010
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Sun Jul 25, 2021 10:44 am     Reply with quote

That seems to just be showing four individual byte shifts with no
carry between the bytes and zero shifted in at the right.
There is no carry in what you show.
That should be:
Code:

char buffer[4];
int1 bit_to_add=0;

   buffer[0]=0x80;
   buffer[1]=0xAA;
   buffer[2]=0;
   buffer[3]=0x80;

   shift_left(&buffer[0],1,bit_to_add);
   shift_left(&buffer[1],1,bit_to_add);
   shift_left(&buffer[2],1,bit_to_add);
   shift_left(&buffer[3],1,bit_to_add);
vtrx



Joined: 11 Oct 2017
Posts: 142

View user's profile Send private message

PostPosted: Sun Jul 25, 2021 2:33 pm     Reply with quote

I actually need to shift right.
I got the expected result, but the routine is not optimized because it is 128 bytes.
it basically starts like this:

Code:
 
                  for(d=0;d<8;d++)
                       {
                        scroll_buf[d+16]=scroll_buf[d+16]>>1;
                        if(bit_test(scroll_buf[d+8],0)==1){bit_set(scroll_buf[d+16],7);}
                        scroll_buf[d+8]=scroll_buf[d+8]>>1;
                        if(bit_test(scroll_buf[d],0)==1){bit_set(scroll_buf[d+8],7);}
                        scroll_buf[d]=scroll_buf[d]>>1;
                       }


Code:
letter x:       letter y:       result x:   result y:
1000 0000  ->   1000 0101   =   0100 0000   0100 0100
1010 1010  ->   1111 1111   =   0101 0101   0111 1111
0000 0000  ->   0101 0101   =   0000 0000   0010 1010
1000 0000  ->   1001 1001   =   0100 1000   0100 1100
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 25, 2021 3:00 pm     Reply with quote

Your coding efficiency is not the greatest.
Quote:
scroll_buf[d+16]=scroll_buf[d+16]>>1;
The line above (from your program) creates this list file code:
Code:

.................... scroll_buf[d+16]=scroll_buf[d+16]>>1;
000C2:  MOVLW  10
000C4:  ADDWF  d,W
000C6:  CLRF   @03
000C8:  ADDLW  scroll_buf
000CA:  MOVWF  @01
000CC:  MOVLW  scroll_buf+-5
000CE:  ADDWFC @03,F
000D0:  MOVFF  03,@@57
000D4:  MOVLW  10
000D6:  ADDWF  d,W
000D8:  CLRF   @03
000DA:  ADDLW  scroll_buf
000DC:  MOVWF  FSR0L
000DE:  MOVLW  scroll_buf+-5
000E0:  ADDWFC @03,W
000E2:  MOVWF  FSR0H
000E4:  BCF    STATUS.C
000E6:  RRCF   INDF0,W
000E8:  MOVFF  @@57,FSR0H
000EC:  MOVFF  01,FSR0L
000F0:  MOVWF  INDF0


But an equivalent way to write that line is shown below:
Code:
scroll_buf[d+16] >>= 1;

It produces a much shorter (and faster) amount of ASM code:
Code:

.................... scroll_buf[d+16] >>= 1;
0013C:  MOVLW  10
0013E:  ADDWF  d,W
00140:  CLRF   @03
00142:  ADDLW  scroll_buf
00144:  MOVWF  FSR0L
00146:  MOVLW  scroll_buf+-5
00148:  ADDWFC @03,W
0014A:  MOVWF  FSR0H
0014C:  BCF    STATUS.C
0014E:  RRCF   INDF0,W
vtrx



Joined: 11 Oct 2017
Posts: 142

View user's profile Send private message

PostPosted: Sun Jul 25, 2021 4:49 pm     Reply with quote

With your tip, i'm trying to optimize the code even more, but i can't fit another loop.
Do you have an idea?

Code:
                     for(d=0;d<8;d++)
                       {                       
                        scroll_buf[d+256]>>=1;
                        if(bit_test(scroll_buf[d+248],0)==1){bit_set(scroll_buf[d+256],7);}                     
                        scroll_buf[d+248]>>=1;
                        if(bit_test(scroll_buf[d+240],0)==1){bit_set(scroll_buf[d+248],7);}
                        scroll_buf[d+240]>>=1;
                        if(bit_test(scroll_buf[d+232],0)==1){bit_set(scroll_buf[d+240],7);}
                        scroll_buf[d+232]>>=1;
                        if(bit_test(scroll_buf[d+224],0)==1){bit_set(scroll_buf[d+232],7);}
                        scroll_buf[d+224]>>=1;
                        if(bit_test(scroll_buf[d+216],0)==1){bit_set(scroll_buf[d+224],7);}
                        scroll_buf[d+216]>>=1;
                        if(bit_test(scroll_buf[d+208],0)==1){bit_set(scroll_buf[d+216],7);}
                        scroll_buf[d+208]>>=1;
                        if(bit_test(scroll_buf[d+200],0)==1){bit_set(scroll_buf[d+208],7);}
                        scroll_buf[d+200]>>=1;
                        if(bit_test(scroll_buf[d+192],0)==1){bit_set(scroll_buf[d+200],7);}
                        scroll_buf[d+192]>>=1;
                        if(bit_test(scroll_buf[d+184],0)==1){bit_set(scroll_buf[d+192],7);}
                        scroll_buf[d+184]>>=1;
                        if(bit_test(scroll_buf[d+176],0)==1){bit_set(scroll_buf[d+184],7);}
                        scroll_buf[d+176]>>=1;
                        if(bit_test(scroll_buf[d+168],0)==1){bit_set(scroll_buf[d+176],7);}
                        scroll_buf[d+168]>>=1;
                        if(bit_test(scroll_buf[d+160],0)==1){bit_set(scroll_buf[d+168],7);}
                        scroll_buf[d+160]>>=1;
                        if(bit_test(scroll_buf[d+152],0)==1){bit_set(scroll_buf[d+160],7);}
                        scroll_buf[d+152]>>=1;
                        if(bit_test(scroll_buf[d+144],0)==1){bit_set(scroll_buf[d+152],7);}
                        scroll_buf[d+144]>>=1;
                        if(bit_test(scroll_buf[d+136],0)==1){bit_set(scroll_buf[d+144],7);}
                        scroll_buf[d+136]>>=1;
                        if(bit_test(scroll_buf[d+128],0)==1){bit_set(scroll_buf[d+136],7);}
                        scroll_buf[d+128]>>=1;                       
                        if(bit_test(scroll_buf[d+120],0)==1){bit_set(scroll_buf[d+128],7);}                               
                        scroll_buf[d+120]>>=1;
                        if(bit_test(scroll_buf[d+112],0)==1){bit_set(scroll_buf[d+120],7);}                     
                        scroll_buf[d+112]>>=1;
                        if(bit_test(scroll_buf[d+104],0)==1){bit_set(scroll_buf[d+112],7);}
                        scroll_buf[d+104]>>=1;
                        if(bit_test(scroll_buf[d+96],0)==1){bit_set(scroll_buf[d+104],7);}
                        scroll_buf[d+96]>>=1;
                        if(bit_test(scroll_buf[d+88],0)==1){bit_set(scroll_buf[d+96],7);}
                        scroll_buf[d+88]>>=1;
                        if(bit_test(scroll_buf[d+80],0)==1){bit_set(scroll_buf[d+88],7);}
                        scroll_buf[d+80]>>=1;
                        if(bit_test(scroll_buf[d+72],0)==1){bit_set(scroll_buf[d+80],7);}
                        scroll_buf[d+72]>>=1;
                        if(bit_test(scroll_buf[d+64],0)==1){bit_set(scroll_buf[d+72],7);}
                        scroll_buf[d+64]>>=1;
                        if(bit_test(scroll_buf[d+56],0)==1){bit_set(scroll_buf[d+64],7);}
                        scroll_buf[d+56]>>=1;
                        if(bit_test(scroll_buf[d+48],0)==1){bit_set(scroll_buf[d+56],7);}
                        scroll_buf[d+48]>>=1;
                        if(bit_test(scroll_buf[d+40],0)==1){bit_set(scroll_buf[d+48],7);}
                        scroll_buf[d+40]>>=1;
                        if(bit_test(scroll_buf[d+32],0)==1){bit_set(scroll_buf[d+40],7);}
                        scroll_buf[d+32]>>=1;
                        if(bit_test(scroll_buf[d+24],0)==1){bit_set(scroll_buf[d+32],7);}
                        scroll_buf[d+24]>>=1;
                        if(bit_test(scroll_buf[d+16],0)==1){bit_set(scroll_buf[d+24],7);}
                        scroll_buf[d+16]>>=1;
                        if(bit_test(scroll_buf[d+8],0)==1){bit_set(scroll_buf[d+16],7);}
                        scroll_buf[d+8]>>=1;
                        if(bit_test(scroll_buf[d],0)==1){bit_set(scroll_buf[d+8],7);}
                        scroll_buf[d]>>=1;                       
                       }     
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Mon Jul 26, 2021 4:05 am     Reply with quote

You are just using an 8 element loop, and then doing this for every eighth
element. Why not just loop from 256?. So something like:
Code:

void scroll(int16 index)
{   
   scroll_buf[index] >>= 1;
   if(bit_test(scroll_buf[index-8],0)==1)
      scroll_buf[index]+=128;
}

      for(d=256;d>=0;d--)
      { 
         scroll(d);     
      }

//d has to be a signed int16 to use like this

To deal with the bottom eight elements I add a test to ensure it doesn't
start trying to pull in the carry from below zero.
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