Blinking LED
From NutWiki
Contents |
Test Environments
| Hardware Comments | Nut/OS 4.6.3 | Nut/OS 4.6.4 | Nut/OS 4.7.4 | Nut/OS 4.8.0 | |
| Ethernut 1.3 H | Additional LED + Resistor required. | OK | OK Binaries | OK Binaries | OK Binaries |
| Ethernut 2.1 B | Additional LED + Resistor required. | OK | OK Binaries | OK Binaries | OK Binaries |
| Ethernut 3.0 E | OK | OK Binaries | OK Binaries | OK Binaries | |
| EIR 1.0 | Additional LED + Resistor required. | OK | OK Binaries | OK Binaries | OK Binaries |
| Compiler: ARM-GCC 4.2.2 ; AVR-GCC 4.3.0 | |||||
Description
Due to technical differences, each board requires a different program to make the LED blink. This document provides individual examples for each of them.
Note that only Ethernut 3 has an on-board User LED. For the other boards you need an extra LED + series resistor.
When using a standard LED the resistor may have a value of 200 Ohm to 1 kOhm. Note, that LEDs do have a polarity and typically one of the two leads is longer than the other one. The longer lead is the anode and will be connected to the resistor.

Ethernut 1.3 / Ethernut 2.1
Description
On these boards, we use pin 2 of PORT E which is reffered to as Pin 41 in the hardware manuals as our digital output pin and pin 3 as +5V Power Supply.
Connect the resistor to pin 3 and the LEDs cathode to pin 41.
Source Code
#include <sys/timer.h> int main(void) { sbi(DDRE, 2); for(;;){ cbi(PORTE, 2); NutSleep(500); sbi(PORTE, 2); NutSleep(500); } return 0; }
Output
Blinking LED
Details
#include <sys/timer.h>provides a library, required by NutSleep().
sbi(DDRE, 2);
sets bit 2 of the Data direction register E high, which makes the Pin work as an output.
Within the endless loop,
cbi(PORTE, 2);
drives bit (pin) 2 of Port E low, which makes the LED lit.
NutSleep(500);
After a NutSleep() (delay) of 500ms,
sbi(PORTE, 2);
drives Pin 2 of Port E high again, swiching the LED off.
After another NutSleep() of 500ms the loop continues cycling.
Ethernut 3
Description
Ethernut 3 provides an on-board User LED (green LED integrated in the reset button.)
Source Code
#include <dev/npl.h> #include <dev/npluled.h> #include <sys/timer.h> int main(void) { for(;;){ NplUledCntl(ULED_ON); NutSleep(500); NplUledCntl(ULED_OFF); NutSleep(500); } return 0; }
Output
Blinking LED
Details
#include <dev/npl.h> #include <dev/npluled.h> #include <sys/timer.h>
provide prototypes, of functions used later.
Within the endless loop,
NplUledCntl(ULED_ON);
switches the LED ON.
NutSleep(500);
After a NutSleep() (delay) of 500ms,
NplUledCntl(ULED_OFF);
switches the LED OFF again.
After another NutSleep() of 500ms the loop continues cycling.
Elektor Internet Radio 1.0
Description
On the EIR we use pin 1 of port A (pin 2 of header K1) as digital output and pin 34 of header K1 as +3,3V power supply.
Connect the resistor to pin 34 of header K1, the LEDs cathode to pin 2 of header K1.
Source Code
#include <sys/timer.h> int main(void) { outr(PIOA_PER, 2); outr(PIOA_OER, 2); for (;;){ outr(PIOA_CODR, 2); NutSleep(500); outr(PIOA_SODR, 2); NutSleep(500); } return 0; }
Output
Blinking LED
Details
#include <sys/timer.h>provides a header file, required by NutSleep().
Within the main() function,
outr(PIOA_PER, 2);
enables PIO (Parallel Input Output Controller) at port A bit 1. This disables any peripheral function on this port bit.
outr(PIOA_OER, 2);
configures port A bit 1 as an output.
Within the endless loop,
outr(PIOA_CODR, 2);
drives bit (pin) 1 of port A low, which makes the LED lit.
NutSleep(500);
After a NutSleep() (delay) of 500ms,
outr(PIOA_SODR, 2);
drives Pin 1 of Port A high again, swiching the LED off.
After another NutSleep() of 500ms the loop continues cycling.
(To better understand this commands, have a look at chapter 34 of Atmels documentation on the AT91SAM7SE512.)
See also
- More Nut/OS Examples
| Languages: |
English |
