Пример #1
0
int main(void)
{
    unsigned char currentState = 0;
    nextState = NOINTERRUPT;

    for(int i = 0 ; i < PACKETSIZE ; i++){
        receive_buffer[i] = 255;
    }

    bufferpos = 0;

    while(1){
        if(nextState != NOINTERRUPT){
            currentState = nextState;
        }
        switch(currentState){
            case INITIALIZATION:
                Initialize_Mega2560();
                Initialize_USART0(9600);   //Fastest Stable Clock is 38400
                Initialize_USART1(9600);
                Initialize_Sabertooth();
                currentState = WAITFORHOST;
                break;

            case WAITFORHOST:
                cli();
                SendStringUSART0((unsigned char *)"ID: MainDrive\r\n");
                if(GetByteUART() == 'D'){
                    SendStringUSART0((unsigned char *)"Master Found. Switching to Drive Mode.\r\n");
                    currentState = DRIVING;
                    SendByteUSART0('r');
                    sei();
                    break;
                }
                _delay_ms(500);
                PORTB |= (1<<PB7);
                _delay_ms(100);
                PORTB &= ~(1<<PB7);
                _delay_ms(100);
                break;

            case DRIVING:
                break;
            case 3:
                break;

            default:
                break;


        }
    }
    return 0;
}
Пример #2
0
/** Main Function */
int main (void)
{
	/** Local Varibles */
	FATFS fs;
	FIL log;
	unsigned char recv;
	enum states {idle, running, sample, transmit} state;
	state = idle; //set initial state to idle
	
	initialize();
	
	// Initialize TIMER/COUNTER0
	initializeTIMER0();
	
	// Initialize file system, check for errors
	//if(initializeFAT(&fs) == ERR_NONE){ 
	
		// Open file for writing, create the file if it does not exist, truncate existing data, check for errors
		//f_open(&log, "/log.txt", FA_WRITE | FA_CREATE_ALWAYS);
		
		// Set TIMER/COUNTER0 period, check for errors
		setTIMER0(5, 255);
		PORTB=0b01000000;
		PORTC=0b11111111;
		
		// While switch A7 is on
		while (PINA & (1 << PA7))
		{
			switch(state){
				case idle:
					recv=GetByteUART();
					while(recv != 's')
						recv=GetByteUART();
					if(recv == 's')
						state = running;
					break;
				case running:
						while(recv != 's'){
							recv=GetByteUART();
							// If TIMER/COUNTER0 has elapsed (checkTimer)
							if(checkTIMER0()){
								state = sample;
								break;
							}
						}
						if(recv == 's')
							state = idle;
					break;
				case sample:
					// Read accelerometer data, write to file, check for errors (PORTF is the accelerometer)
					//read_adc(5); //5 is x, 6 is y
					//f_write(&log, read_adc(5), 8, 8);
					state = transmit;
					break;
				case transmit:
					state = idle;
					break;
				default:
					state = idle;
					break;
			
			}
		}
		// Close the file and unmount the file system, check for errors
	//} //else {//there were errors mounting the filesystem. do stuff
}