samir82show

Slave code v.1

Feb 24th, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include <mega32a.h>
  2. #include <delay.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <spi.h>
  6. #define MAX 10
  7. #define MAXANGLE 1999
  8. #define MINANGLE 999
  9. #define MOTOR_PLUS 0x01
  10. #define MOTOR_MINUS 0x02
  11. #define REMOTE_MOTOR 0x03
  12. unsigned int dump_mode1 = 1; //this dump variable is to keep synchronization with master
  13. unsigned int dump_mode2 = 1; //this dump variable is to keep synchronization with master
  14. unsigned int dump_mode3 = 1; //this dump variable is to keep synchronization with master
  15. unsigned int dump_mode4 = 1; //this dump variable is to keep synchronization with master
  16. // SPI interrupt service routine
  17. interrupt [SPI_STC] void spi_isr(void)
  18. {
  19. unsigned char data;
  20. data = SPDR;
  21. if(data == MOTOR_PLUS) // condition used for Auto/Manual mode
  22. {
  23. if(OCR1A <= MAXANGLE)
  24. {
  25. OCR1A++;
  26. PORTD ^= 1 << PIND7;
  27. }
  28. }
  29. else if(data == MOTOR_MINUS) // condition used for Auto/Manual mode
  30. {
  31. if(OCR1A >= MINANGLE)
  32. {
  33. OCR1A--;
  34. PORTD ^= 1 << PIND7;
  35. }
  36. }
  37. else
  38. {
  39. OCR1A = data * 10; //returning the value back to the PWM range values then assign it to OCR1A
  40. PORTD ^= 1 << PIND7;
  41. }
  42. }
  43.  
  44. // Declare your global variables here
  45. void init()
  46. {
  47. DDRB=0x40;
  48. DDRD=0xA0;
  49.  
  50. TCCR1A=0b10000010;
  51. TCCR1B=0b00011001;
  52. ICR1H=0x4E;
  53. ICR1L=0x1F;
  54. OCR1A = 1499;
  55.  
  56. TIMSK=0x00;
  57.  
  58. ACSR=0x80;
  59. SFIOR=0x00;
  60.  
  61. SPCR=0xC0;
  62. SPSR=0x00;
  63. }
  64. void main(void)
  65. {
  66. // Clear the SPI interrupt flag
  67. #asm
  68. in r30,spsr
  69. in r30,spdr
  70. #endasm
  71. // Global enable interrupts
  72. #asm("sei")
  73. init();
  74. while (1)
  75. {
  76. // Place your code here
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment