samir82show

servo motor control (Slave code)

Jan 20th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <mega32.h>
  2. #include <stdio.h>
  3. #define MAX 10
  4. #define MAXANGLE 1999
  5. #define MINANGLE 999
  6. #define MOTOR_PLUS 0x01
  7. #define MOTOR_MINUS 0x02
  8. #include <delay.h>
  9. unsigned char data;
  10. void init()
  11. {
  12. PORTA=0x01;
  13. DDRA=0x00;
  14. PORTB=0x00;
  15. DDRB=0x40;
  16. PORTC=0x00;
  17. DDRC=0x00;
  18. PORTD=0x00;
  19. DDRD=0b10100010;
  20.  
  21. TCCR1A=0b10000010;
  22. TCCR1B=0b00011001;
  23. ICR1H=0x4E;
  24. ICR1L=0x1F;
  25. OCR1A = 1499;
  26.  
  27. TIMSK=0b00000000;
  28.  
  29. UCSRA=0x02;
  30. UCSRB=0xD8;
  31. UCSRC=0x86;
  32. UBRRH=0x00;
  33. UBRRL=0x0C;
  34.  
  35. ACSR=0x80;
  36. SFIOR=0x00;
  37.  
  38. SPCR=0b11001000;
  39. SPSR=0x00;
  40. }
  41.  
  42. interrupt [SPI_STC] void spi_isr(void)
  43. {
  44. data=SPDR;
  45. }
  46.  
  47. void main(void)
  48. {
  49. bit state = 0;
  50. unsigned int Press_Conf = 0;
  51. char cmd[] = "horzental:122";
  52. init();
  53. // Clear the SPI interrupt flag
  54. #asm
  55. in r30,spsr
  56. in r30,spdr
  57. #endasm
  58. #asm("sei")
  59.  
  60. while (1)
  61. {
  62. // Place your code here
  63. if(data == MOTOR_PLUS)
  64. {
  65. if(OCR1A <= MAXANGLE)
  66. {
  67. OCR1A++;
  68. PORTD ^= 1 << PIND7;
  69. data = 0x00;
  70. }
  71. }
  72. else if(data == MOTOR_MINUS)
  73. {
  74. if(OCR1A >= MINANGLE)
  75. {
  76. OCR1A--;
  77. PORTD ^= 1 << PIND7;
  78. data = 0x00;
  79. }
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment