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:47] – 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 48: | Line 51: | ||
| ===== Practice ===== | ===== Practice ===== | ||
| - | The HomLab uses a combined ships to drive DC motors, which includes 2 integrated H-bridges and circuit breaking diodes. The motor is controlled with three digital signals,one of them is operation enabling signal //enable// and the other two are determining the state of the transistors in the H-bridge. Never can occur that two vertical transistors are opened, because this would short-circuit the power source. This means that the driver is designed as foolproof and only option that can be chosen is which transistor (upper or bottom) of one side of the H-bridge (of “semi-bridge”) is opened. In other words the polarity is selected using two driving signals which is applied to the two ends of the coil of the motor. | + | The HomLab uses a combined ships to drive DC motors, which includes 2 integrated H-bridges and circuit breaking diodes. The motor is controlled with three digital signals, one of them is operation enabling signal //enable// and the other two are determining the state of the transistors in the H-bridge. Never can occur that two vertical transistors are opened, because this would short-circuit the power source. This means that the driver is designed as foolproof and only option that can be chosen is which transistor (upper or bottom) of one side of the H-bridge (of “semi-bridge”) is opened. In other words the polarity is selected using two driving signals which is applied to the two ends of the coil of the motor. |
| - | The board of the motors | + | The Combo Board of the HomeLab allows connecting up to four DC motors. Basically, for every motor there is a H-bridge which is controlled with two digital output pins of the microcontroller, |
| ^ Input A ^ Input B ^ Output A ^ Output B ^ Result | ^ Input A ^ Input B ^ Output A ^ Output B ^ Result | ||
| 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> | ||