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>
- #undef USE_AUTO_MANUAL
- #define MAX 100
- #define MAXANGLE 1999
- #define MINANGLE 999
- #define MOTOR_PLUS 0x01
- #define MOTOR_MINUS 0x02
- #ifndef USE_AUTO_MANUAL
- struct cmd //command struct
- {
- char str[MAX], num[MAX];
- int i;
- } cmd;
- #endif
- #ifndef RXB8
- #define RXB8 1
- #endif
- #ifndef TXB8
- #define TXB8 0
- #endif
- #ifndef UPE
- #define UPE 2
- #endif
- #ifndef DOR
- #define DOR 3
- #endif
- #ifndef FE
- #define FE 4
- #endif
- #ifndef UDRE
- #define UDRE 5
- #endif
- #ifndef RXC
- #define RXC 7
- #endif
- #define FRAMING_ERROR (1<<FE)
- #define PARITY_ERROR (1<<UPE)
- #define DATA_OVERRUN (1<<DOR)
- #define DATA_REGISTER_EMPTY (1<<UDRE)
- #define RX_COMPLETE (1<<RXC)
- // USART Receiver buffer
- #define RX_BUFFER_SIZE 8
- char rx_buffer[RX_BUFFER_SIZE];
- #if RX_BUFFER_SIZE <= 256
- unsigned char rx_wr_index,rx_rd_index,rx_counter;
- #else
- unsigned int rx_wr_index,rx_rd_index,rx_counter;
- #endif
- // This flag is set on USART Receiver buffer overflow
- bit rx_buffer_overflow;
- // USART Receiver interrupt service routine
- interrupt [USART_RXC] void usart_rx_isr(void)
- {
- char status,data;
- status=UCSRA;
- data=UDR;
- if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
- {
- rx_buffer[rx_wr_index++]=data;
- #if RX_BUFFER_SIZE == 256
- // special case for receiver buffer size=256
- if (++rx_counter == 0)
- {
- #else
- if (rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
- if (++rx_counter == RX_BUFFER_SIZE)
- {
- rx_counter=0;
- #endif
- rx_buffer_overflow=1;
- }
- }
- }
- #ifndef _DEBUG_TERMINAL_IO_
- // Get a character from the USART Receiver buffer
- #define _ALTERNATE_GETCHAR_
- #pragma used+
- char getchar(void)
- {
- char data;
- while (rx_counter==0);
- data=rx_buffer[rx_rd_index++];
- #if RX_BUFFER_SIZE != 256
- if (rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
- #endif
- #asm("cli")
- --rx_counter;
- #asm("sei")
- return data;
- }
- #pragma used-
- #endif
- // USART Transmitter buffer
- #define TX_BUFFER_SIZE 8
- char tx_buffer[TX_BUFFER_SIZE];
- #if TX_BUFFER_SIZE <= 256
- unsigned char tx_wr_index,tx_rd_index,tx_counter;
- #else
- unsigned int tx_wr_index,tx_rd_index,tx_counter;
- #endif
- // USART Transmitter interrupt service routine
- interrupt [USART_TXC] void usart_tx_isr(void)
- {
- if (tx_counter)
- {
- --tx_counter;
- UDR=tx_buffer[tx_rd_index++];
- #if TX_BUFFER_SIZE != 256
- if (tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
- #endif
- }
- }
- #ifndef _DEBUG_TERMINAL_IO_
- // Write a character to the USART Transmitter buffer
- #define _ALTERNATE_PUTCHAR_
- #pragma used+
- void putchar(char c)
- {
- while (tx_counter == TX_BUFFER_SIZE);
- #asm("cli")
- if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
- {
- tx_buffer[tx_wr_index++]=c;
- #if TX_BUFFER_SIZE != 256
- if (tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
- #endif
- ++tx_counter;
- }
- else
- UDR=c;
- #asm("sei")
- }
- #pragma used-
- #endif
- #ifdef USE_AUTO_MANUAL
- volatile unsigned char mode;
- interrupt [EXT_INT0] void ext_int0_isr(void)
- {
- // Place your code here
- if(mode++ >= 1)
- 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;
- }
- }
- }
- #endif
- void init()
- {
- DDRA = 0xFF;
- DDRB = 0xB0;
- PORTC = 0xFE;
- DDRC = 0x01;
- DDRD = 0x30;
- PORTD = 0x04;
- TCCR0=0x09;
- TCNT0=0x00;
- OCR0=0x0F;
- TCCR1A=0xA2;
- TCCR1B=0x19;
- ICR1H=0x4E;
- ICR1L=0x1F;
- OCR1A=1499;
- OCR1B=1499;
- #ifndef USE_AUTO_MANUAL
- TIMSK=0x00;
- UCSRA=0x00;
- UCSRB=0xD8;
- UCSRC=0x06;
- UBRRH=0x00;
- UBRRL=0x0C;
- #else
- TIMSK=0x02;
- #endif
- ACSR=0x80;
- SFIOR=0x00;
- GICR|=0x40;
- MCUCR=0x02;
- MCUCSR=0x00;
- GIFR=0x40;
- SPCR=0x50;
- SPSR=0x00;
- }
- #ifndef USE_AUTO_MANUAL
- void command_split(struct cmd* command)
- {
- unsigned char i = 0;
- int c;
- while((command->str[0] = getchar()) != '$');
- while((c = getchar()) != EOF && c != ':' && i < MAX)
- command->str[i++] = c;
- command->str[i] = '\0';
- i = 0;
- while((c = getchar()) != EOF && i < MAX && c != '#')
- command->num[i++] = c;
- command->num[i] = '\0';
- command->i = atoi(command->num);
- }
- unsigned char
- read_line(char *line, char *word) {
- int c;
- unsigned char i = 0;
- while (i < MAX - 1 && (c = getchar()) != '\n')
- line[i++] = c;
- line[i] = '\0';
- if (strstr(line, word) != 0)
- return (1);
- else
- return (0);
- }
- #endif
- void main(void)
- {
- #ifdef USE_AUTO_MANUAL
- unsigned int Press_Conf = 0;
- #else
- char line[MAX], word[] = "+CMTI";
- #endif
- // Global enable interrupts
- #asm("sei")
- init();
- while (1)
- {
- // Place your code here
- #ifdef USE_AUTO_MANUAL
- 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
- #endif
- #ifndef USE_AUTO_MANUAL
- while(read_line(line, word) == 0);
- command_split(&cmd);
- if(!strcmpf(cmd.str,"vertical"))
- OCR1A = cmd.i;
- else if(!strcmpf(cmd.str,"horzental"))
- OCR1B = cmd.i;
- else if(!strcmpf(cmd.str,"grab"))
- spi(cmd.i/10);
- #endif
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment