|
Flying Electron Application Note 1003
LED Simple Example
Background
This application note will demonstrate simple LED control techniques to light LEDs in a very simple
animated sequence. This application note uses the Flying Electron FE_M168 Development Board and the Flying
Electron FE_LED8 LED Indicator Adapter Board 8 Channel. This application note can also be used as a reference
for any AVR microcontroller development board driving LEDs.
Parts Needed
- Flying Electron FE_M168
- Flying Electron FE_LED8
|
|
|
|
|
Flying Electron FE_M168
Click to Enlarge
|
|
Flying Electron FE_LED8
Click to Enlarge
|
|
Software Needed
- Atmel AVR Studio 4
- WinAVR
Hardware Setup
Note: Please use the figure below labeled "Flying Electron Application Note 1003 Hardware Setup"
as a reference when following the hardware setup steps.
- Connect the FE_M168 PORTB HEADER
to the FE_LED8 IO PORT HEADER as shown in the diagram below.
Note: Either of the FE_LED8's two IO PORT HEADERs can be used, however, each pin
of the FE_M168 PORTB HEADER should be connected to the corresping pin of the FE_LED8 IO PORT HEADER. i.e.
pin 0 to pin 0, pin 1 to pin 1, etc.
- Plug the FE_M168's Power Adapter into the FE_M168 POWER JACK
Sample Code
A new project should be created in AVR Studio 4 using the following parameters:
| Project Type: | AVR GCC |
| Project Name: | FE_AN1003 |
| Project Location: | C:\FE_AN1003 |
After the project has been created, the code below should be placed in the "C:\FE_AN1003\FE_AN1003.c" file
created by the AVR Studio 4 project wizard.
The sample code below begins by initializing PORTB to be an output port and setting the initial value of PORTB to be
0x01. After initialization there is a loop which will run forever. Inside the loop, the value of PORTB is
left shifted by 1 bit, then a check is made to reset the value of PORTB if no bits are set after the left shifting operation, and finally a delay
is added to make the shifting of the bits slow enough to be seen by the human eye.
Because the FE_LED8 board is connected to the FE_M168 PORTB HEADER, the LEDs on the FE_LED8 will show the
current value of PORTB. The leds on the FE_LED8 are numbered from 0 to 7 which correspond to the bits 0 to 7
of PORTB. Bits that are high in the value of PORTB will cause the corresponding LED to be lit. Bits that
are low in the value of PORTB will cause the corresponding LED to be dark.
Note: The sample code and sample project are available for download in the Download section of this application note.
/****************************************************************************
* Flying Electron Inc. Application Note 1003
* LED Simple Example
* Copyright 2007 Flying Electron Inc.
****************************************************************************/
// --------------------------------------------------------------------------
// Includes
// --------------------------------------------------------------------------
#include <avr/io.h> // Include the standard AVR IO definitions
#include <stdio.h> // Include the standard IO library
// --------------------------------------------------------------------------
// Main
// --------------------------------------------------------------------------
int main(void) {
// Set PORTB to be output
DDRB = 0xFF;
// Initialize PORTB with starting value
PORTB = 0x01;
// Loop Forever
while (1) {
// Variables
uint32_t i;
// Left Shift PORTB by 1 bit
PORTB = PORTB << 1;
// Load PORTB with initial value if bit has been left shifted
// out of PORTB
if (PORTB == 0x00) PORTB = 0x01;
// Delay by looping and wasting cycles
for (i = 0; i < 10000; i++) {
}
}
}
The AtMega168 fuses listed in the table below should be set in order for the sample code to run properly.
Fuses other then those listed in the table below should be cleared.
 | Boot Flash section size=1024 words, Boot start address=$1C00 |  | Brown-out detection disabled |  | Int. RC Osc. 8Mhz; Start-up time PWRDWN/RESET: 6 CK / 14CK + 65ms |
For more information on how to set the fuses and perform an ISP programming operation on the FE_M168 to run the
sample code, please see the following application notes:
| FE_AN1000 | In System Programming (ISP) using the Atmel STK500 |
| FE_AN1001 | In System Programming (ISP) using the Atmel AVRISP mkII |
Downloads
|