Advertisement
abdullahkahraman

Untitled

May 31st, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. /*******************************************************
  2. * APP6.23 - Torque Control Software
  3. * Dual DC motor speed controller
  4. * Processor: 328P
  5. * Hardware Version: 1.0
  6. * Version Date: 27.05.2016
  7. ********************************************************/
  8.  
  9. //#define PWM_FREQ_1KHZ 0
  10. //#define PWM_FREQ_5KHZ 0
  11. #define PWM_FREQ_10KHZ 0
  12.  
  13. #define clear_M1_D2() PORTD &= ~(1 << 4)
  14. #define set_M1_D2() PORTD |= (1 << 4)
  15.  
  16. #define clear_M2_D1() PORTD &= ~(1 << 7)
  17. #define set_M2_D1() PORTD |= (1 << 7)
  18.  
  19. #define clear_M2_D2() PORTB &= ~(1 << 0)
  20. #define set_M2_D2() PORTB |= (1 << 0)
  21.  
  22. unsigned int M1_direction; // FORWARD or REVERSE for the motor 1
  23. unsigned int M1_start_stop; // START or STOP for the motor 1
  24. unsigned int M1_fault_in; // Fault input from the motor 1
  25. unsigned int M1_torque_set_pnt; // Torque setpoint for the motor 1
  26.  
  27. unsigned int M2_direction; // FORWARD or REVERSE for the motor 2
  28. unsigned int M2_start_stop; // START or STOP for the motor 2
  29. unsigned int M2_fault_in; // Fault input from the motor 2
  30. unsigned int M2_torque_set_pnt; // Torque setpoint for the motor 2
  31.  
  32. void setup()
  33. {
  34. Serial.begin(9600);
  35. /*
  36. * Motor 1 Controls:
  37. * -----------------
  38. * D1: Tied externally
  39. * D2: PORTD.4
  40. * IN1: PORTD.5
  41. * IN2: PORTD.6
  42. * Start-Stop: PORTC.2
  43. * Direction: PORTC.3
  44. * Fault: PORTD.2
  45. *
  46. */
  47.  
  48. // D4, D5, D6 are outputs
  49. DDRD |= (1 << 4);
  50. DDRD |= (1 << 5);
  51. DDRD |= (1 << 6);
  52.  
  53. /*
  54. * Motor 2 Controls:
  55. * -----------------
  56. * D1: PORTD.7
  57. * D2: PORTB.0
  58. * IN1: PORTB.1
  59. * IN2: PORTB.2
  60. * Start-Stop: PORTC.4
  61. * Direction: PORTC.5
  62. * Fault: PORTD.3
  63. *
  64. */
  65.  
  66. // D7, B0, B1 and B2 are outputs
  67. DDRD |= (1 << 7);
  68. DDRB |= (1 << 0);
  69. DDRB |= (1 << 1);
  70. DDRB |= (1 << 2);
  71.  
  72.  
  73. // Set M2_D1 and clear M2_D2
  74. // This will disable M2
  75. set_M2_D1();
  76. clear_M2_D2();
  77.  
  78. // Clear M2_D2 so that it
  79. // will disable M1
  80. clear_M1_D2();
  81.  
  82. // PWM setup for Motor 1:
  83. TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM00);
  84. TCCR0B = _BV(CS01);
  85. OCR0A = 0;
  86. OCR0B = 0;
  87.  
  88. // PWM setup for Motor 2:
  89. TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11);
  90. #ifdef PWM_FREQ_1KHZ
  91. TCCR1B = _BV(CS11) | _BV(WGM13);
  92. OCR1A = 0;
  93. OCR1B = 0;
  94. ICR1 = 999;
  95. #endif
  96. #ifdef PWM_FREQ_5KHZ
  97. TCCR1B = _BV(CS10) | _BV(WGM13);
  98. OCR1A = 0;
  99. OCR1B = 0;
  100. ICR1 = 1599;
  101. #endif
  102. #ifdef PWM_FREQ_10KHZ
  103. TCCR1B = _BV(CS10) | _BV(WGM13);
  104. OCR1A = 0;
  105. OCR1B = 0;
  106. ICR1 = 795;
  107. #endif
  108. }
  109.  
  110.  
  111. void loop()
  112. {
  113. UpdateInputs();
  114. UpdateAnalogs();
  115. HardwarePWM();
  116. }
  117.  
  118. // Subroutines
  119. void UpdateInputs()
  120. // Update Digital inputs
  121. {
  122. // Direction for Motor 1
  123. if ((PINC & (1 << 2)) == 0)
  124. M1_direction = 1;
  125. else
  126. M1_direction = 0;
  127.  
  128. // Start-Stop for Motor 1
  129. if ((PINC & (1 << 3)) == 0)
  130. M1_start_stop = 1;
  131. else
  132. M1_start_stop = 0;
  133.  
  134. // Fault Input for Motor 1
  135. if ((PIND & (1 << 2)) == 0)
  136. M1_fault_in = 0;
  137. else
  138. M1_fault_in = 0;
  139.  
  140. // Direction for Motor 2
  141. if ((PINC & (1 << 5)) == 0)
  142. M2_direction = 1;
  143. else
  144. M2_direction = 0;
  145.  
  146. // Start-Stop for Motor 2
  147. if ((PINC & (1 << 4)) == 0)
  148. M2_start_stop = 1;
  149. else
  150. M2_start_stop = 0;
  151.  
  152. // Fault Input for Motor 2
  153. if ((PIND & (1 << 3)) == 0)
  154. M2_fault_in = 0;
  155. else
  156. M2_fault_in = 0;
  157. }
  158.  
  159. void UpdateAnalogs()
  160. {
  161. static unsigned char working = 0;
  162. static unsigned char adcPin = 0;
  163. static unsigned long tmp = 0;
  164.  
  165. // Check if the ADC module is free and not working
  166. if (!working)
  167. {
  168. // Select the queued ADC channel
  169. ADMUX = bit (REFS0) | (adcPin & 0x07);
  170. // Start a conversion
  171. bitSet (ADCSRA, ADSC);
  172. // Set the flag since the ADC is busy now.
  173. working = true;
  174. }
  175.  
  176. // The ADC clears the bit when done
  177. // Check if the ADC is still working and busy
  178. if (bit_is_clear(ADCSRA, ADSC))
  179. {
  180. // Read analog inputs
  181. // The result is from 0 to 1023
  182. if (adcPin == 0)
  183. {
  184. tmp = (unsigned long)((unsigned long)(ADC) * (unsigned long)(25));
  185. tmp = tmp >> 8;
  186. M1_torque_set_pnt = (unsigned int)(tmp);
  187. Serial.println(M1_torque_set_pnt);
  188. }
  189. else
  190. {
  191. #ifdef PWM_FREQ_1KHZ
  192. tmp = (unsigned long)((unsigned long)(ADC) * (unsigned long)(102));
  193. tmp = tmp >> 10;
  194. M2_torque_set_pnt = (unsigned int)(tmp);
  195. #endif
  196. #ifdef PWM_FREQ_5KHZ
  197. tmp = (unsigned long)((unsigned long)(ADC) * (unsigned long)(150));
  198. tmp = tmp >> 10;
  199. M2_torque_set_pnt = (unsigned int)(tmp);
  200. #endif
  201. #ifdef PWM_FREQ_10KHZ
  202. tmp = (unsigned long)((unsigned long)(ADC) * (unsigned long)(80));
  203. tmp = tmp >> 8;
  204. M2_torque_set_pnt = (unsigned int)(tmp);
  205. #endif
  206. }
  207. // ADC is no longer busy, clear the flag
  208. working = false;
  209. // Toggle between ADC channels A0 and A1
  210. if (adcPin == 0)
  211. adcPin = 1;
  212. else
  213. adcPin = 0;
  214. }
  215. }
  216.  
  217. void HardwarePWM()
  218. {
  219. // If the motor is in START condition
  220. if (M1_start_stop == 1)
  221. {
  222. // Set M1_D2 so that it
  223. // will enable M1
  224. set_M1_D2();
  225.  
  226. // If the direction is FORWARD
  227. if (M1_direction == 1)
  228. {
  229. OCR0A = M1_torque_set_pnt;
  230. OCR0B = 0;
  231. }
  232. else // if the direction is REVERSE
  233. {
  234. OCR0A = 0;
  235. OCR0B = M1_torque_set_pnt;
  236. }
  237. }
  238. // If the motor is in STOP condition
  239. else
  240. {
  241. // Clear M1_D2 so that it
  242. // will disable M1
  243. clear_M1_D2();
  244.  
  245. OCR0A = 0;
  246. OCR0B = 0;
  247. }
  248.  
  249.  
  250. // If the motor is in START condition
  251. if (M2_start_stop == 1)
  252. {
  253. // Clear M2_D1 and set M2_D2
  254. // This will enable M2
  255. clear_M2_D1();
  256. set_M2_D2();
  257.  
  258. // If the direction is FORWARD
  259. if (M2_direction == 1)
  260. {
  261. OCR1A = M2_torque_set_pnt;
  262. OCR1B = 0;
  263. }
  264. else // if the direction is REVERSE
  265. {
  266. OCR1A = 0;
  267. OCR1B = M2_torque_set_pnt;
  268. }
  269. }
  270. // If the motor is in STOP condition
  271. else
  272. {
  273. // Set M2_D1 and clear M2_D2
  274. // This will disable M2
  275. set_M2_D1();
  276. clear_M2_D2();
  277.  
  278. OCR1A = 0;
  279. OCR1B = 0;
  280. }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement