Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * EasyStepper_v1.cpp
- *
- * Created: 09-09-2014 15:47:49
- * Author: Martin Axelsen @ RepRap.me
- * Environment: Atmel Studio 6.2
- * License: CC-BY-NC-SA
- */
- #define F_CPU 1000000
- #include <avr/io.h>
- int main(void)
- {
- PORTCR = 0x00; // Break-Before-Make disable
- PUEB = 0x00; // Pull-up disable
- DDRB = 0x03; // Port B0+B1+B2 as output
- PORTB = 0x00; // Port B0+B1+B2 = low
- ADMUX = 0x02; // Port B2 MUX ADC2
- ADCSRA = 0xe7; // Enable ADC, auto trigger, pre-scale timing 128
- ADCSRB = 0x00; // ADC free running mode
- int dir = 0x0; // Direction flag
- int oldir = dir; // Direction flag buffer
- int step; // Step pin
- long delay0; // Delay set point
- long delay1 = 0; // Delay smoothened
- long delay2; // First counter
- int adcget; // Analog buffer
- while(1)
- {
- adcget = ADCL;
- if (adcget >= 124) { // Catch potmeter position
- dir = 0x00;
- delay0 = 244 - adcget;
- } else {
- dir = 0x01;
- delay0 = adcget - 7;
- }
- if (adcget >= 244) {delay0 = 0;} // Catch >>> button
- if (adcget <= 4) {delay0 = 0;} // Catch <<< button
- delay0 = delay0 + 1; // Add minimum delay to set max speed and avoid motor stalling1
- if (delay1 > delay0) {delay1--;} // Acceleration
- if (delay1 < delay0) {delay1 = delay0;} // Instant deccelleration
- if (oldir != dir) { // Reset speed on direction change
- delay1 = 100;
- oldir = dir;
- }
- if ((adcget < 113) || (adcget > 135)) { // Only run if potmeter is not at center position
- delay2 = delay1; // Copy to buffer
- step = 0x00;
- PORTB = dir + step; // Set pins
- while (delay2-- >0) {} // Count down
- delay2 = delay1; // Copy to buffer
- step = 0x02;
- PORTB = dir + step; // Set pins
- while (delay2-- >0) {} // Count down
- } else {
- delay1 = 100;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement