EVAL-04-MOTOR Xmega Development BoardMotor Control HowTo![]() SummaryThe EVAL-04-MOTOR development board provides the necessary hardware to experiment with the Atmel ATXMega32a4 microcontroller in motor and pneumatic control applications. All the necessary power switching circuitry is included to drive high voltage DC motors, pneumatic valves, stepper motors and servo motors. This document explains the electrical design of the EVAL-04-MOTOR development board, and gives suggestions for how to develop software to control various motors utilizing the integrated event control system, waveform generation module and timer/counter capabilities of the Xmega family of microcontrollers. Requirements
Xmega BasicsThe Xmega family of microcontrollers contains a number of 16-bit timer counters which may be used to create PWM, Servo or Stepper motor control signals. The ATXMega32A4 contains five 16-bit timers which can be easilly configured for waveform generation to drive a variety of motors. Additionally, the eight channel event system allows for easy chaining of timers such that two timers can be configured to create the necessary signals to drive a bipolar stepper motor in the background while your application code performs other tasks. EVAL-04-MOTOR BasicsThe EVAL-04-MOTOR contains an ATXMega32A4 microcontroller which is limited to 3.3V I/O. In order to drive the high voltages and currents that are needed by most electric motors this development board includes a combination of H-Bridge circuits and High current MOSFETs which have low Vgs such that they can switch much higher voltages. This board also includes a 5V switching regulator in order to drive standard servo motors. ![]() Note in the figure above that there are two distinct power inputs to the board. Vmotor supplies the high voltage sections of the board including the two H-Bridges and the switches. Vin is the other power supply and provides power to the microcontroller, accelerometer and sensitive analog components. Vin also provides 5V power for servo motors, so care must be taken to make sure servo control noise does not impact performance of the microcontroller and accelerometer. DC Motor Hardware SetupA simple DC motor is the easiest first device to begin experimenting with. Before we get started you will need the following:
Connect your DC motor using the screw terminals corresponding to SW1. Markings on the board indicate which terminal is positive, and which negative. ![]() DC Motor ControlThe high voltage switches are controlled by GPIO lines from the Xmega microcontroller located on PORTE. ![]()
PWM ControlPORTE can be controlled by making use of the 16-bit timer/counter and enabling waveform generation mode. Each of the GPIOs on PORTE may be toggled at an independent frequency by setting the appropriate Compare/Capture values as indicated below. ![]() By enabling waveform generation, a relevant CCX value can be set on a timer to toggle the output. ![]() An inverted PWM signal can be generated by enabling the inversion bit. ![]() To create a 50% duty cycle signal which will switch Vmotor to SW1 at 1KHz we need to configure PORTE as output, enable waveform generation from PORTE Timer0, and set the CCA and PER values (Compare/Capture A and Period) as shown in the example code below. // setup PORTE GPIO output PORTE.DIR = 0x0F; // PORTE:0-3 // setup timer for PORTE PWM output to motor TCE0.CTRLA = TC_CLKSEL_DIV1024_gc; // 32M/1024=32K or .03ms/tick TCE0.CTRLB = TC0_CCAEN_bm | TC0_WGMODE1_bm | TC0_WGMODE0_bm; // enable waveform gen single slope TCE0.PER = 32; // 1ms = 1KHz waveform TCE0.CCA = 16; // 16/32 = 50% duty cycle Stepper Motor Hardware SetupBefore we get started you will the EVAL-04-MOTOR board and stepper motors you will need the following:
Consult the documentation for your stepper motor, which will indicate the first and second lead for the first coil and first and second lead of the second coil. You can connect these wires to the screw terminals labeled 1,2,3,4 in sequence along the top edge of the board. Becuase you are connecting to an H-Bridge you can't get into much trouble here. The worst that happens is the motor doesn't turn. ![]() Next locate an appropriate power supply for your motor (9-24V) and be careful to connect the positive rail to the Vmotor "+" labeled screw terminal. Likewise connect the "-" terminal to your power supply. If you have a bench power supply with current readout, check that when your motor and power supply are connected (and the microcontroller is off) that no power is being consumed by your motor. Alternatively, check for overheating components on the board. If you notice anything out of the ordinary when you supply power to Vmotor disconnect the power imediately and get a DMM and figure out what's wrong!! Stepper Motor ControlThe EVAL-04-MOTOR includes 2 full H-Bridge drivers based on the ST L298 H-Bridge IC which can drive 2 bipolar(4-wire) stepper motors. Screw terminals are provided to connect stepper motors to the board. If you are interested in using 5, 6 or 8 wire unipolar stepper motors, it is possible to convert them to 4-wire configuration. See http://www.cs.uiowa.edu/~jones/step/types.html for more information. Before connecting and attempting to drive a stepper motor, check how much current your motor requires. Due to the compact size of the EVAL-04-MOTOR the limits of the board are not necessarilly the same as the L298 driver IC. In general the current version of the EVAL-04-MOTOR should not be used to continuously drive loads greater then 3A. This limit is for the combined output of both H-Bridge circuits. In other words, if you are driving 2 stepper motors continuously using both H-Bridge circuits, the power to each motor should be limited to 1.5A. Peak power delivery is limited by the L298 IC and may be greater then 3A. By reducing the duty cycle of the H-Bridge circuits below 100%, higher current is possible. Additionally, the trace width of the Vmotor input may limit current delivery. In this case redundant wires may be added to increase current handling capacity. Stepper Motor BasicsA bipolar stepper motor typically has two coils which may be energized in either a forward or reverse direction. By sequentially energizing the coils in the correct sequence and direction a single step can be driven. ![]() The ST L298 H-Bridge driver circuit is controlled by two lines which allow the enable/disable and direction of current flow through a pair of terminals which are connected to a single coil in the stepper motor as indicated in the figure below. ![]() Below are the details of how the enable and direction lines control the various outputs from the H-Bridge circuit. ![]() The EVAL-04-MOTOR utilizes the GPIOs on PORTC to drive the first H-Bridge (stepper outputs 1-4). PORTD is utilized to drive the second H-Bridge (stepper outputs 5-8). As with regular DC motor driving discussed previously, PORTC and PORTD each have 2 independent 16-bit timer/counters (TIMER0,1) capable of waveform generation. This greatly simplifies generation of the appropriate sequence of current through the stepper coils. As shown in the table below, each output to the H-Bridge circuit has its own compare value which can be set to generate a unique digital waveform. ![]() To implement a full step sequence in the forward direction with a bipolar stepper motor, we can configure TIMER0 to drive the ENX lines to activate the first, then second coil. TIMER1 can be chained off TIMER0, such that after EN1 and EN2 have been activated in sequence we then switch the direction of the current and then activate EN1 and EN2 again completing a single step. Chaining timers is accomplished by setting the clock source of TIMER1 to be an overflow event from TIMER0. ![]() Here is the code to configure TIMER0/1 to generate a full step on a bipolar stepper motor:
// Setup group A motor enable lines for 4-wire bipolar stepper motor
TCC0.CTRLA = TC_CLKSEL_DIV1024_gc; // 32M/1024=32K or .03ms/tick
TCC0.CTRLB |= TC0_CCAEN_bm | TC0_CCBEN_bm
| TC0_WGMODE1_bm | TC0_WGMODE0_bm; // enable waveform gen single slope
TCC0.PER = 15625 / speed; // @32MHz, 31250 ticks/s, TCC0 overflow 2x/step
PORTC.DIRSET = (1<<5) | (1<<4) | (1<<1) | (1<<0); // set PORTC:0,1,4,5 output
TCC0.CCA = TCC0.PER/2; // half period / 50% duty
TCC0.CCB = TCC0.PER/2; // half period / 50% duty
// Setup group A motor direction lines for 4-wire bipolar stepper motor
TCC1.CTRLB |= TC1_CCAEN_bm | TC1_CCBEN_bm | TC1_WGMODE1_bm | TC1_WGMODE0_bm; // enable waveform gen single slope
// chain timer1 after timer0
TCC1.PER = 1; // two overflows of timer0 is one cycle of timer1
// NOTE: this looks funny since we want a period of 2, but when top of counter
// is reached it waits for one more clock (overflow) before resetting to 0
// so period should be 2-1
TCC1.CTRLA = TC_CLKSEL_EVCH1_gc;
TCC1.CCA = 1;
TCC1.CCB = 1;
// setup event1 to be timer0 overflow
EVSYS.CH1MUX = 0xC0; // CH0 = TCC0 overflow
By itself the above code may seem complete, but there is an additional step. In order to get the correct waveform generation we need to invert some of the ports. By default CCA, CCB, etc sets each pin initially hi(on) and then when a compare/capture occurs (count=CCX) the pin is set lo(off). The above code would enable both coils (EN1 and EN2) initially which would cause the motor to brake. What we need is EN1 on initially until count=CCA, and EN2 off initially until count=CCB. To do this we simply invert PORTC:1 as follows. // Forward turn PORTC.PIN0CTRL &= ~PORT_INVEN_bm; // CCA is normal PORTC.PIN1CTRL |= PORT_INVEN_bm; // CCB is inverted PORTC.PIN4CTRL &= ~PORT_INVEN_bm; // CC1A is normal PORTC.PIN5CTRL &= ~PORT_INVEN_bm; // CC1B is normal In order to do a reverse step we need to configure PORTC as follows. // Reverse turn PORTC.PIN0CTRL |= PORT_INVEN_bm; // CCA is inverted PORTC.PIN1CTRL &= ~PORT_INVEN_bm; // CCB is normal PORTC.PIN4CTRL |= PORT_INVEN_bm; // CC1A is inverted PORTC.PIN5CTRL |= PORT_INVEN_bm; // CC1B is inverted Servo Motor Hardware SetupBefore we get started you will the EVAL-04-MOTOR board and servo motors you will need the following:
![]() Servo Motor ControlServo Motors (hobby type) are controlled by providing a 5V power supply and a PWM control signal where pulses are sent every 20ms and may be either 1.0, 1.5, or 2.0ms in width. These signals correspond to 0, 45(neutral), 90deg angles. In some motors this alternatively is 0,90,180 or forward, hold, reverse. ![]() The servo control lines are connected to PORTC pins 2,3 and PORTD pins 2,3. ![]() Below is example code to configure SRV1 for 0,45,90 degree position.
PORTC.DIRSET = (1<<2) | (1<<3); // config PORTC:2,3 as output to SRV1,2
TCC0.CTRLA = TC_CLKSEL_DIV1024_gc; // 32M/1024=32K or .03ms/tick
TCC0.CTRLB |= TC0_CCCEN_bm | TC0_CCDEN_bm | TC0_WGMODE1_bm | TC0_WGMODE0_bm; // enable compare capture for SRV1,2
TCC0.PER = 32*20; // approx 20ms
if(pos == POS_0)
TCC0.CCC = 32;
else if(pos == POS_90)
TCC0.CCC = 64;
else
TCC0.CCC = 48;
3-Axis AcclerometerThe EVAL-04-MOTOR includes a Freescale MMA7361 3-axis analog accelerometer. Output from the accelerometer can be read on PORTX and is referenced to Vref the precision 2.5V voltage reference.
LEDs and GPIOsThere are two user controlled LEDs connected to PORTD pins 7,8. They can be enabled by setting either of these outputs hi. ![]() There are 8 GPIOs which may be configured as analog inputs, digital inputs, or digital outputs. Note, if the MMA7361 3-axis acclerometer is installed on your board you can not use PORTA:4 for other purposes without potentially damaging the accelerometer IC.
Power Supply Noise ReductionOne of the main sources of noise on the board is due to high-frequency switching noise and 60Hz line noise originating in the power supplies that feed into the EVAL-04-MOTOR. Because switching power supplies are more cost effective and often more efficient, it is no surprise that common power supplies are noisy. To reduce noise from power supplies, it may be necessary to add additional decoupling capacitors or other forms of power supply line filtering. Alternatively, battery supplies and linear power supplies will significantly reduce noise on the board. Another consideration is the switching noise from the on-board 5V regulator. This part operates at 150KHz and its contribution to the overall noise on the board is inversely proportional to the load. In our testing the best noise levels on the ground plane near the microcontroller occur when Vin is between 5V and 6V. This causes the regulator to be "on" most of the time and reduces the noise levels on the board. As Vin is increased to 12V noise on the ground plane is doubled. |
Last Updated 01/28/2011
All content copyright 2011 by BostonAndroid