Skip to content

Instantly share code, notes, and snippets.

@Oppodelldog
Created April 16, 2020 20:51
Show Gist options
  • Save Oppodelldog/584f6bfdd91931539ae40386a83e51d0 to your computer and use it in GitHub Desktop.
Save Oppodelldog/584f6bfdd91931539ae40386a83e51d0 to your computer and use it in GitHub Desktop.
Control of Stepper Motor (28BYJ-48) via its driver - used on ATMEGA-88A
/*
* main.c - Control of Stepper Motor (28BYJ-48) via its driver
* written for ATMEGA-88A
*
* Created: 16.04.2020 19:00:00
* Author : nils
*/
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
int stepConfig[4][4] = {[0]={1,0,0,0},[1]={0,1,0,0},[2]={0,0,1,0},[3]={0,0,0,1}};
void waitStep(void){
_delay_us(3000); // docs say min is 2ms, my experience was 2700us
}
void low(int pin){
PORTB &= ~(1 << pin);
}
void high(int pin){
PORTB |= (1 << pin);
}
void setStepPins(int step[]){
step[0]==1?low(PB0):high(PB0);
step[1]==1?low(PB1):high(PB1);
step[2]==1?low(PB2):high(PB2);
step[3]==1?low(PB3):high(PB3);
}
void step(){
setStepPins(stepConfig[0]);
waitStep();
setStepPins(stepConfig[1]);
waitStep();
setStepPins(stepConfig[2]);
waitStep();
setStepPins(stepConfig[3]);
waitStep();
}
int main (void) {
DDRB |= (1 << PB0);
DDRB |= (1 << PB1);
DDRB |= (1 << PB2);
DDRB |= (1 << PB3);
while(1) {
step();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment