Path: Home => AVR overview => Applications => Steppermotor controller    (Diese Seite in Deutsch: Flag DE) Logo
Steppermotor controller small

Steppermotor controller with ATtiny13

This application of an AVR demonstrates controlling a steppermotor with an ATMEL ATtiny13, with the following properties:

0. Content

1. Hardware

Controller/driver The hardware consists of the AVR processor ATtiny13, a six pin standard programming connection for In-System-Programming (ISP), the 7-bit driver ULN2003, the supply for the processor and the filtering of the analogue input signal. The schematic diagram (click on it for a PDF page with higher resolution):


1.1 Microcontroller

The processor ATtiny13 provides the following functions.
The operating voltage of 5 V is supplied to the pins 8 (+5 V) and 4 (0 V) and blocked with a ceramic capacitor of 100 nF.
Pin 1 (= RESET input) is tied with a resistor of 10 kOhm to the operating voltage.
Input PB4 (Pin 3) measures the analogue voltage, using the internal AD converter, by comparision with the operating voltage. From its conversion results, software calculates the target value of the steppermotor's steps.
The output port pins PB0 to PB3 (pins 5, 6, 7 and 2) control the driver for the coils of the steppermotor.

1.2 ISP-Interface

The ISP interface serves as programming interface to program the AVR in the system. The pinout is ATMEL standard.
The ISP interface uses the port bits PB2 (SCK, Pin 7), PB1 (MISO, Pin 6), PB0 (MOSI, Pin 5) and the RESET at Pin 1. The operating voltage at VTG, if connected, provides supply current to the programmer. Leave VTG open if the programmer provides its own supply. GND is reference for the negative operating voltage.

1.3 Coil current driver ULN2003A

The drive current for the coils of the steppermotor is controlled by the driver IC ULN2003A. The outputs with open-collector-driver transistors allow for voltages of up to 50 V and currents of up to 500 mA. They switch the different coils of the motor on aned off.
Induced inductive backdropping voltages, when coils are switched off, are shortcircuited by diodes, that are connected internally from each collector to the pin CD. The motor used here is operated with a 12 V supply voltage and requires a current of approx. 150 mA per active coil (because there are alway two coils active at a time, together 300 mA).
The input pins I7..I4 of the driver are controlled by the processor (active high, logic 1 switches coil on).

1.4 Supply

The supply voltages are filtered to avoid glitches by the switching coils. Supply of the coils is connected over a diode 1N4007 and smoothed by a capacitor of 100 µF.
The processor is supplied by a voltage stabilizer 78L05. Its input is connected to the 12 V supply over a diode 1N4007 and and capacitor of 100 µF. The stabilizer is blocked with tantalum capacitors of 1 µF resp. 2,2 µF to avoid oscillations.
The supply and regulation of the controller/driver comes over a four-wire connection cable from a 12 V power supply (click on the picture to download a PDF doc with higher resolution).

Schematic supply

The 12 V supply is mounted on a small board.

Supply board

2. Software

The software for the ATtiny13 is written in assembler, the source code is here for download.

2.1 Functioning

The software consists of the following basic elements:

Reset- and interrupt vector table

The vector table redirects program execution to the main program, if a reset starts the processor. In case of an interrupt, the service routines for the timer/counter and the AD converter are re-directing. Available vectors that are not used here are represented by RETI instructions.

Init of start values

Initiation of start values is coded starting with the label "Main:". Here

Init of the hardware

Initing the hardware consists of:

AD converter measurements of the input voltage

The AD converter converts the input voltage on pin 3 (PB4, ADC2) to a value between 0..1023 and triggers a Conversion Complete Interrupt. The Interrupt Service Routine, starting at the label "AdcInt:" reads the result from the ports ADCL und ADCH and sums it to the register pair rAdcH:rAdcL. The counter rAdc is decreased by one. If rAdc reaches zero, the sum value is copied to the register pair rAdcRH:rAdcRL, the sum is cleareed, the counter is again set its initial value 64 and the flag bAdc in the flag register is set to one. Finally, the next conversion is started.
Summing up 64 conversion results results in an averaging over these values, removing random results and fluctuations in the input signal, and slows down the measuring process to a convenient cycle duration. The resulting sum value is between zero and 65,535 (0x0000..0xFFFF), an excellent basis for the following calculation of the target value.

Conversion of the measuring result to the target value

If after waking up the processor and exection of the interrupt service routine the flag rAdc in the flag register is set, the conversion routine starting with the label "AdcRdy:" is called. This routine

Step control and output to the motor

The step control and output to the steppermotor is done in the interrupt service routine of the counter, starting with the label "Tc0IntCA:".
First, the actual and the target value registers are compared. If they are equal, a jump to the label "Tc0IntCA0:" is executed. There, the delay counter in the register pair X is decreased by one. If the delay counter reaches zero, the coils are de-energized by writing zeroes to the driving port bits, the delay counter is restarted and the service routine is left.
If the actual and the target value are not equal, the actual value is incresed or decreased. This new value is translated like follows to a new value for the stepper driver: The table "SmTab:" with the two words 0x0605 and 0x090A determines the step sequence of the steppermotor as follows: Note: If the coils Q1..Q4 of the stepper are connected in a different manner to the driver outputs, it is necessary and sufficient to change these two words (see below).
In the service routine finally the delay counter is restarted, to keep the coils activated for the appropriate preselected time, after changing the output pattern.

2.2 Adjustments prior to assembling

In the assembler source code, the following adjustments must/can be made prior to assembling the code: The order of the four coils on connector J2 might be different with different types of motors. If a different order is to be adapted, it is sufficient to change the table SmTab:.
The current table is made for a KP4M4-001 type and is generated as follows:

CoilcolorPortbitStep 1Step 2Step3Step 4

The resulting table code is as follows:



2.3 Commented source code

The source code is available in .asm text format here, in .html format here.

©2007 by