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 12:27] – 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 32: | Line 35: | ||
| Compared to the analog control a digital control has a number of advantages. The main advantage of microcontroller-controlled systems is that it requires only a single digital output and there is no need for complicated digital-to-analog converter. The digital controlling is also more efficient because less energy is converted into heat. | Compared to the analog control a digital control has a number of advantages. The main advantage of microcontroller-controlled systems is that it requires only a single digital output and there is no need for complicated digital-to-analog converter. The digital controlling is also more efficient because less energy is converted into heat. | ||
| - | The driver of DC motor in the HomeLab L293D includes 2 integrated H-bridges and circuit breaking diodes. The motor is controlled with three digital signals,one of them is operation enabling signal // | + | A simplified control scheme is shown in the next drawing. The control voltage Vc is coming to the microcontroller output pin and switch |
| + | The total power which is passing the transistor can be calculated by the formula: | ||
| + | |||
| + | P = I * V | ||
| + | |||
| + | P = I * Vq, and when Vq ~ 0, then P ~ 0 W | ||
| + | |||
| + | This means that the transistor spend almost no energy in the open state. Similar situation is also the case when the transistor is in the closed state. In this case, there is no current flow through the transistor or the motor. Now the power which is going through the transistor, is calculated as follows: | ||
| + | |||
| + | P = I * Vq, and when I = 0, then P = 0 W | ||
| + | |||
| + | In conclusion, we can say that if the transistor is a switch element on the scheme, then the system efficiency is high and the power used by transistors is low. Compared with a linear (analog) system, where the transistor consumes of the half-open state the same amount of power than the motor, it is a very big energy savings. In practice, there is no lossless system and in fact, the losses occur when the transistor switch one state to other. Therefore, higher losses are occurring when the transistors are switched at higher frequencies. | ||
| ===== Practice ===== | ===== Practice ===== | ||
| - | The board of the motors | + | 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 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 45: | 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 56: | 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> | ||