Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mega32a.h>
- #include <delay.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <spi.h>
- #define MAX 10
- #define MAXANGLE 1999
- #define MINANGLE 999
- #define MOTOR_PLUS 0x01
- #define MOTOR_MINUS 0x02
- struct cmd //command struct
- {
- char str[MAX], num[MAX];
- int i;
- } cmd, *cmdptr;
- volatile unsigned char mode;
- interrupt [EXT_INT0] void ext_int0_isr(void)
- {
- // Place your code here
- if(mode++ >= 2)
- mode = 0;
- printf("%d",mode);
- }
- interrupt [TIM0_COMP] void timer0_comp_isr(void)
- {
- //these are the states of the automatic mode they are drove by counter 0
- 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:
- if(OCR1A++ < MAXANGLE);
- else
- state = 2;
- break;
- case 2:
- if(OCR1B++ < MAXANGLE);
- else
- state = 3;
- break;
- case 3:
- if(OCR1Avar++ <= MAXANGLE)
- spi(MOTOR_PLUS);
- else
- state = 4;
- break;
- case 4:
- if(OCR1B-- > MINANGLE);
- else
- state = 5;
- break;
- case 5:
- if(OCR1A-- > MINANGLE);
- else
- state = 6;
- break;
- case 6:
- if(OCR1B++ < MAXANGLE);
- else
- state = 7;
- break;
- case 7:
- if(OCR1Avar-- >= MINANGLE)
- spi(MOTOR_MINUS);
- else
- state = 8;
- break;
- case 8:
- if(OCR1B-- > MINANGLE);
- else
- state = 1;
- break;
- }
- count = 0;
- }
- }
- }
- void init()
- {
- PORTA = 0x00;
- DDRA = 0xFF;
- PORTB = 0x00;
- DDRB = 0xB0;
- PORTC = 0xFE;
- DDRC = 0x01;
- PORTD = 0x04;
- DDRD = 0x30;
- TCCR0=0x09;
- TCNT0=0x00;
- OCR0=0x0F;
- TCCR1A=0xA2;
- TCCR1B=0x19;
- TCNT1H=0x00;
- TCNT1L=0x00;
- ICR1H=0x4E;
- ICR1L=0x1F;
- OCR1A = 1499;
- OCR1B = 1499;
- TIMSK=0x02;
- UCSRA=0x02;
- UCSRB=0b10011000;
- UCSRC=0x06;
- UBRRH=0x00;
- UBRRL=0x0C;
- ACSR=0x80;
- SFIOR=0x00;
- GICR|=0x40;
- MCUCR=0x02;
- MCUCSR=0x00;
- GIFR=0x40;
- SPCR=0x50;
- SPSR=0x00;
- }
- void command_split(struct cmd* command)
- {
- unsigned char i = 0;
- int c;
- while((c = getchar()) != EOF && c != ':' && i < MAX)
- command->str[i++] = c;
- command->str[i] = '\0';
- i = 0;
- while((c = getchar()) != EOF && i < MAX)
- command->num[i++] = c;
- command->num[i] = '\0';
- command->i = atoi(command->num);
- }
- void main(void)
- {
- unsigned int Press_Conf = 0;
- cmdptr = &cmd; //command struct object assignment
- // Global enable interrupts
- #asm("sei")
- init();
- while (1)
- {
- // Place your code here
- if(mode == 1)
- {
- Press_Conf++;
- if(Press_Conf >= 200)
- {
- if(PINC.7 == 0)
- {
- if(OCR1A < MAXANGLE)
- OCR1A++;
- }
- else if(PINC.6 == 0)
- {
- if(OCR1A > MINANGLE)
- OCR1A--;
- }
- if(PINC.5 == 0)
- {
- if(OCR1B < MAXANGLE)
- OCR1B++;
- }
- else if(PINC.4 == 0)
- {
- if(OCR1B > MINANGLE)
- OCR1B--;
- }
- if(PINC.3 == 0)
- {
- spi(0x01);
- }
- else if(PINC.2 == 0)
- {
- spi(0x02);
- }
- Press_Conf = 0;
- }
- }//end of mode 1
- else if(mode == 2)
- {
- cmdptr = &cmd;
- command_split(cmdptr);
- if(!strcmpf(cmd.str,"nothing"))
- putchar('N');;
- else if(!strcmpf(cmd.str,"vertical"))
- puts(cmd.str);
- else if(strcmpf(cmd.str,"horzental"))
- puts(cmd.str);
- else if(!strcmpf(cmd.str,"grab"))
- puts(cmd.str);
- } //end of mode 2
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment