This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:software:homelab:library:pin [2010/03/08 13:09] – mikk.leini | en:software:homelab:library:pin [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Pins ====== | ====== Pins ====== | ||
| - | Pins library provides an easy way to operate | + | Pins library provides an easy way for operating |
| - | ===== Data types ===== | + | ===== Data Types ===== |
| - | * **//pin//** \\ Data type to hold pin registers addresses and bit mask. To get the most efficent program, //pin// typed variables should be constant and they should be initialized at the beginning of the program code. Intializing can be done with macro function // | + | * **//pin//** \\ Data type to hold pin registers addresses and bit mask. To get the most efficent program, //pin// typed variables should be constant, and they should be initialized at the beginning of the program code. Intializing can be done with the macro function // |
| + | ===== Constants ===== | ||
| + | * **// | ||
| + | * **//LED1, LED2, LED3//** - Homelab User interface board LEDs by LED numbers | ||
| + | * **//S1, S2, S3//** - Homelab User interface board buttons | ||
| + | | ||
| ===== Functions ===== | ===== Functions ===== | ||
| Line 12: | Line 17: | ||
| Configures pin as an output. Parameters: | Configures pin as an output. Parameters: | ||
| * //pin// - Pin variable. | * //pin// - Pin variable. | ||
| - | * **//void pin_setup_input(pin pin)//** \\ Viigu sisendiks määramine ilma //pull-up// takistita. Parameetrid: | + | * **//void pin_setup_input(pin pin)//** \\ |
| - | * //pin// - Viigu muutuja. | + | Configures pin as an input without |
| - | * **//void pin_setup_input_with_pullup(pin pin)//** \\ Viigu sisendiks määramine koos //pull-up// | + | * //pin// - Pin variable. |
| - | * //pin// - Viigu muutuja. | + | * **//void pin_setup_input_with_pullup(pin pin)//** \\ |
| - | * **//void pin_set(pin pin)//** \\ Viigu väljundi kõrgeks määramine. Parameetrid: | + | Configures pin as an input with pull-up |
| - | * //pin// - Viigu muutuja. | + | * //pin// - Pin variable. |
| - | * **//void pin_clear(pin pin)//** \\ Viigu väljundi madalaks määramine. Parameetrid: | + | * **//void pin_set(pin pin)//** \\ |
| - | * //pin// - Viigu muutuja. | + | Sets output pin high. Parameters: |
| - | * **//void pin_toggle(pin pin)//** \\ Viigu väljundi oleku pööramine. Madal olek muutub kõrgeks ja vastupidi. Parameetrid: | + | * //pin// - Pin variable. |
| - | * //pin// - Viigu muutuja. | + | * **//void pin_clear(pin pin)//** \\ |
| - | * **//void pin_set_to(pin pin, bool value)//** \\ Viigu väljundi parameetriga määratud olekusse viimine. Parameetrid: | + | Sets output pin low. Parameters: |
| - | * //pin// - Viigu muutuja. | + | * //pin// - Pin variable. |
| - | * //value// - Tõeväärtuse muutuja. | + | * **//void pin_toggle(pin pin)//** \\ |
| - | * **//bool pin_get_value(pin pin)//** \\ Viigu sisendi oleku lugemine ja funktsiooniga tagastamine. Parameetrid: | + | Inverts output pin state. Parameters: |
| - | * //pin// - Viigu muutuja. | + | * //pin// - Pin variable. |
| - | * Tagastab tõeväärtuse. | + | * **//void pin_set_to(pin pin, bool value)//** \\ |
| - | * **//bool pin_get_debounced_value(pin pin)//** \\ Viigu sisendi oleku lugemine läbi lüliti väreluse filtri ja selle tagastamine. Filtreerimine toimub minimaalselt | + | Sets output pin to desired state. Parameters: |
| - | * //pin// - Viigu muutuja. | + | * //pin// - Pin variable. |
| - | * Tagastab tõeväärtuse. | + | * //value// - Desired state boolean value. |
| + | * **//bool pin_get_value(pin pin)//** \\ | ||
| + | Gets pin value. Parameters: | ||
| + | * //pin// - Pin variable. | ||
| + | * Return boolean //true// when pin is high and //false// when pin is low. | ||
| + | * **//bool pin_get_debounced_value(pin pin)//** \\ | ||
| + | Reads pin value through the switch debounce filter. Filtering takes at least 8 ms and may last up to 100 ms, depending on when the bouncing ends. If the bouncing does not end, // | ||
| + | * //pin// - Pin variable. | ||
| + | * Return pin boolean value - //true// when pin is high and //false// when pin is low or undetermined. | ||
| - | ===== Näide | + | ===== Example |
| - | Näide sellest, kuidas ühe viigu väärtus teha sõltuvaks teisest. Programmis omandab viik PC3 viigule | + | Example of getting and setting a pin's value. Pin PC0 value is inverted and attached to pin PC3. |
| <code c> | <code c> | ||
| Line 46: | Line 59: | ||
| bool value; | bool value; | ||
| - | // Viigu väljundiks seadistamine | + | // Configuring pin as an output pin |
| pin_setup_output(output_pin); | pin_setup_output(output_pin); | ||
| - | // Viigu pull-up | + | // Configuring pin as an input pin with pull-up |
| pin_setup_input_with_pullup(input_pin); | pin_setup_input_with_pullup(input_pin); | ||
| - | // Lõputu tsükkel | + | // Endless loop |
| while (true) | while (true) | ||
| { | { | ||
| - | // Sisendviigu väärtuse lugemine | + | // Getting an input pin value |
| value = pin_get_value(input_pin); | value = pin_get_value(input_pin); | ||
| - | // Väljundviigule vastupidise väärtuse omistamine | + | // Setting an output pin value |
| pin_set_to(output_pin, | pin_set_to(output_pin, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | led_on, led_off, button_read commands example | ||
| + | <code c> | ||
| + | #include < | ||
| + | |||
| + | // Homelab buttons and LEDs are predefined in the library | ||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | // Set LED pin as output | ||
| + | pin_setup_output(led_green); | ||
| + | |||
| + | // Set Button S1 pin as input | ||
| + | pin_setup_input(S1); | ||
| + | |||
| + | // Endless loop | ||
| + | while (true) | ||
| + | { | ||
| + | // If button is pressed, turn on LED, if not pressed, turn LED off. | ||
| + | if(button_read(S1) == true) | ||
| + | led_on(led_green); | ||
| + | else | ||
| + | led_off(led_green); | ||
| } | } | ||
| } | } | ||
| </ | </ | ||