Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mega32a.h>
- #include <spi.h>
- #define MAXANGLE 1999
- #define MINANGLE 999
- #define MOTOR_PLUS 0x01
- #define MOTOR_MINUS 0x02
- volatile unsigned char mode;
- // Timer 0 overflow interrupt service routine
- interrupt [TIM0_OVF] void timer0_ovf_isr(void)
- {
- // Place your code here
- static unsigned int OCR1Avar = 1499;
- static unsigned char state = 1;
- static unsigned char count = 0;
- count++;
- if(!mode)
- {
- if(count >= 10)
- {
- switch(state)
- {
- case 1: //forward Axial movement
- if(OCR1A++ < MAXANGLE);
- else
- state = 2;
- break;
- case 2: //forward Vertical movement
- if(OCR1B++ < MAXANGLE);
- else
- state = 3;
- break;
- case 3: // grabbing object
- if(OCR1Avar++ <= MAXANGLE)
- spi(MOTOR_PLUS);
- else
- state = 4;
- break;
- case 4: //reverse Vertical movement
- if(OCR1B-- > MINANGLE);
- else
- state = 5;
- break;
- case 5: //reverse Axial movement
- if(OCR1A-- > MINANGLE);
- else
- state = 6;
- break;
- case 6: //forward Vertical movement
- if(OCR1B++ < MAXANGLE);
- else
- state = 7;
- break;
- case 7: //release object
- if(OCR1Avar-- >= MINANGLE)
- spi(MOTOR_MINUS);
- else
- state = 8;
- break;
- case 8: //reverse Vertical movement
- if(OCR1B-- > MINANGLE);
- else
- state = 1;
- break;
- }
- count = 0;
- }
- }
- }
- void main(void)
- {
- // Declare your local variables here
- unsigned char Press_Conf = 0;
- // 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: 1000.000 kHz
- // Mode: CTC top=OCR0
- // OC0 output: Disconnected
- TCCR0=0x09;
- TCNT0=0x00;
- OCR0=0xFF;
- // Timer/Counter 1 initialization
- // Clock source: System Clock
- // Clock value: 1000.000 kHz
- // 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=0b10100010;
- TCCR1B=0b00011001;
- TCNT1H=0x00;
- TCNT1L=0x00;
- ICR1H=0x4E;
- ICR1L=0x1F;
- //OCR1AH=0x05;
- //OCR1AL=0xDB;
- //OCR1BH=0x05;
- OCR1A = 1499;
- OCR1B = 1499;
- // 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=0x01;
- // 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;
- // Global enable interrupts
- #asm("sei")
- while (1)
- {
- // Place your code here
- if(PINC.1 == 0)
- {
- Press_Conf++;
- if(Press_Conf >= 255)
- {
- mode = ~mode;
- PORTC ^= 1 << PINC0;
- Press_Conf = 0;
- }
- }
- if(mode)
- {
- if(PINC.7 == 0)
- {
- Press_Conf++;
- if(Press_Conf >= 255)
- {
- if(OCR1A < MAXANGLE)
- OCR1A++;
- Press_Conf = 0;
- }
- }
- else if(PINC.6 == 0)
- {
- Press_Conf++;
- if(Press_Conf >= 255)
- {
- if(OCR1A > MINANGLE)
- OCR1A--;
- Press_Conf = 0;
- }
- }
- if(PINC.5 == 0)
- {
- Press_Conf++;
- if(Press_Conf >= 255)
- {
- if(OCR1B < MAXANGLE)
- OCR1B++;
- }
- }
- else if(PINC.4 == 0)
- {
- Press_Conf++;
- if(Press_Conf >= 255)
- {
- if(OCR1B > MINANGLE)
- OCR1B--;
- Press_Conf = 0;
- }
- }
- if(PINC.3 == 0)
- {
- Press_Conf++;
- if(Press_Conf >= 255)
- {
- spi(MOTOR_PLUS);
- Press_Conf = 0;
- }
- }
- else if(PINC.2 == 0)
- {
- Press_Conf++;
- if(Press_Conf >= 255)
- {
- spi(MOTOR_MINUS);
- Press_Conf = 0;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment