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 » Simple UART code

April 12, 2012
by Blackbird
Blackbird's Avatar

Got the scoreboard sensor code figured out thanks to your help, and a friend's.

I think I am over thinking this UART stuff, but it is just not sinking in. I have read the info in the library, and all of the UART posts in the forum.

I am trying to get the micro to send info to a RCSystems DoubleTalk RC8660 text to voice chip. I just need to send simple text strings and ASCII strings.

I am using the setup info from the UART.c below.

void uart_init() {
  // set baud rate
  UBRR0H = 0;
  UBRR0L = 7;   // for 115200bps with 14.7456MHz clock
  // enable uart RX and TX
  UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  // set 8N1 frame format
  UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);

  // set up STDIO handlers so you can use printf, etc
  fdevopen(&uart_putchar, &uart_getchar);

I have tried to combine this with the printf & uart_write codes but keep getting errors.

I do not need to receive anything from the sound chip.

What am I missing? Is there more micro setup code that I need?
There must be a simple way to send the strings that I am overlooking.

Thanks for helping with my learning.

April 12, 2012
by pcbolt
pcbolt's Avatar

Wow... that is an amazing chip.

Just a few questions.

  • What kind of errors are you getting?
  • Have you tried to talk to a PC with your code and the NK cable?
  • What does your wiring look like?
  • What is the operating voltage of the RC8660 (5v or 3.3v)?

Hope you can post a video when it works.

April 13, 2012
by Blackbird
Blackbird's Avatar

I guess I should back up a bit. This is my first attempt at using the UART to send from my coding. I have not tried any RS232 stuff to the computer other than the normal programming of the chip. The errors are all in the coding during compiling (I can get more specific when I get home), so that is where I am having a problem. Trying to get the code to even compile. Apparently I am much better at reverse engineering code for new features than creating it.

I have not even connected to the 8660 yet. Most of the code I have seen is receiving as well as sending, which may be adding to my confusion.

I was looking for the simple, initilize the chip to send, send text string with this code.

When I do hook the 2 together, they will both be running at 3.3v to keep things simple. I already have a simple LED program on the chip at 3.3v so I know that it is working OK. Just can't get past the coding steps.

The plan is to have the text strings in IF statements for specific scores, like the win, where it will just send the string to the chip. The 8860 automatically converts any text string sent to it to voice. Once that is done, I will start working on the recorded sounds/messages.

I will certainly post the project video/pictures once it is working properly. Thanks again for your time and patience.

April 13, 2012
by Blackbird
Blackbird's Avatar

I have been doing more reading (Google) and I think I am missing some basic setup steps. I am going to try some more tonight, but still appreciate any input.

April 13, 2012
by pcbolt
pcbolt's Avatar

Blackbird -

The Nerdkits "tempsensor" project communicates over the UART port. If you can compile that code and are able to read the output string on your PC, you should be good to go. You don't need much more than that since you only need one-way communications.

Running your MCU at 3.3v should not be a problem. Rememeber to switch back to 5v if you are using the USB cable from a PC to upload code to the MCU.

April 17, 2012
by Blackbird
Blackbird's Avatar

My hair is almost gone..... I have tried many different codes and wiring setups and cannot get the DoubleTalk to respond.

The closest I came was when I flashed the ATMega and forgot to disconnect 1 wire so the power backfed the DoubleTalk and it started talking jibberish during the read cycle. The chip still works, as I can use the RCStudio software that came with it to communicate and make it talk.

I also set the DoubleTalk to receive at 115200 (I thought about dropping the speeds down, but didn't want to screw up working code)(I also tried it on AutoDetect). I think the problem may be getting the DoubleTalk to see the right starting/ending character. The OP Guide says "Note: The RC8660 will not begin speaking or execute commands until it receives a CR (0Dh) or NUL (00h) character." It wants it at the end of each text message. I thought the /r would do that, but I am not so sure now.

How would I send the (oDh) or (00h) at the end?

It reads text strings, but receives commands in ASCII. Not sure how to get that to send.

I would like to just get it to say a letter, so maybe it would be better to send the letter in ASCII followed by the ASCII for CR or NUL just to test it. Any ideas?

I did use HyperTerminal and am able to see the TempSensor output on the PC as well as the short program below so I know the ATMega is sending.

// uart.c
// for NerdKits with ATmega168, 14.7456 MHz clock
// mrobbins@mit.edu

#include <stdio.h>
#include <stdlib.h>

#include <avr/io.h>
#include <inttypes.h>
#include "../libnerdkits/uart.h"  // add a reference to the header file 
#include "../libnerdkits/delay.h"
#include <avr/pgmspace.h>

int main() {

  // start up the serial port
  uart_init();

  FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
  stdin = stdout = &uart_stream;

while (1) {
  delay_ms(2000);

  // write message to serial port

  printf_P(PSTR("\r"));
  printf_P(PSTR("Hello world\r\n"));
   }  
return 0;
}

Thanks again for your time.

April 17, 2012
by pcbolt
pcbolt's Avatar

Blackbird -

I think you can use the "uart_write()" function to send individual characters. A simple use would be:

uart_write(0x0D);
uart_write(0x00);

This will send the ASCII codes as values, not as literals. I would have thought the "\r" would have worked though.

Post a Reply

Please log in to post a reply.

Did you know that many systems in nature can be described by a first order response? Learn more...