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 13:55] – 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 58: | Line 61: | ||
| | 0 | | 0 | ||
| - | DC motors can be controlled | + | For each motor that is connected to the H-bridge is operated |
| <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 69: | Line 72: | ||
| { PIN(D, 5), PIN(D, 4) } | { PIN(D, 5), PIN(D, 4) } | ||
| }; | }; | ||
| - | + | static int motorindex[4][2] = | |
| - | // Allowing the control of the chosen | + | { |
| - | void dcmotor_init(unsigned char index) | + | { 0, 1 }, |
| - | { | + | { 2, 3 }, |
| + | { 4, 5 }, | ||
| + | { 6, 7 } | ||
| + | }; | ||
| + | // Initializing a PWM to chosen motor | ||
| + | void dcmotor_drive_pwm_init(unsigned char index, timer2_prescale prescaler) | ||
| + | { | ||
| + | unsigned char i, pwm; | ||
| + | |||
| pin_setup_output(dcmotor_pins[index][0]); | pin_setup_output(dcmotor_pins[index][0]); | ||
| pin_setup_output(dcmotor_pins[index][1]); | pin_setup_output(dcmotor_pins[index][1]); | ||
| - | } | ||
| - | // Determining the operation and the direction of the chosen | + | motor[index] = 1; |
| - | void dcmotor_drive(unsigned char index, signed char direction) | + | pwm = PWMDEFAULT; |
| - | { | + | |
| - | pin_set_to(dcmotor_pins[index][0], direction < 0); | + | // Starting all channels |
| - | pin_set_to(dcmotor_pins[index][1], direction | + | for(i=0 ; i<CHMAX ; i++) |
| + | { | ||
| + | // PWM state variable initialization | ||
| + | compare[i] = pwm; | ||
| + | compbuff[i] = pwm; | ||
| + | } | ||
| + | |||
| + | // Starting Timer 2 to normal mode | ||
| + | timer2_init_normal(prescaler); | ||
| + | // Allow Timer 2 interrupt | ||
| + | timer2_overflow_interrupt_enable(true); | ||
| + | |||
| + | // Enable global interrupts | ||
| + | sei(); | ||
| + | } | ||
| + | // Generating a PWM for chosen motor | ||
| + | void dcmotor_drive_pwm(unsigned char index, signed char direction, | ||
| + | unsigned char speed) | ||
| + | { | ||
| + | if(direction == -1) | ||
| + | { | ||
| + | compbuff[motorindex[index][0]] = 0x00; | ||
| + | compbuff[motorindex[index][1]] = speed; | ||
| + | } | ||
| + | if(direction | ||
| + | { | ||
| + | compbuff[motorindex[index][0]] = speed; | ||
| + | compbuff[motorindex[index][1]] = 0x00; | ||
| + | } | ||
| } | } | ||
| </ | </ | ||
| - | With the array // | + | The controlling pins of four motor-controllers are determined |
| + | |||
| + | Function dcmotor_drive_pwm is for control motor speed. This function need three input values: motor number, | ||
| - | 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 | + | 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> | ||