|
|
View previous topic :: View next topic |
Author |
Message |
ViperARG
Joined: 07 Jan 2017 Posts: 16
|
Bitwise operators HELP |
Posted: Mon Jan 30, 2017 10:55 am |
|
|
Hi guys, I'm the noob again haha ^^.
I'm still learning C code and PICs, and I always used to switch pins like this PORTBbits.RB0 = 1; for example to do a simple LED chaser, but I saw "<< >> " to move bits right and left and I wanted to build an LED chaser from RB0 to RB7 using this bitwise operator, can I get an example please ? |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Mon Jan 30, 2017 11:21 am |
|
|
Code: | unsigned int8 pattern = 0x80;
pattern >>= 1; // pattern now holds 0x40
pattern >>= 2; // pattern now holds 0x10
pattern <<= 1; // pattern now holds 0x20 |
I'm leaving it up to you to determine:
a) how to write directly to an entire port at once;
b) how to create your LED chaser. In general, it will have to consist of a shift section (you'll need to keep track of whether you're traveling L or R, and perform the appropriate shift), and a delay section. These two steps will repeat forever. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Mon Jan 30, 2017 11:54 am |
|
|
Also, as a further comment, >> & << are the generic 'C' shifts. However for a rotation, you might want to look at the rotate_right and rotate_left functions:
Code: |
int8 val=0b00000001;
rotate_left(&val,1); //gives 0b00000010
rotate_right(&val,1); //back to the start
rotate_right(&val,1); //gives 0b10000000
|
Unlike the shift, where the bit would disappear 'out the bottom', the bit rotates back round to the top of the variable. |
|
|
ViperARG
Joined: 07 Jan 2017 Posts: 16
|
|
Posted: Wed Feb 01, 2017 12:03 pm |
|
|
Ttelmah wrote: | Also, as a further comment, >> & << are the generic 'C' shifts. However for a rotation, you might want to look at the rotate_right and rotate_left functions:
Code: |
int8 val=0b00000001;
rotate_left(&val,1); //gives 0b00000010
rotate_right(&val,1); //back to the start
rotate_right(&val,1); //gives 0b10000000
|
Unlike the shift, where the bit would disappear 'out the bottom', the bit rotates back round to the top of the variable. |
Awesome thanks, I didnĀ“t know that. I think that one is more suitable for what I want to do ^^ |
|
|
|
|
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
|