Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. #include <DCMotor.h>
  2. #include "wiring_private.h"
  3. #include <avr/io.h>
  4. #include <avr/power.h>
  5.  
  6.  
  7. #if !defined(MCUCSR)
  8. #define MCUCSR MCUSR
  9. #endif
  10.  
  11. //watchdog registers to the same name (WDTCSR)
  12. #if !defined(WDTCSR)
  13. #define WDTCSR WDTCR
  14. #endif
  15.  
  16. //if enhanced watchdog, define irq values, create disable macro
  17. #if defined(WDIF)
  18. #define WD_IRQ 0xC0
  19. #define WD_RST_IRQ 0xC8
  20. #define WD_DISABLE() do{ \
  21. MCUCSR &= ~(1<<WDRF); \
  22. WD_SET(WD_OFF); \
  23. }while(0)
  24.  
  25. #endif
  26. #define WDTO_15MS 0
  27. #define WD_SET(val,...) \
  28. __asm__ __volatile__( \
  29. "in __tmp_reg__,__SREG__" "\n\t" \
  30. "cli" "\n\t" \
  31. "wdr" "\n\t" \
  32. "sts %[wdreg],%[wden]" "\n\t" \
  33. "sts %[wdreg],%[wdval]" "\n\t" \
  34. "out __SREG__,__tmp_reg__" "\n\t" \
  35. : \
  36. : [wdreg] "M" (&WDTCSR), \
  37. [wden] "r" ((uint8_t)(0x18)), \
  38. [wdval] "r" ((uint8_t)(val|(__VA_ARGS__+0))) \
  39. : "r0" \
  40. )
  41.  
  42.  
  43. /* Multiplo Pyfirmata UNLP vars */
  44. DCMotor motor0(M0_EN, M0_D0, M0_D1);
  45. DCMotor motor1(M1_EN, M1_D0, M1_D1);
  46.  
  47. void setup() {
  48. // put your setup code here, to run once:
  49. motor1.setClockwise(false);
  50. Serial1.begin(57600);
  51. pinMode(3, OUTPUT);
  52.  
  53. MCUSR &= ~(1 << WDRF);
  54. WD_SET(0);
  55.  
  56. /* Disable clock division */
  57. clock_prescale_set(clock_div_1);
  58.  
  59.  
  60.  
  61. sei();
  62.  
  63. // BECAREFULL
  64. // Timer/Counter0, 1, and 3 share the same prescaler module, but the Timer/Counters can have different prescaler settings.
  65.  
  66. //=====================================================================
  67. // Timer 0 Delay functions y OC0A como salida de PWM en pin 9
  68. //=====================================================================
  69.  
  70. // on the ATmega168, timer 0 is also used for fast hardware pwm
  71. // (using phase-correct PWM would mean that timer 0 overflowed half as often
  72. // resulting in different millis() behavior on the ATmega8 and ATmega168)
  73. sbi(TCCR0A, WGM01);
  74. sbi(TCCR0A, WGM00);
  75.  
  76. // set timer 0 prescale factor to 64
  77. cbi(TCCR0B, CS02);
  78. sbi(TCCR0B, CS01);
  79. sbi(TCCR0B, CS00);
  80.  
  81. // enable timer 0 overflow interrupt
  82. sbi(TIMSK0, TOIE0);
  83.  
  84. //----Configuracion para OC0A-----
  85. // El WGM0X ya esta definido por que lo usa arduino para el timer
  86. //sbi(TCCR0A,COM0A1);
  87. cbi(TCCR0A, COM0A0);
  88. cbi(TCCR0B, WGM02);
  89.  
  90.  
  91. //=====================================================================
  92. //Timer 3 PIN 6 only OC3A
  93. //=====================================================================
  94.  
  95. // se libera el timer3 para compatibilizar el core con la RGLib
  96. // (se pierde la funcion PWM del PIN6)
  97.  
  98. //OC4D pin connected
  99. //nOC4D pin Disconnected
  100. //sbi(TCCR3A,COM3A1);
  101. //cbi(TCCR3A,COM3A0);
  102.  
  103. //cbi(TCCR3B, WGM33);
  104. //cbi(TCCR3B, WGM32);
  105. //cbi(TCCR3A, WGM31);
  106. //sbi(TCCR3A, WGM30);
  107.  
  108. //En ppo esta apagado el pwm
  109. //cbi(TCCR3B,CS43);
  110. //cbi(TCCR3B,CS42);
  111. //cbi(TCCR3B,CS41);
  112. //sbi(TCCR3B,CS40);
  113.  
  114. //=====================================================================
  115. //Timer 4 Motors PWM (A & B) y GP TIMER D
  116. //=====================================================================
  117.  
  118. //OC4A pin connected
  119. //nOC4A pin Disconnected
  120. //sbi(TCCR4A,COM4A1);
  121. cbi(TCCR4A, COM4A0);
  122.  
  123. //OC4B pin connected
  124. //nOC4B pin Disconnected
  125. //sbi(TCCR4A,COM4B1);
  126. cbi(TCCR4A, COM4B0);
  127.  
  128. //Enable PWM en A y B
  129. //Phase and Frequency Correct PWM
  130. sbi(TCCR4A, PWM4A);
  131. sbi(TCCR4A, PWM4B);
  132.  
  133. //Prescaler --> Freq sea 31.35Khz.
  134.  
  135. //En ppo esta apagado el pwm
  136. cbi(TCCR4B, CS43);
  137. cbi(TCCR4B, CS42);
  138. cbi(TCCR4B, CS41);
  139. sbi(TCCR4B, CS40);
  140.  
  141. //--------------------------------------------------------------------------------------
  142. // Timer 4D para el pin 5
  143.  
  144. //OC4D pin connected
  145. //nOC4D pin Disconnected
  146. //sbi(TCCR4D,COM4D1);
  147. cbi(TCCR4D, COM4D0);
  148.  
  149. sbi(TCCR4C, PWM4D);
  150.  
  151. //Phase and Frequency Correct PWM
  152. cbi(TCCR4D, WGM41);
  153. sbi(TCCR4D, WGM40);
  154.  
  155. //=====================================================================
  156. // A/D
  157. //=====================================================================
  158.  
  159. // set a2d prescale factor to 128
  160. // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
  161. // XXX: this will not work properly for other clock speeds, and
  162. // this code should use F_CPU to determine the prescale factor.
  163. sbi(ADCSRA, ADPS2);
  164. sbi(ADCSRA, ADPS1);
  165. sbi(ADCSRA, ADPS0);
  166.  
  167. // enable a2d conversions
  168. sbi(ADCSRA, ADEN);
  169.  
  170.  
  171. //=====================================================================
  172. // USART
  173. //=====================================================================
  174. // We dont need to do anything
  175.  
  176.  
  177. //=====================================================================
  178. // Watchdog de CDC
  179. //=====================================================================
  180.  
  181. // The watchdog timer is configured for interrupting every 15ms for the cdc and usb tasks.
  182. WD_SET(WD_IRQ, WDTO_15MS);
  183.  
  184.  
  185.  
  186. }
  187.  
  188. int a4, a5;
  189.  
  190. void loop() {
  191. // put your main code here, to run repeatedly:
  192. a4 = analogRead(4);
  193. a5 = analogRead(5);
  194. Serial1.print("A4 = ");
  195. Serial1.println(a4);
  196. Serial1.print("A5 = ");
  197. Serial1.println(a5);
  198. /*
  199. if (a4 < a5){
  200. motor0.setSpeed(50);
  201. delay(1000);
  202. motor0.setSpeed(0);
  203. }
  204. else {
  205. motor1.setSpeed(50);
  206. delay(1000);
  207. motor1.setSpeed(0);
  208. }
  209. delay(1000);
  210. */
  211. digitalWrite(M1_D0, HIGH);
  212. digitalWrite(M1_D1, LOW);
  213. analogWrite(3, 200);
  214. //digitalWrite(M1_EN, HIGH);
  215. //sbi(TCCR4A, COM4B1);
  216. //TCCR4A = COM4B1 | PWM4B;
  217. //OCR4B = 127; // set pwm duty
  218.  
  219.  
  220. delay(500);
  221. digitalWrite(M1_EN, LOW);
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement