Hi , i hope someone here knows a bit or two about programming attiny13's.
because i hit a bump, i am not an expert and i am stuck now.
what i want to do is directly drive a micro stepper trough an attiny13.
i do not need to control the speed, only the motion , forward or backwards.
and the trigger from that comes from an RC signal on PB1.
but when putting power to this attiny13, the motor does not react in any way.
i tried with a similar code directly from the arduino and that did work.
but somehow in the translation to the attiny13 its goes wrong.
( yes i know best way is to use a driver, but i can't for this project)
#define INP PB1 // Define input pin as RCPIN
#define A PB0
#define A_bar PB2
#define B PB3
#define B_bar PB4
#define x 4000 // Smaller values result in more speed
#define stepsPerRevolution 24 // You can find the number of steps required to make a complete revolution in the data sheet of your motor
int main(void)
{
// Set motor pins as outputs
DDRB |= _BV(A) | _BV(A_bar) | _BV(B) | _BV(B_bar);
// Set input pullup for both INP and INP2
PORTB |= (1 << INP);
while (1)
{
// Stop timer
TCCR0B = 0;
// Clear timer
TCNT0 = 0;
// Wait for servo pulse to begin on INP2
loop_until_bit_is_set(PINB, INP);
// Start timer and divide timer clock by 64
TCCR0B |= (1 << CS01) | (1 << CS00);
// Wait for servo pulse to end on INP2
loop_until_bit_is_clear(PINB, INP);
// Read timer to obtain pulse width
byte pulse_width = TCNT0;
// If pulse width is greater than 120, rotate motor clockwise
if (pulse_width > 120) {
// Rotate motor for one quarter revolution
for (int i = 0; i < (stepsPerRevolution / 4); i++) {
digitalWrite(A, HIGH);
digitalWrite(A_bar, LOW);
digitalWrite(B, HIGH);
digitalWrite(B_bar, LOW);
_delay_ms(x);
digitalWrite(A, LOW);
digitalWrite(A_bar, HIGH);
digitalWrite(B, HIGH);
digitalWrite(B_bar, LOW);
_delay_ms(x);
digitalWrite(A, LOW);
digitalWrite(A_bar, HIGH);
digitalWrite(B, LOW);
digitalWrite(B_bar, HIGH);
_delay_ms(x);
digitalWrite(A, HIGH);
digitalWrite(A_bar, LOW);
digitalWrite(B, LOW);
digitalWrite(B_bar, HIGH);
_delay_ms(x);
}
}
// If pulse width is less than 70, rotate motor counterclockwise
else if (pulse_width < 70) {
// Rotate motor for one quarter revolution
for (int i = 0; i < (stepsPerRevolution / 4); i++) {
digitalWrite(A, HIGH);
digitalWrite(A_bar, LOW);
digitalWrite(B, LOW);
digitalWrite(B_bar, HIGH);
_delay_ms(x);
digitalWrite(A, LOW);
digitalWrite(A_bar, HIGH);
digitalWrite(B, LOW);
digitalWrite(B_bar, HIGH);
_delay_ms(x);
digitalWrite(A, LOW);
digitalWrite(A_bar, HIGH);
digitalWrite(B, HIGH);
digitalWrite(B_bar, LOW);
_delay_ms(x);
digitalWrite(A, HIGH);
digitalWrite(A_bar, LOW);
digitalWrite(B, HIGH);
digitalWrite(B_bar, LOW);
_delay_ms(x);
}
}
// If pulse width is between 70 and 120, stop the motor
else {
delay(1);
}
}
}
