This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:motor:servo [2015/11/12 08:16] – heikopikner | en:examples:motor:servo [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Servomotor ====== | ====== Servomotor ====== | ||
| - | //Necessary knowledge: [HW] [[en: | + | //Necessary knowledge: |
| + | [HW] [[en: | ||
| + | [AVR] [[en:avr:timers]], [AVR] [[en: | ||
| + | [LIB] [[en: | ||
| ===== Theory ===== | ===== Theory ===== | ||
| Line 24: | Line 27: | ||
| <code c> | <code c> | ||
| - | // The value of the timer (20 ms)for achieving the full period of PWM. | + | // The value of the timer (20 ms)for achieving the full period of PWM |
| // F_CPU is the clock rate of the microcontroller which is divided with | // F_CPU is the clock rate of the microcontroller which is divided with | ||
| - | // 50 Hz and 8. | + | // 50 Hz and 8 |
| #define PWM_PERIOD | #define PWM_PERIOD | ||
| // Middle position of PWM servo (5 ms / 20 ms) | // Middle position of PWM servo (5 ms / 20 ms) | ||
| - | // Middle position is 15/200 of full period. | + | // Middle position is 15/200 of full period |
| #define PWM_MIDDLE_POS | #define PWM_MIDDLE_POS | ||
| - | // Factor for converting the percents (-100% to 100%)to periods. | + | // Factor for converting the percents (-100% to 100%)to periods |
| // +1 is added to ensure that semi periods would reach to the boundaries | // +1 is added to ensure that semi periods would reach to the boundaries | ||
| - | // of 1 ms and 2 ms or // a little over. | + | // of 1 ms and 2 ms or // a little over |
| #define PWM_RATIO | #define PWM_RATIO | ||
| - | // Set-up of the pins. | + | // Set-up of the pins |
| static pin servo_pins[2] = | static pin servo_pins[2] = | ||
| { | { | ||
| Line 44: | Line 47: | ||
| }; | }; | ||
| - | // Preparing the servo motor for working. | + | // Preparing the servo motor for working |
| void servomotor_init(unsigned char index) | void servomotor_init(unsigned char index) | ||
| { | { | ||
| - | // The pin of PWM signal for output. | + | // The pin of PWM signal for output |
| pin_setup_output(servo_pins[index]); | pin_setup_output(servo_pins[index]); | ||
| - | // Setup of timer 1. | + | // Setup of timer 1 |
| // Prescaler = 8 | // Prescaler = 8 | ||
| // Fast PWM mode, where TOP = ICR | // Fast PWM mode, where TOP = ICR | ||
| - | // OUTA and OUTB to low in comparisson. | + | // OUTA and OUTB to low in comparisson |
| timer1_init_fast_pwm( | timer1_init_fast_pwm( | ||
| TIMER1_PRESCALE_8, | TIMER1_PRESCALE_8, | ||
| Line 61: | Line 64: | ||
| TIMER1_FAST_PWM_OUTPUT_DISABLE); | TIMER1_FAST_PWM_OUTPUT_DISABLE); | ||
| - | // Determining the period by maximum value. | + | // Determining the period by maximum value |
| timer1_set_input_capture_value(PWM_PERIOD); | timer1_set_input_capture_value(PWM_PERIOD); | ||
| } | } | ||
| - | // Determining the position of the servo motor. | + | // Determining the position of the servo motor |
| // The parameter of the position is from -100% to +100%. | // The parameter of the position is from -100% to +100%. | ||
| void servomotor_position(unsigned char index, signed short position) | void servomotor_position(unsigned char index, signed short position) | ||
| Line 87: | Line 90: | ||
| <code c> | <code c> | ||
| - | // Testing program of the motors module of the HomeLab kit. | + | // Testing program of the motors module of the HomeLab kit |
| #include < | #include < | ||
| #include < | #include < | ||
| Line 96: | Line 99: | ||
| short position; | short position; | ||
| - | // Set-up of the ADC. | + | // Set-up of the ADC |
| adc_init(ADC_REF_AVCC, | adc_init(ADC_REF_AVCC, | ||
| - | // Set-up of the motor. | + | // Set-up of the motor |
| servomotor_init(1); | servomotor_init(1); | ||
| - | // Endless loop. | + | // Endless loop |
| while (1) | while (1) | ||
| { | { | ||
| // Reading the position of the potentiometer and | // Reading the position of the potentiometer and | ||
| // converting the range of | // converting the range of | ||
| - | // the servo motor. | + | // the servo motor |
| // For HomeLab II ADC must be read for the corresponding channel, | // For HomeLab II ADC must be read for the corresponding channel, | ||
| // and use the following formula: | // and use the following formula: | ||
| Line 113: | Line 116: | ||
| position = ((short)adc_get_value(15) / 10) - 102 ; | position = ((short)adc_get_value(15) / 10) - 102 ; | ||
| - | // Determining the position of the servo motor. | + | // Determining the position of the servo motor |
| servomotor_position(1, | servomotor_position(1, | ||
| } | } | ||
| } | } | ||
| </ | </ | ||