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 » help needed with uart code

June 09, 2009
by HarmVeenstra
HarmVeenstra's Avatar

am (trying to) get a chess program to work on the nerdkit.

What I need to do some debugging is a function to write the value of a "string" through the uart to my terminal program.

I have included uart.h and initialized the uart like in all other nerdkit programs.

I want to be able to code e.g.:

char[] myString;
strcpy(myString,"test data");
showOnTerminal(myString);

void showOnTerminal ( char[] s) {
    // can't seem to get working code here !    HELP
}
June 09, 2009
by wayward
wayward's Avatar

Hi HarmVeenstra,

there's a somewhat in-depth explanation on why that fails to work. Your code needs a bit more hammering. (I sense Java/JavaScript training ;))

Firstly, this:

char myString[];

does not reserve any memory for the character array. It's just a pointer to an unspecified place in the RAM. Instead, you may want to try this:

char myString[20];

Another thing is that string literals of the form "blah blah" also fail in the default NerdKits setup (see the discussion referenced above). You'll have to store your string in program memory.

Try this:

#include <avr/pgmspace.h>
#define MAX_MSG_LEN 20  /* for example */

/* ... */

/* it's really a non-issue, but the canonical method of
   naming C identifiers is to separate words_with_underscores,
   not camelCase -- sorry for being so nitpicky */
char my_string[MAX_MSG_LEN];
strncpy_P(my_string, PSTR("test data"), MAX_MSG_LEN);
show_on_terminal(my_string);

void show_on_terminal(char s[]) {
  /* do whatever you please */
}

After this, you can modify the my_string at will. That was the main idea, right? Otherwise you can just do lcd_write_string(PSTR("test data")); .

Good luck,

Zoran

December 05, 2009
by pbfy0
pbfy0's Avatar

I think show_on_terminal should be like this:

void show_on_terminal(PGM_P s){
    while(pgm_read_byte(s)){
        uart_write(pgm_read_byte(s);
        s++;
    }
}

and then you'd call it like this:

show_on_terminal(PSTR("Hello World!\n"));

you'd need to include

<avr/pgmspace.h>

in the beginning of the file like this:

#include <avr/pgmspace.h>

I hope you get it working

Paul

March 19, 2011
by plinko
plinko's Avatar

This approach worked most effectively for me to spit formatted data to the serial port:

printf_P(PSTR("Data 1: %d, Data 2 %d\r\n"), adc_read(), adc_avg());

adc_read and adc_avg return uint16_t

Post a Reply

Please log in to post a reply.

Did you know that one NerdKits customer controlled a laser pointer with his computer using a microcontroller? Learn more...