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 » dip_arithmetic project questions

August 09, 2010
by kle8309
kle8309's Avatar

Can anyone explain how does or why we use a integer16 (16 bits)lcd write function to write a uint8_t (8 bits).

A different question but isn't it a bad idea to short the pin when connecting the switch to ground. It draws about 85mA of current when I measured it. Is there is safer way to do it.

August 09, 2010
by kle8309
kle8309's Avatar

int main() { // start up the LCD lcd_init(); lcd_home();

// Set the 6 pins to input mode - Two 3 bit numbers
DDRC &= ~(1<<PC0); // set PC0 as input DDRC &= ~(1<<PC1); // set PC1 as input DDRC &= ~(1<<PC2); // set PC2 as input DDRC &= ~(1<<PC3); // set PC3 as input DDRC &= ~(1<<PC4); // set PC4 as input DDRC &= ~(1<<PC5); // set PC5 as input

// turn on the internal resistors for the pins

PORTC |= (1<<PC0); // turn on internal pull up resistor for PC0 PORTC |= (1<<PC1); // turn on internal pull up resistor for PC1 PORTC |= (1<<PC2); // turn on internal pull up resistor for PC2 PORTC |= (1<<PC3); // turn on internal pull up resistor for PC3 PORTC |= (1<<PC4); // turn on internal pull up resistor for PC6 PORTC |= (1<<PC5); // turn on internal pull up resistor for PC7

// declare the variables to represent each bit, of our two 3 bit numbers uint8_t a1; uint8_t a2; uint8_t a3;

uint8_t b1; uint8_t b2; uint8_t b3;

while(1) {

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

  a1 = (PINC & (1<<PC0)) >> PC0;
  a2 = (PINC & (1<<PC1)) >> PC1;
  a3 = (PINC & (1<<PC2)) >> PC2;

  b1 = (PINC & (1<<PC3)) >> PC3;
  b2 = (PINC & (1<<PC4)) >> PC4;
  b3 = (PINC & (1<<PC5)) >> PC5;

      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("        "));

  //delay_ms(500);

} return 0; }

August 09, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi kle8309,

When you attempt to call a function that takes a 16 bit integer with an 8 bit integer the 8 bit integer is cast into a 16 bit integer when it gets passed to the function. Seeing as most of the time we use either 16 bit or 8 bit integers we just made one lcd_write_int16() function and use it for both. Hope that answers your question.

Humberto

August 10, 2010
by kle8309
kle8309's Avatar

Thanks Humberto!

Post a Reply

Please log in to post a reply.

Did you know that using an alternating excitation voltage can reduce the noise in many sensor measurements? Learn more...