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 » printf dip arithmetic

January 20, 2011
by missle3944
missle3944's Avatar

Hi im trying to display my project on my computer using the printf function through the MCU. In other words I want to show to answer to the input numbers on my computer. I keep getting an error when I do this.

    / dip_arithmetic.c
    // for NerdKits with ATmega168
    // hevans@mit.edu

    #define F_CPU 14745600

    #include <stdio.h>
    #include <math.h>

    #include <avr/io.h>
    #include <avr/interrupt.h>
    #include <avr/pgmspace.h>
    #include <inttypes.h>

    #include "../libnerdkits/delay.h"
    #include "../libnerdkits/lcd.h"
    #include "../libnerdkits/uart.h"

    // PIN DEFINITIONS:
    // 
    // PC0 -- DIP SWITCH DP1
    // PC1 -- DIP SWITCH DP2
    // PC2 -- DIP SWITCH DP3
    // PC3 -- DIP SWITCH DP4
    // PC4 -- DIP SWITCH DP5
    // PC5 -- DIP SWITCH DP6
    //
    // NOTE: (switches connects to GND when closed, otherwise
    // the pin is internally pulled up through a pull-up resistor to VCC)

    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("Mulitplying: "));
          lcd_write_int16((a1<<2) + (a2<<1) + a3);
          lcd_write_data('x');
          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("        "));

          printf_P(PSTR( "Equals..."));
          printf_P_int16(PTSR(((a1<<2) + (a2<<1) + a3)  *  ((b1<<2) + (b2<<1) + b3) );

      }
      return 0;
January 21, 2011
by Rick_S
Rick_S's Avatar

What is the error you are getting? Is it at compile time? Upload time? Etc...

Also, a screen shot of the error would help if you can grab one and post it.

Rick

January 21, 2011
by bretm
bretm's Avatar

printf_P won't do anything until you tell it where you want the output to go. You have to initialize the serial port with uart_init() and then direct standard output to the uart_putchar function. Code from the tempsensor project shows how:

// start up the serial port
uart_init();
FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
stdin = stdout = &uart_stream;

Post a Reply

Please log in to post a reply.

Did you know that you can impress a loved one with a digitally-controlled Valentine's Day Card with randomly twinkling LEDs? Learn more...