I have been trying to interface a bipolar stepper motor with an ATmega32 controller using AVR assembly, but it just keeps clicking without movement or just move back and forth in the same step. Here is the code:
.include "m32def.inc"
; define motor pins
.equ A1 = PB0
.equ A2 = PB1
.equ B1 = PB2
.equ B2 = PB3
.cseg
.org 0x00
ser r16
out DDRB, r16 ;do not need to save r16
start:
; Step 1
sbi PORTB, A1
cbi PORTB, A2
cbi PORTB, B1
cbi PORTB, B2
ldi delayMultiplier, 50
rcall delay
; Step 2
cbi PORTB, A1
cbi PORTB, A2
sbi PORTB, B1
cbi PORTB, B2
ldi delayMultiplier, 50
rcall delay
; Step 3
cbi PORTB, A1
sbi PORTB, A2
cbi PORTB, B1
cbi PORTB, B2
ldi delayMultiplier, 50
rcall delay
; Step 4
cbi PORTB, A1
cbi PORTB, A2
cbi PORTB, B1
sbi PORTB, B2
ldi delayMultiplier, 50
rcall delay
rjmp start
.def delayMultiplier = r25
.equ l1 = 20000
delay:
push r27
push r26
push r25
loop2:
ldi r26, LOW(l1)
ldi r27, HIGH(l1)
loop1:
sbiw r26, 1
brne loop1
dec delayMultiplier
brne loop2
pop r25
pop r26
pop r27
ret
I tried some C code that does the same function and it kind of worked, but was still glitchy and behaved weirdly. It starts rotating then begin clicking a bit and eventually totally stopping.
#include <avr/io.h>
#define F_CPU 8000000L
#include <util/delay.h>
int main(void)
{
DDRB = 0xff;
while (1)
{
PORTB = 0x01; //A1 high
_delay_ms(500);
PORTB = 0x02; //B1 high
_delay_ms(500);
PORTB = 0x04; //A2 high
_delay_ms(500);
PORTB = 0x08; //B2 high
_delay_ms(500);
}
}
lastly I tried it using an Arduino UNO, and for some reason is worked flawlessly
void setup() {
// put your setup code here, to run once:
DDRB = 0xff;
}
void loop() {
// put your main code here, to run repeatedly:
PORTB = 0x01; //A1 high
delay(500);
PORTB = 0x02; //B1 high
delay(500);
PORTB = 0x04; //A2 high
delay(500);
PORTB = 0x08; //B2 high
delay(500);
}
it also worked with this code
int a1 = 7, a2 = 6, b1 = 5, b2 = 4;
void setup() {
// put your setup code here, to run once:
pinMode(a1,OUTPUT);
pinMode(a2,OUTPUT);
pinMode(b1,OUTPUT);
pinMode(b2,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(a1,HIGH);
digitalWrite(a2,LOW);
digitalWrite(b1,LOW);
digitalWrite(b2,LOW);
delay(100);
digitalWrite(a1,LOW);
digitalWrite(a2,LOW);
digitalWrite(b1,HIGH);
digitalWrite(b2,LOW);
delay(100);
digitalWrite(a1,LOW);
digitalWrite(a2,HIGH);
digitalWrite(b1,LOW);
digitalWrite(b2,LOW);
delay(100);
digitalWrite(a1,LOW);
digitalWrite(a2,LOW);
digitalWrite(b1,LOW);
digitalWrite(b2,HIGH);
delay(100);
}
Obviously I have no Idea what is going on so please advice.
Note that I am doing this for a university project so I have to do it using assembly.
Circuit Schematic: