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 » Turning a light on and off via serial connection

November 18, 2011
by stwilliams
stwilliams's Avatar

Hey guys I have been rolling along pretty well with the setup and test projects so far but as programming isn’t one of my strongest skills I think that I might have just hit a simple snag on a project that I am currently working on.

What I am trying to do is get the controller to just turn on or off a single LED when certain commands/keys are given to it via serial connection. Currently I only have 1 LED plugged in but I believe that if I take the LCD off I can put a total of 17 LEDs on the board to individually light up when given a command via serial, if I am reading the information right. Note that I am not trying to do all of them at the same time because of the power constraints, just one at a time.

I have kind of “mixed” the code from the various projects that from what I can tell seem to be what I need. I can compile the following code and send it to the controller for programming with no errors but when I connect to the controller using HyperTerminal like the servo squitter project and press 0 or 1, nothing happens. I have the board wired up exactly like the LED blink project which runs just fine on it. Anyone have any ideas on what I am doing wrong or could push me in the right direction? Any comments or information would be much appreciated.

// Light PC4 test 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/uart.h"

int main() {

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

  char tc;

while(1) {
  // Wait for a character to be sent to the serial port.
    tc = uart_read();
    if(tc=='0') PORTC &= ~(1<<PC4);
    if(tc=='1') PORTC |= (1<<PC4);

  }

  return 0;
}
November 18, 2011
by treymd
treymd's Avatar

The pin direction needs to be set using the data direction register (DDRC). You should see a line in the LED blink code that does this.

November 18, 2011
by Ralphxyz
Ralphxyz's Avatar

stwilliams said:

if I take the LCD off I can put a total of 17 LEDs on the board to individually light up when given a command via serial,

You aught to consider using shift registers you can daisy chain them and get more then 17 while still having the LCD!!

Ralph

November 21, 2011
by stwilliams
stwilliams's Avatar

You are a genius! I can’t believe that I missed something so stupid. I made the change and it is working perfectly now with 1 LED. Thank you so much for your help Trey. Ralph I am sorry I am new to this but I don’t really understand, how would shift registers allow me to connect more than the number of outputs but still just turn on individual lights via command?

November 21, 2011
by Ralphxyz
Ralphxyz's Avatar

stwilliams asked?

how would shift registers allow me to connect more than the number of outputs but still just turn on individual lights via command?

A shift register uses 3 pins on the mcu then a shift register can be cascaded (daisy chained) to more shift registers.

Search the forum for "shift register. claesz was (has made) making a library to use with shift registers and Rick has done a lot of work with them.

Using shift registers is often the next step after you have finished the Nerdkit User Guide and done the Tutorials and Projects.

You can easily daisy chain 4 or 5 shift registers probable more!

Ralph

November 21, 2011
by dgikuljot
dgikuljot's Avatar

@ Ralphxys I dont think he physicially understands what a shift register is.

@stwilliams A shift register is basically an ic. SO esentially you will connect it to the mcu using 3 pins, and then depending on the ic you choose, you will get additional pins you can use as input/output if you write the code correctly. For example some shift registers have 8i/o pins. So it is almost like an expanision pack for input out pins for the mcu. Correct me if I am wrong

November 22, 2011
by Rick_S
Rick_S's Avatar

Shift registers typically take one type of input and shift it into outputs. The inputs and outputs can be serial (such as when we connect the 3 wires to the NK) or parallel (typically 8 wires) They only allow data movement one direction though. Which is good for output. Common usage is in LED arrays such as the one's I've detailed in my blog (haven't updated since July sorry). The advantage here is you can turn off the register's outputs, feed the data to it for which outputs you want high vs. low, then turn on the outputs. It takes a little more programming, but once you get past that hurdle, they come in very handy.

There are also devices called port expanders. They allow for 2 way communication and truely will give you additional i/o for your micro. Again, with more progamming required to make them work well.

Another option - popular with another user here "Noter" - is to just use another micro as a slave device to the primary similar to what the NK guys do with thier multi-panel LED array tutorial.

That is one thing nice about this hobby, there are many ways to do things. Which is best??? The one that works for you. Wink

Rick

Post a Reply

Please log in to post a reply.

Did you know that two resistors can be used to make a voltage divider? Learn more...