Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. /*
  2. * EasyStepper_v1.cpp
  3. *
  4. * Created: 09-09-2014 15:47:49
  5. * Author: Martin Axelsen @ RepRap.me
  6. * Environment: Atmel Studio 6.2
  7. * License: CC-BY-NC-SA
  8. */
  9.  
  10.  
  11. #define F_CPU 1000000
  12. #include <avr/io.h>
  13.  
  14. int main(void)
  15. {
  16. PORTCR = 0x00; // Break-Before-Make disable
  17. PUEB = 0x00; // Pull-up disable
  18. DDRB = 0x03; // Port B0+B1+B2 as output
  19. PORTB = 0x00; // Port B0+B1+B2 = low
  20.  
  21. ADMUX = 0x02; // Port B2 MUX ADC2
  22. ADCSRA = 0xe7; // Enable ADC, auto trigger, pre-scale timing 128
  23. ADCSRB = 0x00; // ADC free running mode
  24.  
  25. int dir = 0x0; // Direction flag
  26. int oldir = dir; // Direction flag buffer
  27. int step; // Step pin
  28. long delay0; // Delay set point
  29. long delay1 = 0; // Delay smoothened
  30. long delay2; // First counter
  31. int adcget; // Analog buffer
  32.  
  33. while(1)
  34. {
  35. adcget = ADCL;
  36.  
  37. if (adcget >= 124) { // Catch potmeter position
  38. dir = 0x00;
  39. delay0 = 244 - adcget;
  40. } else {
  41. dir = 0x01;
  42. delay0 = adcget - 7;
  43. }
  44.  
  45. if (adcget >= 244) {delay0 = 0;} // Catch >>> button
  46. if (adcget <= 4) {delay0 = 0;} // Catch <<< button
  47.  
  48. delay0 = delay0 + 1; // Add minimum delay to set max speed and avoid motor stalling1
  49.  
  50. if (delay1 > delay0) {delay1--;} // Acceleration
  51. if (delay1 < delay0) {delay1 = delay0;} // Instant deccelleration
  52.  
  53. if (oldir != dir) { // Reset speed on direction change
  54. delay1 = 100;
  55. oldir = dir;
  56. }
  57.  
  58. if ((adcget < 113) || (adcget > 135)) { // Only run if potmeter is not at center position
  59.  
  60. delay2 = delay1; // Copy to buffer
  61.  
  62. step = 0x00;
  63. PORTB = dir + step; // Set pins
  64. while (delay2-- >0) {} // Count down
  65.  
  66. delay2 = delay1; // Copy to buffer
  67.  
  68. step = 0x02;
  69. PORTB = dir + step; // Set pins
  70. while (delay2-- >0) {} // Count down
  71. } else {
  72. delay1 = 100;
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement