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>
- #undefine AUTO_MANUAL //define and compile to use automated + manual push button modes, undefine and compile to use GSM mode
- #define MAX 10
- #define MAXANGLE 1999
- #define MINANGLE 999
- #define MOTOR_PLUS 0x01
- #define MOTOR_MINUS 0x02
- #ifndef AUTO_MANUAL
- struct cmd // struct to hold the splitted command
- {
- char str[MAX], num[MAX];
- unsigned int i;
- } cmd, *cmdptr;
- #endif
- volatile unsigned char mode; //variable mode switching between Auto and Manual mode
- // External Interrupt 0 service routine for mode switching
- interrupt [EXT_INT0] void ext_int0_isr(void)
- {
- // Place your code here
- if(mode++ >= 1)
- mode = 0;
- printf("%d",mode);
- }
- #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
- // Timer 0 output compare interrupt service routine
- #ifdef AUTO_MANUAL
- interrupt [TIM0_COMP] void timer0_comp_isr(void)
- {
- //these are the states of the automatic mode they are driven 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
- #ifndef AUTO_MANUAL
- void command_split(struct cmd* command) //this function is to process command into two values
- {
- unsigned char i = 0;
- int c;
- while((command->str[0] = getchar()) != '$');
- while((c = getchar()) != ':' && i < MAX)
- command->str[i++] = c;
- command->str[i] = '\0';
- i = 0;
- while((c = getchar()) != '#' && i < MAX)
- command->num[i++] = c;
- command->num[i] = '\0';
- command->i = atoi(command->num);
- }
- #endif
- void init() //initialization function
- {
- 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;
- #ifdef AUTO_MANUAL
- TIMSK = 0x02;
- #else
- TIMSK = 0x00;
- UCSRA=0x00;
- UCSRB=0xD8;
- UCSRC=0x06;
- UBRRH=0x00;
- UBRRL=0x0C;
- #endif
- ACSR=0x80;
- SFIOR=0x00;
- GICR|=0x40;
- MCUCR=0x02;
- MCUCSR=0x00;
- GIFR=0x40;
- SPCR=0x50;
- SPSR=0x00;
- }
- void main(void)
- {
- #ifndef AUTO_MANUAL
- unsigned char minimized_angle; //this variable is to hold the value sent to the remote motor in the slave micro
- cmdptr = &cmd; //command struct object assignment
- #endif
- #ifdef AUTO_MANUAL
- unsigned int Press_Conf = 0;
- #endif
- // Global enable interrupts
- #asm("sei")
- init();
- while (1)
- {
- // Place your code here
- #ifdef 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 AUTO_MANUAL //start of GSM mode
- cmdptr = &cmd;
- command_split(cmdptr);
- if(!strcmpf(cmd.str,"vertical"))
- {
- OCR1A = cmd.i;
- }
- else if(!strcmpf(cmd.str,"horzental"))
- {
- OCR1B = cmd.i;
- }
- else if(!strcmpf(cmd.str,"grab"))
- {
- minimized_angle = cmd.i/10; //dividing the value of degree by 10 to get char size number to be able to send it thru SPI
- spi(minimized_angle); //sending the minimized angle value through SPI
- } //end of GSM mode
- #endif
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment