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:sensor:potentiometer [2015/11/09 12:52] heikopikneren:examples:sensor:potentiometer [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
-~~PB~~ +<pagebreak> 
-====== Potentsiometer ======+====== Potentiometer ======
  
-//Necessary knowledge : [HW] [[en:hardware:homelab:sensor]], [HW] [[en:hardware:homelab:digi]], [ELC] [[en:electronics:voltage_divider]], [AVR] [[en:avr:adc]], [LIB] [[en:software:homelab:library:adc]], [LIB] [[en:software:homelab:library:module:segment_display]]//+//Necessary knowledge:  
 +[HW] [[en:hardware:homelab:digi]], 
 +[ELC] [[en:electronics:voltage_divider]], 
 +[AVR] [[en:avr:adc]],  
 +[LIB] [[en:software:homelab:library:adc]], [LIB] [[en:software:homelab:library:module:segment_display]]//
  
 ===== Theory ===== ===== Theory =====
Line 15: Line 19:
 [{{  :examples:sensor:pote.png?250|Single-turn potentiometer}}] [{{  :examples:sensor:pote.png?250|Single-turn potentiometer}}]
  
-On the module of the HomeLab is a 10 kΩ turn potentiometer. This potentiometer is connected between ground and +5 V potentials and the slider is connected to the channel 3 of the analogue-digital converter. With this connection, the output voltage of the potentiometer can be regulated between 0 V and 5 V. The digital value of the potentiometer output voltage on its entire adjusting range can be measured if the comparison voltage from AVR digital-analogue converter is taken from AVCC pin. The following function for AVR ADC are in the library of the HomeLab:    +On the module of the HomeLab is a 10 kΩ turn potentiometer. This potentiometer is connected between ground and microcontroller supply potentials and the slider is connected to the analogue-digital converter (ADC). With this connection, the output voltage of the potentiometer can be regulated between 0 V and microcontroller supply. The digital value of the potentiometer output voltage on its entire adjusting range can be measured if the comparison voltage from AVR digital-analogue converter is taken from AVCC pin. For the Homelab III, the maximum voltage of the AVCC pin is 2.7 V but the inputs can deliver 3.3 volts. The following function for AVR ADC are in the library of the HomeLab:
  
-~~CL~~+  * void adc_init(reference, prescale)  
 +  * unsigned short adc_get_value(channel)  
 +  * unsigned short adc_get_average_value(channel, num_samples) 
  
-<code sql> +The function //adc_init// must be called out in the beginning of the program, this is used for making the ADC to work. The reference voltage must be chosen from either AREF pin or AVCC pin, or fixed inner voltage must be selected. In addition the clock cycle of the converter must be set by the prescaler (factor of frequency divider), which will be used to divide the controller clock cycle. The conversion is quicker when using higher clock cycle but with this the accuracy may suffer. Function //adc_get_value// is for measuringthis able to select the channel and it returns 10 bit (11-bit for Homelab III) results. The function for measuring is inter lockinghence it waits for the end of conversion and returns the results only after all measuring is done.
-// +
-// Data types for adjustment +
-// +
-typedef enum +
-+
- ADC_REF_AREF = 0x00, +
- ADC_REF_AVCC = 0x01, +
- ADC_REF_2V56 = 0x03 +
-+
-adc_reference;+
  
-typedef enum +The following functions of the library are provided to support the ATmega2561 ADC converter.
-+
- ADC_PRESCALE_2   = 0x01, +
- ADC_PRESCALE_4   = 0x02, +
- ADC_PRESCALE_8   = 0x03, +
- ADC_PRESCALE_16  = 0x04, +
- ADC_PRESCALE_32  = 0x05, +
- ADC_PRESCALE_64  = 0x06, +
- ADC_PRESCALE_128 = 0x07 +
-+
-adc_prescale;+
  
-//+~~CL~~ 
 + 
 +<code c>
 // Starting the ADC // Starting the ADC
-// 
 void adc_init(adc_reference reference, adc_prescale prescale) void adc_init(adc_reference reference, adc_prescale prescale)
 { {
Line 55: Line 42:
 } }
  
-// 
 // Converting the values of selected channel // Converting the values of selected channel
-// 
 unsigned short adc_get_value(unsigned char channel) unsigned short adc_get_value(unsigned char channel)
 {  {
Line 76: Line 61:
 } }
 </code> </code>
-The function //adc_init// must be called out in the beginning of the program, this is used for making the ADC to work. The reference voltage must be chosen from either AREF pin or AVCC pin, or fixed inner voltage of 2.56 V must be selected. In addition the clock cycle of the converter must be set by the prescaler (factor of frequency divider), which will be used to divide the controller clock cycle. The conversion is quicker when using higher clock cycle but with this the accuracy may suffer. Function //adc_get_value// is for measuring, this able to select the channel and it returns 10 bit results as 16 bit integer. The function for measuring is inter locking, hence it waits for the end of conversion and returns the results only after all measuring is done. 
  
-In previously explained example program analogue-digital converter and 7 segment number indicator library are used. The 10 bit value of analogue-digital converter is multiplied by 10 and divided by 1024 to get the value between 0 and 9. The value 10 is impossible to reach because while dividing in C-language only integer value is calculated and not rounded result. Function of averaging the result of converter is used to get more accurate result. Derived from this the operating program shows the numbers 0 to 9, which correspond to the position of the potentiometer on the indicator.+In previously explained example program analogue-digital converter and 7 segment number indicator library are used. The value of analogue-digital converter is multiplied by 10 and divided by 2048 to get the value between 0 and 9. The value 10 is impossible to reach because while dividing in C-language only integer value is calculated and not rounded result. Function of averaging the result of converter is used to get more accurate result. Derived from this the operating program shows the numbers 0 to 9, which correspond to the position of the potentiometer on the indicator.
  
 +It should be noticed where is connected to the analog potentiometer in the particular hardware. For example, it is connected to an analog channel 3 in case of the Homelab Sensor Module II and analog channel 15th in case of the Home Labor III Interface module.
  
 <code c> <code c>
-// 
 // Example program of potentiometer on the Sensor module // Example program of potentiometer on the Sensor module
 // The position of the potentiometer is displayed on the 7-segment indicator // The position of the potentiometer is displayed on the 7-segment indicator
-// 
 #include <homelab/adc.h> #include <homelab/adc.h>
 #include <homelab/module/segment_display.h> #include <homelab/module/segment_display.h>
Line 95: Line 78:
 #define ADC_CHANNEL 15 #define ADC_CHANNEL 15
  
-// 
 // Main program // Main program
-// 
 int main(void) int main(void)
 { {
Line 106: Line 87:
  
  // Adjusting ADC  // Adjusting ADC
- adc_init(ADC_REF_AVCC, ADC_PRESCALE_8);+ adc_init(ADC_REF_AVCC, ADC_PRESCALE_16);
  
  // Endless loop  // Endless loop
- while (true)+ while (1)
  {  {
  // Reading 4 times rounded values of the channel  // Reading 4 times rounded values of the channel
en/examples/sensor/potentiometer.1447073561.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