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.

Basic Electronics » Help with reading UART Stream on mac

August 18, 2013
by Affarram
Affarram's Avatar

In the Temperature Sensor project it says:

"The last line uses printf_P() to write a line to stdout, which you remember we mapped to the UART stream which writes data to the serial connection. If you wanted to you could connect your computer and read the temperature from your computer. The printf and fprintf functions are very powerful, and you can find information about in our printf/scanf tutorial on our website."

I watched the video on this topic and understand how to use printf/scanf. I have loaded the C code into Xcode and it works great when testing my code in the console. But, I am having trouble figuring out how to use my micro controller and have it display things in terminal on my mac like was shown in the "C Programming: The printf and scanf Family of Functions" tutorial. If anyone can help I would appreciate it.

Thank You Sean

August 19, 2013
by Ralphxyz
Ralphxyz's Avatar

Seems like I post a how-to here in the forum oh wait I posted it in the Nerdkits Library.

I gave up on using my MAC after Apple once again abandoned it's legacy (2 years old or more) users. But I'll try to remember how to do that, maybe Google will get you there that is how I figured it out.

Ralph

August 19, 2013
by Affarram
Affarram's Avatar

Ralph,

Thanks for the response. My first thought was to look in the library but it doesn't seem to be working. I searched the forums for a bit and was unable to find the answer. I hate to ask a question if it has been posed many times. I will continue to search the web until I figure this out.

Sean

August 19, 2013
by Noter
Noter's Avatar

You need a serial port terminal program that runs on your mac to see ascii data from the serial line that connects your micro controller.

August 20, 2013
by Ralphxyz
Ralphxyz's Avatar

"You need a serial port terminal program"

That is built-in on the MAC, I just cannot remember how to use it.

Ralph

August 20, 2013
by Ralphxyz
Ralphxyz's Avatar

Ah here ya go, a simple google of Mac serial?.

You use the "screen" command!! Seems like I had to do certain things when screen hung I'll have to check my notes, if I could find them.

Ralph

August 20, 2013
by Affarram
Affarram's Avatar

Thanks. This should be enough for me to figure out the rest.

August 27, 2013
by Affarram
Affarram's Avatar

Ok, so. I have looked into using the "screen" command and I must be doing something wrong.

I compiled this code to my micro controller:

// printf_and_scanf.c
// for NerdKits with ATmega168
// mrobbins@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:
//

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;

  // initialize variables
  int16_t y = 0;
  double z = 0.0;

  // loop doing this forever!
  while(1) {
    // ask some questions and collect responses
    printf_P(PSTR("\r\nHow old are you? "));
    scanf_P(PSTR("%d"), &y);
    printf_P(PSTR("\r\nWhat's your favorite number? "));
    scanf_P(PSTR("%lf"), &z);

    // send results back over the serial port
    printf_P(PSTR("\r\nCool!\r\n You are %d years old and like the number %f."), y, z);

    // send results to the LCD as well
    lcd_clear_and_home();
    lcd_line_one();
    fprintf_P(&lcd_stream, PSTR("Age: %d"), y);
    lcd_line_two();
    fprintf_P(&lcd_stream, PSTR("Fav. Number: %7f"), z);
    lcd_line_four();
    fprintf_P(&lcd_stream, PSTR("  www.NerdKits.com  "));
  }

  return 0;
}

Then when i reset my nerdkit the LCD is blank. I then use terminal to connect to my nerdkit by typing

screen /dev/cu.usbserial

or

screen /dev/tty.usbserial

They both give me the same result.

My nerdkit's LCD then displays

Age: 0
Fav. Number: 0.00000

www.nerdkits.com

And in terminal I get

�3���

and anything I type is more gibberish. Any Ideas on what I can try?

August 27, 2013
by Affarram
Affarram's Avatar

So I discovered something called a 'Baud Rate' after a few hours of watching youtube videos. I then looked in the uart.c file and found that the baud rate is set to 115200bps with 14.7456MHz clock.

In terminal I typed

screen /dev/tty.usbserial 115200

now I get

How old are you? 
What's your favorite number?

so I seem to have found my solution.

Post a Reply

Please log in to post a reply.

Did you know that you can connect to certain car computers via the OBD-II port with a microcontroller? Learn more...