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 » Help with Comunication

July 24, 2009
by pedroh96
pedroh96's Avatar

Hey guys, I've been writing a code to do the following: when I type a message in the terminal, the message is displayed in my LCD.

Please, check my code

 #define F_CPU 14745600
 #include <stdio.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"

 int main() {
   // init LCD
   lcd_init();
   FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
   lcd_write_string(PSTR("Waiting for message.."));

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

   char tc;
   char message;
   while(1) {
     lcd_line_two();
     // Wait for a character to be sent to the serial port.
     tc = uart_read();
    tc = message;
    fprintf_P(&lcd_stream, PSTR("%c"), message);

   }

   return 0;
 }

If there's a simplest way to do it, please tell me (or tell me how to do it fixing that code).

Thanks, pH

July 24, 2009
by wayward
wayward's Avatar

Hi pedroh96,

here's a simple code snippet that should work. It prints everything up until and including the terminating newline character, then exits.

Cheers!

char tc = NULL;
lcd_line_two();

while(c != '\n') {       // loop until a newline arrives
    tc = uart_read();    // get a character from the UART, blocking until available
    lcd_write_data(td);  // since you're writing a single char, printf is not needed
}
July 24, 2009
by Fahr
Fahr's Avatar

If you want to fix your own code, change;

tc = message;

into

message = tc;

Right now you're sending an unitialized char to the screen.

July 24, 2009
by pedroh96
pedroh96's Avatar

Hey! Actually, I don't want to write a single character, but some words. How can I change my code to do this?

Thanks, pH

July 24, 2009
by pedroh96
pedroh96's Avatar

Please help, guys. I'd like to know how to show more then just a character in the screen.

Thanks, pH

July 24, 2009
by wayward
wayward's Avatar

This code

while(c != '\n') {       // loop until a newline arrives
    tc = uart_read();    // get a character from the UART, blocking until available
    lcd_write_data(td);  // since you're writing a single char, printf is not needed
}

indeed does print a single character, but it repeats that action until a newline ('\n') is encountered. End result is that your input line is sent to the LCD without an intervening character array, which would only be an unnecessary bother/risk in this case. Try it! :)

July 24, 2009
by wayward
wayward's Avatar
lcd_write_data(td);

should read

lcd_write_data(tc);

Post a Reply

Please log in to post a reply.

Did you know that first-order systems have a exponentially decaying response to step inputs? Learn more...