Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 24, 2024, 04:26:59 16:26


Login with username, password and session length


Pages: [1]
Print
Author Topic: Bitfiddlin'  (Read 4217 times)
0 Members and 2 Guests are viewing this topic.
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« on: July 04, 2006, 01:57:42 01:57 »

Yeah, let's roll...
I know that some of You are complaining abt. the martian language of HI-TECH C, at least when it comes to the supplied examples in HI-TECH PICC and HI-TECH PICC18(ver. 9.50). I agree, when I first saw it, I didn't know wheter it was C or assembler. To begin with, 'ave a look at the attached tut. (probably at that german server since uploads doesn't work here anymore; Rego, where art though). It should straighten out some of Yr. questions....
:p
http://rapidshare.de/files/24875337/Bitfiddling.doc.html
 
As You can see, HI-TECH compilers rely heavy on these short snippets in various forms. More to come...
Logged

Code Warrior
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« Reply #1 on: July 05, 2006, 04:50:09 16:50 »

In addition to the above, here comes two more important bitfiddling techinques:
Left shift << means that You move all bits in a byte to the left one position. This is the same as dividing the number by two as in: 11111111(255d) << 1 = 01111111(127d). Here You can see that the leftmost digit is "kicked out".
If You want to divide by more than two: 11111111(255d) <<2 = 00111111(63d)
Moving 1:s even more: 11111111(255d) << 3 = 1010101(85d)
 
Right shift >> is also true when You multiply a binary byte:
0111111(127d) >> 1 = 11111111(255d). Here the rigtmost 1 is inserted to the left and cut away the 0 in the beginning.
Apart from an faster way to multipie and divide, this is a precise method to control the bitregisters in the PIC based on an algorithm.
Feedback appreciated...Wink
Logged

Code Warrior
freddyg
Junior Member
**
Offline Offline

Posts: 85

Thank You
-Given: 3
-Receive: 2


« Reply #2 on: July 05, 2006, 06:41:27 18:41 »

Wrong way round!

Shift left is MULTIPLY by 2, shift right is DIVIDE by 2.

...err... I'm getting confused. Is your post to do with what is supposed to happen, or to do with bugs with the compiler  :confused:

Freddy
« Last Edit: July 05, 2006, 06:44:44 18:44 by freddyg » Logged
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« Reply #3 on: July 05, 2006, 08:52:09 20:52 »

Yr. absolutely right... I happened to reverse the calculations and the bitshifting and ended up with the wrong order for the bits...:mad:

This Tut was mainly for those who are not familiar with bitfiddling in a byte. I think it is specially useful for those who want to take fully control of the PIC:s different 8-bits registers.
Do You find my other Tut:s about AND,OR,XOR satisfactorly? Anyway, thank You for being observant...Smiley
Remarks and suggestion appreciable
P.S. I have here in Sweden a quite simple RTOS application regarding a coffe vending machine. Unfortunately, it contains the concept of "State Machines" which some of You may be not familiar with. I'll try to avoid this without compromising understanding.
If You are interested, tell me so. The processor is a PIC628.Cheesy
Logged

Code Warrior
mana111
Newbie
*
Offline Offline

Posts: 28

Thank You
-Given: 9
-Receive: 6


« Reply #4 on: July 05, 2006, 10:57:43 22:57 »

I am interested Smiley
Logged
Mega32
Active Member
***
Offline Offline

Posts: 179

Thank You
-Given: 207
-Receive: 37


« Reply #5 on: July 06, 2006, 06:18:48 18:18 »

This is a nice thread from the AVR Forum (He..He)

http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=37871

/Mega32
Logged
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« Reply #6 on: July 10, 2006, 01:17:37 13:17 »

A good one.. though, I don't understand why everybody has to write bitvalues in hex. It's much easier to write bitvalues(at least 8-bits) in binary representation. Then You are able to see directly which bits are set or not. After say, two weeks, it's impossible to remember what 0x3F in ADCON1 meant....
Logged

Code Warrior
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC