NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Variation on 3 bit adder
January 22, 2012 by Blackwatch42nd |
Hi folks, My first time posting and really (I mean REALLY) new at this micro controller stuff. After I had gone through the exercises in the maunal, I decided to try a slight variation on the 3 Bit Adder (Bitwise Arithmetic). I also wanted to try to refer to the book as little as possible (well that didn't last long). For reasons I'm still haven't figured out, I wired the DIP switch a little strange: BP1 -> Sw1 BP2 -> Sw2 BP3 -> Sw3 CP1 -> Sw6 CP2 -> Sw7 CP3 -> Sw8 But, because the way the DIP switch was oriented I wanted the LSBs to be Sw3 and Sw8. What ended up happening was the bits were reversed in the register from the way I wanted. Now I realize I could have switched the wires around but that would have been too easy, soooo ... I wrote a function to reverse the bits. I could not find anything, anywhere to do this, so I would like ya'll (I'm from Texas) to tell me if there is a better way to do it (program wise) and whats a good way to out a binary number to the display other than how I did it, with text? I'm including the whole code (is that too much?) and thanx in advance John
|
---|---|
January 22, 2012 by JimFrederickson |
I don't see anything that needs to be changed in your "ReverseBits" function. Assuming am assuming that you want to output the values of the pins one at a time as they are read. So would, at least, make this change...
This gets rid of a few function calls per bit-group. (And the cursor only gets positions once.) The LCD goes to the next position on a line automatically, so you can take advantage of that. Ultimately though I would display the bits directly from the firstnumb and secondnumb uint8_t values.
or
This approach gets rid of more of the function calls, and I think it is easier to read as well. (Hopefully this is correct and doesn't lead you astray... I didn't 'test it'! But if it is not entirely correct it is quite close.) |
January 23, 2012 by Blackwatch42nd |
Jim, Thanx for the quick reply. I think I understand the need for fewer function calls (less overhead) but I guess the question becomes a point of diminishing returns. How do you tell when it's better to write the same code 3 different times or call a function 3 times? Hmmm? With regard to the "lcd_write_data" call, I'm not sure how to use it (or some of the other lcd function, ie byte, nibble). "lcd_write_data" seems like it only takes one argument and your example passes two. I do like you first alternate solulution. It is cleaner and easier to read, but you'd have to start with the 3rd bit (msb) to make it read correctly on the the display. Is there some place that explaines all these function calls without having to decipher the include files? Thanx again John |
January 23, 2012 by Blackwatch42nd |
Jim, Oops, sorry. You were correct about the order. I miss read my own bit order. John |
January 23, 2012 by JimFrederickson |
Hello John, Sorry about the function call... The ',' should be '+' on all of the calls. So:
What happens is that you start with an ASCII '0' character value and then you add the appropriate bit-value to it. They way it will display and ASCII '0' if the bit is clear, or an ASCII '1' of the bit is set. If this manner the 'if statements' do not have to be used to get the same affect. (Sorry for the confusion, caused from my mistake...) |
January 23, 2012 by JimFrederickson |
As for the bit patterns I displayed one value both ways to show lsb first and msb first. (Well most least significant bit and most significant bit from 'firstnumb'.) gets the lsb of firstnumb, using a mask to only keep bit 0.
Shifts 1 bit right, (divides by 2), firstnumb and so bit position 1 is in bit position 0, then uses a mask to keep bit 0.
Shifts, (divides by 4), firstnumb 2 positions so that position 2 is in bit position 0, then uses a mask to keep bit 0.
|
January 23, 2012 by JimFrederickson |
The 'libnerkits files' are files that they created explicitly to for their product. The good part though, is that the files are mostly small and learning by working example is often easier than just ready about something. (Being able to read someone else's code and seeing how someone else codes things is always a good thing.) I am not sure if there is a clear way "you tell when it's better to write the same code 3 different times or call a function 3 times".. Writing the same code 3 times can use up a lot more space. Also if you have to change it then you have multiple places to change it, unless you have called a function then there is only 1 place. Also if you write the code out 3 times often runs faster. (There are also 'macros' that can work like 'inline functions' so that there is still only 1 place to change how it works.) For me I am always consciously aware of several factors whenever I code something. My two most important factors are: 1 - Do I think that I will be able to read and understand what I did today, in 2-3 months from now... 2 - Can it be changed easily, or made easier to change, in the future. This is my single most important criteria, and affects about 80%-90% of what I do and how I do it. Other factors are: 3 - Do I need to adhere to some specific standard or specification? This is the 'rough order' and the order can be changed depending on 'degree'. (For instance I VERY MUCH avoid the c floating point libraries because they tend to be quite large, and in my tests it seems to be confusing to the optimization routines so anytime I use them I have already very carefully considered their need as well as potential alternatives.) Back to this little 'microcosm' of 'displaying binary'... (Okay the following code has ALL BEEN COMPILED so the syntax is correct and functioning! this time... :( ) Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
The functions produce output as:
I use the first function. I use it because to me it is easy to read and looks better. I can just 'glance at it' and very quickly see my intentions. Now, that beings said... Here are the actual sizes of each function: (As compiled by my configuration of the AVR-GCC Environment.) myuart_writebinary - 120 bytes myuart_writebinary2 - 42 bytes (Note display LSB ->> MSB, backwards) myuart_writebinary3 - 48 bytes myuart_writebinary4 - 68 bytes myuart_writebinary5 - 48 bytes So the 'one I prefer' is the largest one. (NOTE: Putting in the '-' between the nibbles of the byte costs about 10bytes of program space, but for me the improved readability of the output is important.) Am I concerned about the the size difference? For me not really. If I was running short of 'code space' then I likely would change things, but for what I do it has never been a real factor. I am sure there are many other ways, better/smaller, to output a binary number as well. The 'c language' is extremely flexible which is why it has been around so long and has such wide use. |
January 23, 2012 by JimFrederickson |
The 'code sections' are there, but they are not showing up on My Browser unless I click the 'View Original' link. They looked great in the Preview? (Which I dig just before I made the post. WHY? I don't know! Yet... (maybe I will reboot and see...) (Can everyone else see the code examples without using the link?) |
January 23, 2012 by Blackwatch42nd |
Jim, Thanx for the info. It helps alot, though I still need to wade thru and get a good understanding of it. The email I got from the forum was short some of the code and several paragraphs but it looks like it all showed up ok on the website. Seems a little strange. Anyway, thanx again and i have more questions as time goes along. John |
Please log in to post a reply.
Did you know that you can see each keypress on a computer keyboard on an oscilloscope? Learn more...
|