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.

Microcontroller Programming » residue left in off position (bitwise arithmetic example)

January 03, 2013
by Vreni
Vreni's Avatar

Hello,

i tried to change the bitwise arithmetic programme, i left shift the number a8 by pc5(=z1 in the code). So if switch 8 (connected to PC5) is set i get 32 that is correct but in case i switch it off i get a 02. The same happens when i do that for a7<<pc4 my variable z2 will show 16 (correct) but in the off position it shows 06. I thought that maybe in PINC are still some values set so i added an & condition but nothing improved. Please find the section of the code below :

while(1) {

      // (PINC & (1<<PC0)) returns 8 bit number, 0's except position PA0 which is
      // the bit we want
      // shift it back by PA0 to put the bit we want in the 0th position.

  a3 = (PINC & (1<<PC0)) >> PC0;
  a4=(PINC & (1<<PC1))>>PC1;
  a5=(PINC & (1<<PC2))>>PC2;
  a6=(PINC & (1<<PC3))>>PC3;
  a7=(PINC & (1<<PC4))>>PC4;
  a8=(PINC & (1<<PC5))>>PC5;
  z1=(PINC & (a8<<PC5));
  z2=(PINC & (a7<<PC4));

      lcd_home();
      lcd_write_string(PSTR("Wert a8: "));
      lcd_write_int16(a8);
      lcd_line_two();
      lcd_write_string(PSTR("Wert z1: "));
      lcd_write_int16(z1);
      lcd_line_three();
      lcd_write_string(PSTR("Wert z2: "));
      lcd_write_int16(z2);
      lcd_line_four();
      lcd_write_string(PSTR("Wert a7: "));
      lcd_write_int16(a7);

Thanks a lot for your help. Best regards, Verena

January 03, 2013
by pcbolt
pcbolt's Avatar

Hi Verena -

I think your code is fine but the problem is when you output to the LCD. First when you output 32 you use 2 character spaces then when you print a single 0, it doesn't overwrite the previous 2 (from 32). Which explains why 06 remains after a 16. If you do this.....

lcd_write_string(PSTR("Wert z1: "));
lcd_write_int16(z1);
lcd_write_string(PSTR("     "));

....for each of the LCD lines it should clear up the problem.

Post a Reply

Please log in to post a reply.

Did you know that many systems in nature can be described by a first order response? Learn more...