Difference between revisions of "Blinking LED"

From Nutwiki
Jump to: navigation, search
m (Test Environments)
 
m (1 revision imported)
 
(No difference)

Latest revision as of 17:02, 27 October 2016

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.

File:Ledsch.png

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

<source lang="c">

  1. include <sys/timer.h>

int main(void) { sbi(DDRE, 2);

   for(;;){
       cbi(PORTE, 2);
       NutSleep(500);
       sbi(PORTE, 2);
       NutSleep(500);
   }
   return 0;

} </source>

Output

Blinking LED

Details

<source lang="c">

  1. include <sys/timer.h>

</source> provides a library, required by NutSleep().

<source lang="c"> sbi(DDRE, 2); </source>

sets bit 2 of the Data direction register E high, which makes the Pin work as an output.

Within the endless loop,
<source lang="c"> cbi(PORTE, 2); </source> drives bit (pin) 2 of Port E low, which makes the LED lit. <source lang="c"> NutSleep(500); </source> After a NutSleep() (delay) of 500ms, <source lang="c"> sbi(PORTE, 2); </source> 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

<source lang="c">

  1. include <dev/npl.h>
  2. include <dev/npluled.h>
  3. include <sys/timer.h>

int main(void) {

   for(;;){
       NplUledCntl(ULED_ON);
       NutSleep(500);
       NplUledCntl(ULED_OFF);
       NutSleep(500);
   }

return 0; } </source>

Output

Blinking LED

Details

<source lang="c">

  1. include <dev/npl.h>
  2. include <dev/npluled.h>
  3. include <sys/timer.h>

</source> provide prototypes, of functions used later.

Within the endless loop,

<source lang="c"> NplUledCntl(ULED_ON); </source>

switches the LED ON.

<source lang="c"> NutSleep(500); </source>

After a NutSleep() (delay) of 500ms,

<source lang="c"> NplUledCntl(ULED_OFF); </source>

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

<source lang="c">

  1. 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;

} </source>

Output

Blinking LED

Details

<source lang="c">

  1. include <sys/timer.h>

</source> provides a header file, required by NutSleep(). Within the main() function,

<source lang="c"> outr(PIOA_PER, 2); </source>

enables PIO (Parallel Input Output Controller) at port A bit 1. This disables any peripheral function on this port bit.

<source lang="c"> outr(PIOA_OER, 2); </source>

configures port A bit 1 as an output.

Within the endless loop,

<source lang="c"> outr(PIOA_CODR, 2); </source>

drives bit (pin) 1 of port A low, which makes the LED lit.

<source lang="c"> NutSleep(500); </source>

After a NutSleep() (delay) of 500ms,

<source lang="c"> outr(PIOA_SODR, 2); </source>

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