This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:examples:digi:led [2015/11/03 06:07] – raivo.sell | en:examples:digi:led [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ~~PB~~ | + | < |
====== Light-emitting Diode ====== | ====== Light-emitting Diode ====== | ||
- | //Necessary knowledge: [HW] [[en: | + | //Necessary knowledge: |
+ | [HW][[en: | ||
+ | [ELC][[en:electronics:led_resistor]], | ||
+ | [AVR][[en:avr:registers]], [AVR] [[en:avr:io]], | ||
+ | [LIB][[en:software:homelab: | ||
===== Theory ===== | ===== Theory ===== | ||
Line 22: | Line 26: | ||
===== HomeLab Practice ===== | ===== HomeLab Practice ===== | ||
- | The HomeLab controller control module has one single | + | The HomeLab controller control module has one single |
All practical examples for the HomeLab kit, LED switching included, use HomeLab’s pin library. Pin library includes data type //pin//, which contains addresses of pin related registers and pin bitmask. If to create a pin type variable in the program and then initialize it by using macro function PIN, the pin can be used freely with this variable (pin) through whole program without being able to use registers. | All practical examples for the HomeLab kit, LED switching included, use HomeLab’s pin library. Pin library includes data type //pin//, which contains addresses of pin related registers and pin bitmask. If to create a pin type variable in the program and then initialize it by using macro function PIN, the pin can be used freely with this variable (pin) through whole program without being able to use registers. | ||
- | Here are 2 example programs, which are doing exactly the same thing, but one is created on the basis of HomeLab’s library, the other is not. | + | Here are 2 example programs, which are doing exactly the same thing, but one is created on the basis of HomeLab’s library, the other is not. The debug LED, led_debug in HomeLab library, has been described as PB7 (HomeLab I & II) and PQ2 (HomeLab III). The Debug LED is physically located in the Controller module. |
<code c> | <code c> | ||
- | // | + | // HomeLab Controller module LED test program, which |
- | // HomeLab Controller module LED test program. | + | // is based on HomeLab library |
- | // The code is base on HomeLab library. | + | |
- | // | + | |
#include < | #include < | ||
- | // | ||
// LED pin configuration. | // LED pin configuration. | ||
- | // | + | pin led_debug |
- | pin debug_led | + | |
- | // | ||
// Main program | // Main program | ||
- | // | ||
int main(void) | int main(void) | ||
{ | { | ||
// Configuring LED pin as an output | // Configuring LED pin as an output | ||
- | pin_setup_output(debug_led); | + | pin_setup_output(led_debug); |
// Lighting up LED | // Lighting up LED | ||
- | pin_clear(debug_led); | + | pin_clear(led_debug); |
} | } | ||
</ | </ | ||
<code c> | <code c> | ||
- | // | + | // HomeLab |
- | // HomeLab Controller module LED test program. | + | // accesses |
- | // The code directly | + | |
- | // | + | |
#include < | #include < | ||
- | // | ||
// Main program | // Main program | ||
- | // | ||
int main(void) | int main(void) | ||
{ | { | ||
Line 79: | Line 73: | ||
<code c> | <code c> | ||
- | // Macro constant for defining register pointer | + | // Defining the Pins inside the pin struct |
- | #define _REG_PTR_ volatile uint8_t * | + | // pin name = PIN(PORT LETTER, PIN NUMBER IN PORT); |
+ | pin led_green = PIN(H,5); | ||
- | // | ||
- | // Pin data type | ||
- | // | ||
- | typedef struct pin | ||
- | { | ||
- | _REG_PTR_ ddr; | ||
- | _REG_PTR_ port; | ||
- | _REG_PTR_ pin; | ||
- | uint8_t mask; | ||
- | } | ||
- | pin; | ||
- | |||
- | // | ||
- | // Macro function for pin variable initializing | ||
- | // | ||
- | #define PIN(port_char, | ||
- | { \ | ||
- | (_REG_PTR_)& | ||
- | (_REG_PTR_)& | ||
- | (_REG_PTR_)& | ||
- | bit_mask(bit_index) \ | ||
- | } | ||
- | |||
- | // | ||
// Configuring pin as output | // Configuring pin as output | ||
- | // | + | inline void pin_setup_output(pin pin){ |
- | inline void pin_setup_output(pin pin) | + | |
- | { | + | |
bitmask_set(*pin.ddr, | bitmask_set(*pin.ddr, | ||
} | } | ||
- | // | ||
// Setting pin high | // Setting pin high | ||
- | // | + | inline void pin_set(pin pin){ |
- | inline void pin_set(pin pin) | + | |
- | { | + | |
bitmask_set(*pin.port, | bitmask_set(*pin.port, | ||
} | } | ||
- | // | + | |
// Setting pin low | // Setting pin low | ||
- | // | + | inline void pin_clear(pin pin){ |
- | inline void pin_clear(pin pin) | + | |
- | { | + | |
bitmask_clear(*pin.port, | bitmask_clear(*pin.port, | ||
} | } | ||
Line 131: | Line 95: | ||
In addition to the Controller module, LEDs are also located on the User interface module board. They are connected electrically in the same way as Controller module’s LED, which means cathode is connected to the AVR pin. For more information see the modules hardware guide. | In addition to the Controller module, LEDs are also located on the User interface module board. They are connected electrically in the same way as Controller module’s LED, which means cathode is connected to the AVR pin. For more information see the modules hardware guide. | ||
- | In addition to //pin_set// and // | + | In addition to //pin_set// and // |
- | Their HomeLab | + | |
^Constant name^Alternative name ^ HomeLab I & II pin^HomeLab III pin^Description^ | ^Constant name^Alternative name ^ HomeLab I & II pin^HomeLab III pin^Description^ | ||
Line 140: | Line 103: | ||
|led_red|LED3|PC5|PH3| Red LED| | |led_red|LED3|PC5|PH3| Red LED| | ||
+ | HomeLab library based example program which uses LEDs constants looks as follows: | ||
+ | <code c> | ||
- | <code c> | + | // LED test program for HomeLab User interface module |
- | // | + | |
- | // HomeLab User interface module | + | |
- | // | + | |
#include < | #include < | ||
- | // | ||
// Main program | // Main program | ||
- | // | ||
int main(void) | int main(void) | ||
{ | { | ||
Line 158: | Line 118: | ||
pin_setup_output(led_green); | pin_setup_output(led_green); | ||
- | // Lighting up green LED | + | // Lighting up red and green LED |
+ | led_on(led_red); | ||
led_on(led_green); | led_on(led_green); | ||
- | + | | |
- | // Turn off red and yellow LED | + | |
- | led_off(led_red); | + | |
led_off(led_yellow); | led_off(led_yellow); | ||
} | } | ||
</ | </ | ||
- | |||
- | ~~DISCUSSION~~ |