Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:examples:digi:led [2015/11/03 06:07] raivo.sellen:examples:digi:led [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
-~~PB~~+<pagebreak>
 ====== Light-emitting Diode ====== ====== Light-emitting Diode ======
  
-//Necessary knowledge: [HW] [[en:hardware:homelab:controller]], [HW] [[en:hardware:homelab:digi]], [ELC] [[en:electronics:led_resistor]], [AVR] [[en:avr:registers]], [AVR] [[en:avr:io]], [LIB] [[en:software:homelab:library:pin]]//+//Necessary knowledge:  
 +[HW][[en:hardware:homelab:digi]],  
 +[ELC][[en:electronics:led_resistor]], 
 +[AVR][[en:avr:registers]], [AVR] [[en:avr:io]],  
 +[LIB][[en:software:homelab:library:bit]], [LIB][[en:software:homelab:library:pin]]//
  
 ===== Theory ===== ===== Theory =====
Line 22: Line 26:
 ===== HomeLab Practice ===== ===== HomeLab Practice =====
  
-The HomeLab controller control module has one single red LED, whose anode is connected through resistor to a +5 V power supply and the cathode is connected to the ATmega128 pin number PB7. In order to switch on and off this LED, PB7 should be defined as the output pin and set low or high accordingly. Which means if the pin is set high, the LED is turned off and if the pin is set low, the LED is turned on. Basically it would be possible to connect the LED also so that the anode is connected to the pin of microcontroller, and the cathode is connected to the earth (somewhere there has to be a resistor too) – in that case when the pin is set as high, the LED shines and when the pin is set as low the LED is switched off.+The HomeLab controller control module has one single indicator LED, whose anode is connected through resistor to a power supply and the cathode is connected to the controllers pin. In order to switch on and off this LED, LED pin should be defined as the output and set low or high accordingly. Which means if the pin is set high, the LED is turned off and if the pin is set low, the LED is turned on. Basically it would be possible to connect the LED also so that the anode is connected to the pin of microcontroller, and the cathode is connected to the earth (somewhere there has to be a resistor too) – in that case when the pin is set as high, the LED shines and when the pin is set as low the LED is switched off.
  
 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 <homelab/pin.h> #include <homelab/pin.h>
  
-// 
 // LED pin configuration. // LED pin configuration.
-// +pin led_debug = PIN(Q,2);
-pin debug_led = PIN(B7);+
  
-// 
 // 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> </code>
  
 <code c> <code c>
-// +// HomeLab II Controller module LED test program, which 
-// HomeLab Controller module LED test program. +// accesses  registers directly
-// The code directly accesses registers+
-//+
 #include <avr/io.h> #include <avr/io.h>
  
-// 
 // 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, bit_index) \ 
-{ \ 
- (_REG_PTR_)&DDR ## port_char, \ 
- (_REG_PTR_)&PORT ## port_char, \ 
- (_REG_PTR_)&PIN ## port_char, \ 
- 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, pin.mask);  bitmask_set(*pin.ddr, pin.mask);
 } }
    
-// 
 // Setting pin high // Setting pin high
-// +inline void pin_set(pin pin){
-inline void pin_set(pin pin) +
-{+
  bitmask_set(*pin.port, pin.mask);  bitmask_set(*pin.port, pin.mask);
 } }
    
-//+
 // Setting pin low // Setting pin low
-// +inline void pin_clear(pin pin){
-inline void pin_clear(pin pin) +
-{+
  bitmask_clear(*pin.port, pin.mask);  bitmask_clear(*pin.port, pin.mask);
 } }
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 //pin_clear// commands one can use //led_on// and //led_off// commands to control LED pins. +In addition to //pin_set// and //pin_clear// commands one can use //led_on// and //led_off// commands to control LED pins. The following table shows LEDs constants which are described in the library and the corresponding Controller module pins. Green, yellow and red LEDs are located in the user interface module.
-Their HomeLab library based example program looks as follows: +
  
 ^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 LED test program. +
-//+
 #include <homelab/pin.h> #include <homelab/pin.h>
  
-// 
 // 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 yellow LED
- // Turn off red and yellow LED +
- led_off(led_red);+
  led_off(led_yellow);  led_off(led_yellow);
 } }
 </code> </code>
- 
-~~DISCUSSION~~ 
en/examples/digi/led.1446530848.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0