Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #define F_CPU 8000000UL
  2. #include <avr/io.h>
  3. #include <avr/pgmspace.h>
  4. #include <util/delay.h>
  5. #include <stdio.h>
  6. #include "mirf.h"
  7. #include "mirf.c"
  8. #include "spi.h"
  9. #include "spi.c"
  10. #include <stdlib.h>
  11.  
  12. // adres nadawania clie1 = 63:6c:69:65:31 zrodlo: http://dolcevie.com/js/converter.html
  13. uint8_t tx_address[5] = { 0x63, 0x6c, 0x69, 0x65, 0x31 };
  14.  
  15. // adres odbioru serv1 = 73:65:72:76:31 zrodlo: http://dolcevie.com/js/converter.html
  16. uint8_t rx_address[5] = { 0x73, 0x65, 0x72, 0x76, 0x31 };
  17.  
  18. void init_pwm()
  19. {
  20. DDRD |= (1<<PD4)|(1<<PD5);
  21.  
  22. /*TCCR1A*/
  23. TCCR1A |=(1<<5)|(1<<COM1A1)|(1<<WGM12)|(1<<WGM11);
  24. TCCR1A &= ~(1<<COM1A0);
  25. TCCR1B |= (1<<WGM13);
  26.  
  27. /*TCCR1B*/
  28.  
  29. TCCR1B |= (1<<7)|(0<<CS12 | 1<<CS11 | 1<<CS10);
  30.  
  31. /*NADANIE START. WARTOSCI*/
  32. OCR1A=0;
  33. OCR1B=0;
  34. ICR1=255;
  35. }
  36.  
  37. void init_engines()
  38. {
  39. DDRA |= (1<<PA0)|(1<<PA1);
  40. DDRD |=(1<<PD7);
  41. DDRC |=(1<<PC0);
  42. }
  43.  
  44. void watch_dog_engines()
  45. {
  46. OCR1A=0;
  47. OCR1B=0;
  48.  
  49. //PORTA = PORTA & (0b11111100);
  50. //PORTD = PORTD & (0b01111111);
  51. //PORTC = PORTC & (0b11111110);
  52. }
  53.  
  54. void steer_pwm(int adc_from_pilot)
  55. {
  56. int max_pwm = 980;
  57. float pwm_for_left=90+((980-adc_from_pilot)/6);
  58. float pwm_for_right=90+(adc_from_pilot/6);
  59.  
  60. OCR1A = pwm_for_left;
  61. OCR1B = pwm_for_right;
  62. }
  63.  
  64. void go_forward()
  65. {
  66. PORTA |=(1<<PA0);
  67. PORTD |=(1<<PD7);
  68. PORTC &=~(1<<PC0);
  69. PORTA &=~(1<<PA1);
  70. }
  71.  
  72. void go_backward()
  73. {
  74. PORTA |=(1<<PA1);
  75. PORTD &=~(1<<PD7);
  76. PORTC |=(1<<PC0);
  77. PORTA &=~(1<<PA0);
  78. }
  79.  
  80. steer_engines(char engine1, char engine2)
  81. {
  82. if(engine1 == '1' && engine2 == '0')
  83. go_forward();
  84. else if(engine1 == '0' && engine2 == '1')
  85. go_backward();
  86. else
  87. watch_dog_engines();
  88. }
  89.  
  90. int mainloop()
  91. {
  92. int connection_lost=0;
  93.  
  94. PORTD &= ~(1<<PD6);
  95. _delay_ms(10);
  96.  
  97. watch_dog_engines();
  98.  
  99. while(1)
  100. {
  101. char to_print[14];
  102. char temp[12];
  103. PORTD |=(1<<PD6);
  104.  
  105. while(!mirf_data_ready())
  106. {
  107. _delay_us(20);
  108. connection_lost++;
  109.  
  110. if(connection_lost==0)
  111. mainloop();
  112. }
  113.  
  114. mirf_get_data(to_print);
  115. connection_lost=0;
  116.  
  117.  
  118.  
  119. for(int i=2;i<14;i++)
  120. temp[i-2]=to_print[i];
  121.  
  122. int digital_value=atoi(temp);
  123. steer_pwm(digital_value);
  124. steer_engines(to_print[0], to_print[1]);
  125.  
  126. _delay_ms(10);
  127. PORTD &= ~(1<<PD6);
  128. _delay_ms(10);
  129. }
  130.  
  131. return 0;
  132. }
  133.  
  134. int main()
  135. {
  136. DDRD |= (1<<PD6);
  137.  
  138. mirf_init();
  139. _delay_ms(50);
  140. sei();
  141.  
  142. mirf_set_RADDR(rx_address);
  143. mirf_set_TADDR(tx_address);
  144.  
  145. mirf_config();
  146.  
  147. init_pwm();
  148. init_engines();
  149.  
  150. mainloop();
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement