Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. /*
  2. * stepper.c
  3. *
  4. * Created on: Mar 20, 2018
  5. * Author: ranasl62
  6. */
  7.  
  8. #include <avr/io.h>
  9. #include <stdio.h>
  10. #include <util/delay.h>
  11.  
  12. #define F_CPU 16000000UL /**< Clock speed for delay functions. */
  13.  
  14. #define FOSC 16000000 /**< Clock speed for UBRR calculation. refer page 179 of 328p datasheet. */
  15. #define BAUD 9600 /**< Baud Rate in bps. refer page 179 of 328p datasheet. */
  16. #define MYUBRR FOSC/16/BAUD-1 /**< UBRR = (F_CPU/(16*Baud))-1 for asynch USART page 179 328p datasheet. Baud rate 9600bps, assuming 16MHz clock UBRR0 becomes 0x0067*/
  17.  
  18.  
  19. #define Stepper_PORT PORTC
  20. #define Stepper_DDR DDRC
  21.  
  22. #define A PC3
  23. #define A_Prime PC2
  24. #define B PC1
  25. #define B_Prime PC0
  26. #define L298_EN PC4
  27.  
  28. int step_change;
  29. uint8_t direction;
  30. static long int pos;
  31.  
  32. #define Clockwise 0
  33. #define CounterClockwise 1
  34.  
  35. int f=0,l=0; //front and end of queue
  36.  
  37. struct arr{
  38. char String[3],x;
  39. int a;
  40. }ax[100];
  41.  
  42.  
  43. const uint8_t Full_step_FSM[4]=
  44. {
  45. /*
  46. A A' B B'
  47. 1 0 0 0
  48. 1 0 1 0
  49. 0 1 1 0
  50. 0 1 0 1
  51.  
  52. */
  53. 0b1000,
  54. 0b1010,
  55. 0b0110,
  56. 0b0101
  57. };
  58.  
  59. /**
  60. * @brief Initialize USART for 8 bit data transmit no parity and 1 stop bit.
  61. *
  62. *@details This is a code snippet from datasheet page 182
  63. *
  64. * @param ubrr The UBRR value calculated in macro MYUBRR
  65. * @see MYUBRR
  66. */
  67. void USART_init(unsigned int ubrr)
  68. {
  69.  
  70. UCSR0C = (0<<USBS0)|(3<<UCSZ00); /// Step 1. Set UCSR0C in Asynchronous mode, no parity, 1 stop bit, 8 data bits
  71. UCSR0A = 0b00000000;/// Step 2. Set UCSR0A in Normal speed, disable multi-proc
  72.  
  73. UBRR0H = (unsigned char)(ubrr>>8);/// Step 3. Load ubrr into UBRR0H and UBRR0L
  74. UBRR0L = (unsigned char)ubrr;
  75.  
  76.  
  77. UCSR0B = 0b00011000;/// Step 4. Enable Tx Rx and disable interrupt in UCSR0B
  78. }
  79.  
  80. /**
  81. * @brief Send 8bit data.
  82. *
  83. *@details This is a code snippet from datasheet page 184
  84. *
  85. * @param data The 8 bit data to be sent
  86. */
  87.  
  88. int USART_send(char c, FILE *stream)
  89. {
  90.  
  91. while ( !( UCSR0A & (1<<UDRE0)) )/// Step 1. Wait until UDRE0 flag is high. Busy Waitinig
  92. {;}
  93.  
  94. UDR0 = c; /// Step 2. Write char to UDR0 for transmission
  95. }
  96. /**
  97. * @brief Receive 8bit sata.
  98. *
  99. *@details This is a code snippet from datasheet page 187
  100. *
  101. * @return Returns received data from UDR0
  102. */
  103. int USART_receive(FILE *stream )
  104. {
  105.  
  106. while ( !(UCSR0A & (1<<RXC0)) )/// Step 1. Wait for Receive Complete Flag is high. Busy waiting
  107. ;
  108.  
  109. return UDR0;/// Step 2. Get and return received data from buffer
  110. }
  111.  
  112. void init_stepper_ports()
  113. {
  114. Stepper_DDR |= (1<<A)|(1<<A_Prime)|(1<<B)|(1<<B_Prime)|(1<<L298_EN);
  115. Stepper_PORT &= ~(1<<L298_EN);// Disable L298
  116. }
  117. uint8_t get_index(long int step_count)
  118. {
  119. uint8_t idx = 0;
  120. uint8_t temp_idx=0;
  121. if(step_count%4 < 0)
  122. {
  123. temp_idx = (step_count%4)+4;
  124. }
  125. else
  126. temp_idx = step_count%4;
  127.  
  128. if(temp_idx)
  129. idx = (temp_idx);
  130.  
  131. printf("index %d\r",idx);
  132. return idx;
  133. }
  134. void stepper_FSM(long steps)
  135. {
  136. static long int step_counter=0;
  137. long i =0;
  138. uint8_t bit_mask=0b1111;
  139. for(i=0;i<steps;i++)
  140. {
  141. Stepper_PORT = (Stepper_PORT & ~(bit_mask))|(Full_step_FSM[get_index(step_counter)]);
  142. if(direction== Clockwise){
  143. step_counter +=1;
  144. pos=(pos%200)+1;
  145. }
  146. else{
  147. step_counter-=1;
  148. pos=(pos%200)-1;
  149. //printf("%d %d\r",posi,pos);
  150. }
  151. _delay_ms(150);
  152. }
  153. }
  154. void pop(){
  155. if(f<l)f--;
  156. }
  157. void top(){
  158. if(f<l&&l!=0)
  159. printf("%s %c %d\r",ax[f].String,ax[f].x,ax[f].a);
  160. }
  161. int main()
  162. {
  163. USART_init(MYUBRR);
  164. // Initialize the standard IO handlers
  165. stdout = fdevopen(USART_send, NULL);
  166. stdin = fdevopen(NULL, USART_receive);
  167. init_stepper_ports();
  168.  
  169. Stepper_PORT |= (1<<L298_EN);
  170. pos=0;
  171. uint8_t en_dis=1;
  172. uint8_t q_en=0;
  173. float unit=0.5;
  174. long int posi=0;
  175.  
  176. while(1)
  177. {
  178.  
  179. int16_t a;
  180. unsigned char str[3];
  181. scanf("%s",str);
  182. printf("%s\r",str);
  183. if(q_en){
  184. ax[l].String[0]=str[0];
  185. ax[l].String[1]=str[1];
  186. ax[l].String[2]=str[2];
  187. }
  188. if(str[0]=='M'&&str[1]=='1'&&str[2]=='7'){
  189. en_dis=1;
  190. }
  191. else if(str[0]=='M'&&str[1]=='1'&&str[2]=='8'){
  192. en_dis=0;
  193. }
  194. else if(en_dis){
  195. if((str[0]=='M'&&str[1]=='2')||(str[0]=='M'&&str[1]=='3'&&str[2]=='0')){
  196. return 0;
  197. }
  198. else if(str[0]=='M'&&str[1]=='0'&&str[2]=='0')
  199. {
  200. q_en=1;
  201. }
  202. else if(str[0]=='G'&&str[1]=='2'&&str[2]=='8'){
  203. //printf("%d %d\r",posi,pos);
  204. a=((posi-pos)*unit+0.5);
  205. if(a<0){
  206. a=a*(-1);
  207. //printf("Yes CounterClockwise\r");
  208. //set_direction(CounterClockwise);
  209. //printf("%d\r",a);
  210. direction=CounterClockwise;
  211. stepper_FSM(a);
  212. _delay_ms(300);
  213. }
  214. else {
  215. //printf("Yes Clockwise\r");
  216. //set_direction(Clockwise);
  217. //printf("%d\r",a);
  218. direction=Clockwise;
  219. stepper_FSM(a);
  220. _delay_ms(300);
  221.  
  222. }
  223. //pos=0;
  224.  
  225. }
  226. else if(str[0]=='G' && str[1]=='2' && str[2]=='0'){
  227. unit=22.86;
  228.  
  229. }
  230. else if(str[0]=='G' && str[1]=='2' && str[2]=='1'){
  231. unit=0.5;
  232.  
  233. }
  234. else if(str[0]=='G' && str[1]=='9' && str[2]=='0'){
  235. posi=0;
  236.  
  237. }
  238. else if(str[0]=='G' && str[1]=='9' && str[2]=='1'){
  239. //printf("%d %d\r",pos,posi);
  240. posi=pos;
  241. }
  242. else if(str[0]=='G' && str[1]=='0' && str[2]=='1'){
  243. //scanf("%s",&str);
  244. unsigned char x;
  245. scanf(" %c",&x);
  246. scanf("%d",&a);
  247. if(q_en){
  248. ax[l].a=a;
  249. ax[l].x=x;
  250. }
  251. a=((a*unit)+0.05);
  252. // printf("%d\r",a);
  253. if(a<0){
  254. a=a*(-1);
  255. //printf("Yes CounterClockwise\r");
  256. //set_direction(CounterClockwise);
  257. direction=CounterClockwise;
  258. stepper_FSM(a);
  259. // printf("%d %d\r",pos,posi);
  260. _delay_ms(300);
  261. }
  262. else {
  263. //printf("Yes Clockwise\r");
  264. //set_direction(Clockwise);
  265. direction=Clockwise;
  266. stepper_FSM(a);
  267. _delay_ms(300);
  268.  
  269. }
  270. //printf("%d %d\r",pos,posi);
  271. }
  272. Stepper_PORT=0x00;//release power to motor
  273. if(q_en){
  274. l++;
  275. }
  276. }
  277.  
  278.  
  279.  
  280. }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement