samir82show
Newbie level 5
- Joined
- Jan 21, 2014
- Messages
- 8
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 109
Hi, I'm Samir
I finished coding SPI control of three servo motors, two of them are connected to the first micro, the third is connected to the second micro.
My problem is there is specific variable unsigned char state = 0; has to be exist and initialized otherwise the Slave code doesn't run.
attached the proteus image of connectivity and the code below
Master Code http://pastebin.com/EHSsMsnr
Slave Code http://pastebin.com/3XvfCtkh
thanks
I finished coding SPI control of three servo motors, two of them are connected to the first micro, the third is connected to the second micro.
My problem is there is specific variable unsigned char state = 0; has to be exist and initialized otherwise the Slave code doesn't run.
attached the proteus image of connectivity and the code below
Master Code http://pastebin.com/EHSsMsnr
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 /***************************************************** This program was produced by the CodeWizardAVR V2.05.0 Professional Automatic Program Generator © Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l. [url]http://www.hpinfotech.com[/url] Project : Version : Date : 1/20/2014 Author : NeVaDa Company : Comments: Chip type : ATmega32 Program type : Application AVR Core Clock frequency: 1.000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 512 *****************************************************/ #include <mega32.h> // SPI functions #include <spi.h> #define MAXANGLE 1999 #define MINANGLE 999 #define MOTOR_PLUS 0x08 #define MOTOR_MINUS 0x04 unsigned int Press_Conf = 0; void main(void) { // Declare your local variables here // Input/Output Ports initialization // Port A initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00; DDRA=0x00; // Port B initialization // Func7=Out Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In // State7=0 State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T PORTB=0x00; DDRB=0xB0; // Port C initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0xFE; DDRC=0x01; // Port D initialization // Func7=In Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T PORTD=0x00; DDRD=0x30; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped // Mode: Normal top=0xFF // OC0 output: Disconnected TCCR0=0x00; TCNT0=0x00; OCR0=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: Timer1 Stopped // Mode: Fast PWM top=ICR1 // OC1A output: Non-Inv. // OC1B output: Non-Inv. // Noise Canceler: Off // Input Capture on Falling Edge // Timer1 Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off TCCR1A=0xA2; TCCR1B=0x19; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x4E; ICR1L=0x1F; OCR1AH=0x03; OCR1AL=0xE7; OCR1BH=0x03; OCR1BL=0xE7; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer2 Stopped // Mode: Normal top=0xFF // OC2 output: Disconnected ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; // External Interrupt(s) initialization // INT0: Off // INT1: Off // INT2: Off MCUCR=0x00; MCUCSR=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x00; // USART initialization // USART disabled UCSRB=0x00; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00; // ADC initialization // ADC disabled ADCSRA=0x00; // SPI initialization // SPI Type: Master // SPI Clock Rate: 250.000 kHz // SPI Clock Phase: Cycle Start // SPI Clock Polarity: Low // SPI Data Order: MSB First SPCR=0x50; SPSR=0x00; // TWI initialization // TWI disabled TWCR=0x00; while (1) { // Place your code here if(PINC.7 == 0) { Press_Conf++; if(Press_Conf >= 500) { if(OCR1A < MAXANGLE) { OCR1A = OCR1A + 1; PORTC ^= 1 << PINC0; } Press_Conf = 0; } } else if(PINC.6 == 0) { Press_Conf++; if(Press_Conf >= 500) { if(OCR1A > MINANGLE) { OCR1A = OCR1A - 1; PORTC ^= 1 << PINC0; } Press_Conf = 0; } } else if(PINC.5 == 0) { Press_Conf++; if(Press_Conf >= 500) { if(OCR1B < MAXANGLE) { OCR1B = OCR1B + 1; PORTC ^= 1 << PINC0; } Press_Conf = 0; } } else if(PINC.4 == 0) { Press_Conf++; if(Press_Conf >= 500) { if(OCR1B > MINANGLE) { OCR1B = OCR1B - 1; PORTC ^= 1 << PINC0; } Press_Conf = 0; } } else if(PINC.3 == 0) { Press_Conf++; if(Press_Conf >= 500) { spi(MOTOR_PLUS); Press_Conf = 0; } } else if(PINC.2 == 0) { Press_Conf++; if(Press_Conf >= 500) { spi(MOTOR_MINUS); Press_Conf = 0; } } } }
Slave Code http://pastebin.com/3XvfCtkh
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 /***************************************************** This program was produced by the CodeWizardAVR V2.05.0 Professional Automatic Program Generator © Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l. [url]http://www.hpinfotech.com[/url] Project : Version : Date : 1/20/2014 Author : NeVaDa Company : Comments: Chip type : ATmega32 Program type : Application AVR Core Clock frequency: 1.000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 512 *****************************************************/ #include <mega32.h> #define MOTOR_PLUS 0x08 #define MOTOR_MINUS 0x04 #define MAXANGLE 1999 #define MINANGLE 999 unsigned char data; unsigned char state = 0; // SPI interrupt service routine interrupt [SPI_STC] void spi_isr(void) { data=SPDR; } // Declare your global variables here void main(void) { // Declare your local variables here // Input/Output Ports initialization // Port A initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00; DDRA=0x00; // Port B initialization // Func7=In Func6=Out Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=0 State5=T State4=T State3=T State2=T State1=T State0=T PORTB=0x00; DDRB=0x40; // Port C initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0x00; DDRC=0x00; // Port D initialization // Func7=In Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T PORTD=0x00; DDRD=0xB0; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped // Mode: Normal top=0xFF // OC0 output: Disconnected TCCR0=0x00; TCNT0=0x00; OCR0=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: Timer1 Stopped // Mode: Fast PWM top=ICR1 // OC1A output: Non-Inv. // OC1B output: Non-Inv. // Noise Canceler: Off // Input Capture on Falling Edge // Timer1 Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off TCCR1A=0xA2; TCCR1B=0x19; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x4E; ICR1L=0x1F; OCR1AH=0x03; OCR1AL=0xE7; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer2 Stopped // Mode: Normal top=0xFF // OC2 output: Disconnected ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; // External Interrupt(s) initialization // INT0: Off // INT1: Off // INT2: Off MCUCR=0x00; MCUCSR=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x00; // USART initialization // USART disabled UCSRB=0x00; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00; // ADC initialization // ADC disabled ADCSRA=0x00; // SPI initialization // SPI Type: Slave // SPI Clock Phase: Cycle Start // SPI Clock Polarity: Low // SPI Data Order: MSB First SPCR=0xC0; SPSR=0x00; // Clear the SPI interrupt flag #asm in r30,spsr in r30,spdr #endasm // TWI initialization // TWI disabled TWCR=0x00; // Global enable interrupts #asm("sei") while (1) { // Place your code here if(data == MOTOR_PLUS) { if(OCR1A < MAXANGLE) { OCR1A = OCR1A + 1; PORTD ^= 1 << PIND7; data = 0x00; } } else if(data == MOTOR_MINUS) { if(OCR1A > MINANGLE) { OCR1A = OCR1A - 1; PORTD ^= 1 << PIND7; data = 0x00; } } } }
thanks
Last edited by a moderator: