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.

Project Help and Ideas » Interactive LCD (laptop <=> MC device)

April 14, 2009
by ueharaa
ueharaa's Avatar

Hello all, I just wanted to share my first Nerdkits project. It's a small project that I hope will be useful as a starting place for command line interface for future projects. This is my first project, so I offer my experience without any guarantees of anything working :)

Test: To communicate with my device. I want to open a program/command prompt and type something and have the LCD display it.

Details:

software requirements:

  1. python
  2. pyserial ** there was some simple python syntax error that needed to be fixed in miniterm.py (I can't remember what it was but, I think it wouldn't run unless you fixed the error. Missing colon perhaps?)
  3. pywin32 ** needed for windows. Linux or mac systems probably won't need this.

wiring requirements: I followed the nerdkits setup for the temperature sensor and , make sure you can copy(burn?) your program to your device.

First Test: first test your connection to your device by running (you can also use putty in serial mode)

python miniterm.py -p YOURPORT -b 115200

if everything is hooked up properly you should see your temperature being displayed to console.

Second Test: I wrote this code to interact using miniterm.py It takes user input and displays it on the LCD and echos the input back to the console. Typing "q" will send it to it's dying loop.

Code:

#include <stdio.h>
#include <math.h>
#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include "../libnerdkits/uart.h"
#include <ctype.h>
#include <avr/pgmspace.h>

static void delay_1s(void) {
  uint8_t i;

  for (i = 0; i < 100; i++)
    delay_ms(10);
}

int main() {
  // start up the LCD
  lcd_init();
  FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
  lcd_home();

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

// variables to store input
  uint8_t i;
  char buf[25];

//remapping stderr
  stdout = stdin = &uart_stream;
  stderr = &lcd_stream;

  lcd_home();
//to serial port
  printf_P(PSTR("Hello world! \n"));
//to lcd
  lcd_write_string(PSTR("Good Morning world!"));

  while(1) {
      printf_P(PSTR("Enter command: "));
      if (fgets(buf, sizeof buf - 1, stdin) == NULL)
        break;
// q to quit
      if (tolower(buf[0]) == 'q')
    break;

//to serial port
      printf_P(PSTR("input: %s\n"),buf);

      lcd_clear_and_home();
//to lcd
      fprintf_P(&lcd_stream, PSTR("%s"), buf);

/*
//attempt to get rid of two black spaces on LCD at the end of any string
      for (i=0;i<25;i++){
        if (buf[i]=='/0')
          break;
        printf_P(PSTR("i:%s\n"),buf[i]);
        lcd_putchar(buf[i],&lcd_stream);
      }
*/
  }
  lcd_clear_and_home();
  lcd_write_string(PSTR("I'm dying!"));

  delay_1s();
  for (i = 0; i < 3; i++)
    {
      putc('.', stderr);
      delay_1s();
    }
  fprintf(stderr, "\n ");
  lcd_line_two();
  lcd_write_string(PSTR("help me! sea otters! "));

  return 0;
}

Other notes: For some reason I get two black spaces on the LCD when I send text to it, not sure what that is. Perhaps someone on this list can help?

Hope this helps others interested in having their computer talk to their MC and vice versa.

-andy

April 14, 2009
by ueharaa
ueharaa's Avatar

sorry about the awful spacing. Not sure what happened, When I originally posted, it did not look that way. -andy

PS. Nerdkits, Can someone fix the spacing on the code part, it looks really awful now :(

April 14, 2009
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Just fixed the spacing -- just use a tab or at least four spaces before any code segments and it will do the right thing in the future.

As for your two black spaces, my guess is that you are getting a "\r\n" (Windows newline) in there. The fgets(...) function will include its terminating newline, but if your terminal sends a two-character \r\n newline, they'll both get included. I see you tried writing some code to get around this, but maybe you can put something like this right before your fprintf_P(...) statement:

for (i=0;i<25;i++){
  if (buf[i]=='\0') {
    break;
  } else if(buf[i]=='\r' || buf[i]=='\n') {
    buf[i]=='\0';
    break;
  }
}

The code block above will go through the buffer and if it finds either newline character, it will replace that with a '\0' to terminate the string there.

Also note that I changed your '/0' to a '\0' -- the latter is the correct way to denote a null character, although I think this might be because the forum has a problem with backslashes at the moment!

Let me know if that works!

Mike

April 14, 2009
by ranger
ranger's Avatar

somewhat related... is there a simple way to read the information sent over the serial cable from the tempsensor project? Ideally something from a (linux) terminal.

April 14, 2009
by wayward
wayward's Avatar

Assuming you have the serial-to-USB converter and pl2303 driver, then simply reading and writing to and from the special file /dev/ttyUSBx (ttyUSB0 unless you have other serial devices via USB, for example a Palm Pilot via cable) suffices. E.g.:

tail -f /dev/ttyUSB0

will spit out the lines from the MCU to your terminal. Accordingly,

cat > /dev/ttyUSB0

and you can send stuff to the MCU. Use your UNIX-fu for the rest.

Zoran

May 26, 2009
by tom8787
tom8787's Avatar

Great project, was creating something similar. Tnx for posting!

May 27, 2009
by tom8787
tom8787's Avatar

@ranger: Read this topic: http://www.nerdkits.com/forum/thread/91/

Post a Reply

Please log in to post a reply.

Did you know that you can build a giant scrolling LED array with our microcontroller kit? Learn more...