NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » Bitwise Math modification

February 19, 2010
by Ralphxyz
Ralphxyz's Avatar

Me who knows so little, tried to modify the Bitwise Math project.

      lcd_home();
  lcd_write_string(PSTR("Adding: "));
  lcd_write_int16((a1<<2) + (a2<<1) + a3);
  lcd_write_data('+');
  lcd_write_int16((b1<<2) + (b2<<1) + b3);
  lcd_write_string(PSTR("        "));

  lcd_line_two();
  lcd_write_string(PSTR("Equals: "));
  lcd_write_int16(  ((a1<<2) + (a2<<1) + a3)  +  ((b1<<2) + (b2<<1) +   b3) );
  lcd_write_string(PSTR(" "));

  lcd_write_int16(b3);
  lcd_write_int16(b2);
  lcd_write_int16(b1);
  lcd_write_int16(a3);
  lcd_write_int16(a2);
  lcd_write_int16(a1);

I tried to show the switch positions on the LCD (the last six lines are my code).

It almost works.

The problem is with 3 or more switches "ON" I get 1111000 (4 ones). To see the problem goto http://nk.rovenet.com.

Thanks for the help, Ralph

February 19, 2010
by bretm
bretm's Avatar

You're not erasing the previous characters from the LCD. On the second-to-last problem the result was two digits (14) so the bits were written further over to the right. When you go back to a one-digit answer, that last "1" is left over from the previous screen.

February 20, 2010
by Ralphxyz
Ralphxyz's Avatar

Well if I turn more switches on I still get the 7 digits. It seems as if I would see more.

Anyway thanks for the reply, now how would I show the position (on off 0 1) of the switches?

Ralph

February 20, 2010
by bretm
bretm's Avatar

You would only see more if the result of the addition went to triple digits, i.e. 100 or more. It's never going to be bigger than 14 without modifying the program and adding more switches, so that's not going to happen.

After you output the six bits you can just output another space with PSTR(" ").

I'm not sure what your other question is. It looks like you're already showing the position of the switches (the 0's and 1's).

February 20, 2010
by Ralphxyz
Ralphxyz's Avatar

Adding a lcd_write_string(PSTR("")); did no change the behavior.

The problem is that I am seeing 7 digits instead of six.

I am not doing any addition just displaying the variables as 0 or 1 depending on the position of the switch.

I am not doing a loop so where does that 7th digit come from? And how do I not show it?

As illustrated in the pictures closing one or two switches works correctly (6 digits) closing a third switch or more produces 7 digits.

All I am doing is writing the variables I am not doing anything to them.

It should be just a simple display of the switch position.

I am sorry if I seem dense, I am. Am I just missing something simple.

Ralph

February 20, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Ralphxyz,

I think bretm, got it right when he suggested that the problem is being caused by not erasing the last number you wrote to the LCD. I think it would help to think of what you are writing to your second line character by character, and remember that nothing gets erased unless you explicitly write something over it. Look at what happens when you switch from writing 7+4 to 7+0

Equals: 11 111111 
Equals: 7 000111

The code and LCD is actually doing what it is supposed to. The final 1 is just never over written with anything. This happens because you jumped from displaying a two digit final result to a one digit final result. bretm's solution was right, but you actually have to write a space (or a couple) to make sure the junk at the end gets erased. Adding a

lcd_write_string(PSTR("  "));

right after you write the six digits should do the trick.

These might seem like simple things, but they are not. When controlling low level hardware this way you have to think of every little thing, all this is great learning!

Humberto

February 20, 2010
by JKITSON
JKITSON's Avatar

HI In the last picture your answer is only 1 digit (7). The previous pictures show 2 digits answers. What is happening is the binary digits shift left 1 position for a single digit answer and leave what ever was in the privious data there as garbage..

Do the lcd_write_string(PSTR(" ")); (note: there has to be a space between the quotes) prior to printing the line of info. This should clear the garbage and work...

jim

February 20, 2010
by Ralphxyz
Ralphxyz's Avatar

The "lcd_write_string(PSTR(" "));" did the trick!!

Thank you everyone.

I do not comprehend exactly what was happening nor why it works but it works and I can move on.

Thanks again,

Ralph

Post a Reply

Please log in to post a reply.

Did you know that a thermometer can be made "faster" by using a bit of math? Learn more...