This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:motor:dc [2015/11/11 14:25] – heikopikner | en:examples:motor:dc [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ~~PB~~ | + | < |
| ====== DC motor ====== | ====== DC motor ====== | ||
| - | //Necessary knowledge: [HW] [[en: | + | //Necessary knowledge: |
| + | [HW] [[en: | ||
| + | [AVR] [[en: | ||
| + | [LIB] [[en: | ||
| ===== Theory ===== | ===== Theory ===== | ||
| Line 61: | Line 64: | ||
| <code c> | <code c> | ||
| - | // The setup of the pins driving pins. | + | // The setup of the pins driving pins |
| static pin dcmotor_pins[4][2] = | static pin dcmotor_pins[4][2] = | ||
| { | { | ||
| Line 87: | Line 90: | ||
| pwm = PWMDEFAULT; | pwm = PWMDEFAULT; | ||
| - | // Kõigi kanalite käivitamine | + | // Starting all channels |
| for(i=0 ; i<CHMAX ; i++) | for(i=0 ; i<CHMAX ; i++) | ||
| { | { | ||
| - | // PWM-i algväärtustamine | + | // PWM state variable initialization |
| compare[i] = pwm; | compare[i] = pwm; | ||
| compbuff[i] = pwm; | compbuff[i] = pwm; | ||
| } | } | ||
| - | // Timer 2 normaalrežiimis käivitamine | + | // Starting |
| timer2_init_normal(prescaler); | timer2_init_normal(prescaler); | ||
| - | // Timer 2 katkestuste lubamine | + | // Allow Timer 2 interrupt |
| timer2_overflow_interrupt_enable(true); | timer2_overflow_interrupt_enable(true); | ||
| - | // Globaalsete katkestuste lubamine | + | // Enable global interrupts |
| sei(); | sei(); | ||
| } | } | ||
| Line 120: | Line 123: | ||
| </ | </ | ||
| - | With the array // | + | The controlling pins of four motor-controllers are determined |
| - | The following is an example program which controls first and second DC motor so that they alter their revolving direction after every second. The speed could be controlled | + | Function dcmotor_drive_pwm is for control motor speed. This function need three input values: motor number, direction (-1, 0, +1), where -1 is the rotation in one direction, +1 other direction and 0 for stop and thirdly, the speed range of 0-255. The speed value is not linked to a specific rotational speed, it is the relative value between minimal and maximal motor speed. Motor actual speed depends on the motor type, load and the supply voltage. Motor speed accuracy is 8-bits, which means that the minimum control accuracy is 1/255 of the maximum engine speed. |
| + | |||
| + | The following is an example program which controls first and second DC motor so that first motor rotates half of the speed and the second | ||
| <code c> | <code c> | ||