Table of Contents

Encoders

Related to modules: [HW] Motor Module

This library includes usage functions of Homelab's encoders.

Functions

  • void encoder_init(unsigned char index)

configures selected encoder and the input pins. Parameters:

  • index - Number of the encoder. Value 0 to 1.
  • void encoder_reset_pulses(unsigned char index)

Resets and starts counting the selected encoder. Parameters:

  • index - Number of the encoder. Value 0 to 1.
  • unsigned short encoder_get_pulses(unsigned char index)

Request of counted encoder impulses. Parameters:

  • index - Number of the encoder. Value 0 to 1.
  • Returns the number of pulses

 

Example

//
// Example of using the Homelab's encoders.
// The number of pulses is displayed on LCD.
//
#include <stdio.h>
#include <homelab/module/lcd_gfx.h>
#include <homelab/delay.h>
#include <homelab/module/encoders.h>
#include <homelab/module/buzzer.h>
#include <homelab/pin.h>
 
// Button pin
pin button2 = PIN(C, 1);
 
 
int main(void)
{
	unsigned short pulses = 0;
	char text[16];
 
	// Sets buttons
	pin_setup_input_with_pullup(button2);
 
	// Initialization of encoder 
	encoder_init(0);
 
	// Reset and start counting
	encoder_reset_pulses(0);
 
	// LCD display initialization
	lcd_gfx_init();
 
	// Clear display
	lcd_gfx_clear();
 
	// Turning back light ON
	lcd_gfx_backlight(true);	
 
	// Displaying source code name
	lcd_gfx_goto_char_xy(3, 1);
	lcd_gfx_write_string("Encoder");
 
	while (true)
	{
		pulses = encoder_get_pulses(0);
 
 
		// Generating a string
		sprintf(text, "Pulsse: %d",pulses);
 
 
		lcd_gfx_goto_char_xy(0, 3);
		lcd_gfx_write_string(text);
 
 
		// Button 2 is pressed
	    if(!pin_get_debounced_value(button2))
		{
			// Reset and start counting
			encoder_reset_pulses(0);
 
			// Make a beep
			buzzer_sound (60, 100);
 
			// Erase previous number
			lcd_gfx_write_string("    ");
		} 
 
		// Software delay for 10 ms 
		sw_delay_ms(10);
	}
}
en/software/homelab/library/module/encoder.txt · Last modified: 2018/12/27 12:53 (external edit)
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