Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #define F_CPU 8000000UL
  2. #define BAUD 9600
  3.  
  4. #include <avr/io.h>
  5. #include <avr/interrupt.h>
  6. #include <util/setbaud.h>
  7.  
  8. #include "robot.h"
  9.  
  10. //INTERRUPTS
  11.  
  12. ISR(USART_RXC_vect)
  13. {
  14. bluetooth_data=UDR; //receive data from hc-06
  15. bluetooth_flag=1;
  16. }
  17.  
  18. void uc_init(void);
  19. void pwm_init(void);
  20. void usart_init(void);
  21. void adc_init(void);
  22.  
  23. extern volatile uint8_t bluetooth_flag, bluetooth_data;
  24.  
  25. int main(void)
  26. {
  27. uc_init();
  28.  
  29. sei();
  30.  
  31. while(1)
  32. {
  33. if(bluetooth_flag==1)
  34. {
  35. bluetooth_flag=0;
  36.  
  37. if(bluetooth_data==65)
  38. {
  39. while(1)
  40. {
  41. if(bluetooth_flag==1)
  42. {
  43. bluetooth_flag=0;
  44. if(bluetooth_data==72) break;
  45. robot_control(bluetooth_data);
  46. }
  47. }
  48. }
  49.  
  50. else if(bluetooth_data==66)
  51. {
  52. TR_PORT|=(1<<TR_PIN);
  53. adc_init();
  54. line_follower();
  55. }
  56. }
  57. }
  58.  
  59. return 0;
  60. }
  61.  
  62. //MAIN FUNCTIONS
  63.  
  64. void uc_init(void)
  65. {
  66. DC_DDR|=(1<<DC_1_A)|(1<<DC_1_B)|(1<<DC_2_A)|(1<<DC_2_B); //engine outputs
  67. DC_PORT=0;
  68. TR_DDR|=(1<<TR_PIN); //transistor output
  69.  
  70. pwm_init();
  71.  
  72. usart_init();
  73. }
  74.  
  75. void pwm_init(void)
  76. {
  77. DDRB|=(1<<PB1)|(1<<PB3); //PWM pins
  78. TCCR2|=(1<<WGM20)|(1<<WGM21)|(1<<CS21)|(1<<COM21); //timer 2 PWM fcpu/8 prescaler, clear at top, set at bottom
  79. OCR2=255; //DC 1
  80. TCCR1A|=(1<<COM1A1)|(1<<WGM10); //timer 1 PWM
  81. TCCR1B|=(1<<WGM12)|(1<<CS11);
  82. OCR1A=255; //DC 2
  83. }
  84.  
  85. void usart_init(void)
  86. {
  87. UBRRH=UBRRH_VALUE; //usart speed configuration
  88. UBRRL=UBRRL_VALUE;
  89.  
  90. UCSRC|=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); //8bit 1stop no parity
  91. UCSRB|=(1<<TXEN)|(1<<RXEN)|(1<<RXCIE); //enable receiver and transmitter, receive interrupt enabled
  92. }
  93.  
  94. void adc_init(void)
  95. {
  96. ADCSRA|=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement