// PC_Comm.c // Doug Bemis // S. Haynes, Microprocessors // 12/14/06 // Project 3, Light direction/speed change // when interrupt (push) thrown #include "Demonstrator.h" #include "PC_Comm.h" extern int go; extern int milliseconds; extern int seconds; extern int totalS; extern int tries; extern int running; extern int trials[]; extern int times[]; extern int success; extern int lit; int main(void) { // run the initialization routines initializer(); // Init port pins DDRB = 0x00; // set port B for input DDRD = 0xFF; // set port D for output //used for outputting values to the PC if it is connected //char* buf = (char *)(malloc(20*sizeof(char))); //Begin forever chatting with the PC for(;;) { if (go != 0) { /* initialize random generator */ srand ( milliseconds ); tries=0; //used for outputting values to the PC if it is connected char* buf = (char *)(malloc(30*sizeof(char))); while (tries < 10) { _delay_loop_2(30000); sendString("Get ready!\r"); running = 1; PORTD=0x00; //random time between 1 second and 5 seconds int tmax = (abs(rand())*1000)%4000; //make sure tmax is positive if (tmax <0) tmax *= -1; //tmax min is 1second, or 1000 ms, so add it incase rand=0 tmax += 1000; int compare = 0; milliseconds=0; //wait the random time while (getMillis() < tmax); //reset milliseconds to count for user now milliseconds = 0; compare =0; lit=1; //light it up! PORTD=0xFF; //wait till they push the button, or 8 seconds elapse while (getMillis() < 8000 && running==1); PORTD=0x00; lit=0; //didnt press in 8 seconds, mark it if (success==0) { trials[tries] = 8000; tries++; running = 0; } //store the time wait times[tries-1] = tmax; success=0; if (tries <10) { buf = (char *)(malloc(20*sizeof(char))); sprintf(buf,"Got it... %d left",9-tries); sendString(buf); sendChar('\r'); free(buf); } } int total = 0; for (int x=0; x< 10; x++) { total += trials[x]; buf = (char *)(malloc(20*sizeof(char))); sprintf(buf,"Try %d: You took %dms on a wait of %d",x+1,trials[x],times[x]); sendString(buf); sendChar('\r'); free(buf); } float avg = (float)total/10.0; sendString("-----------\r"); buf = (char *)(malloc(40*sizeof(char))); sprintf(buf,"Your average response time: %.3g",avg); sendString(buf); sendChar('\r'); free(buf); tries=0; go = 0; } } return 0; } char isCharAvailable() { // Does the RX0 bit of the USART Status and Control Register // indicate a char has been received? if ( (UCSR0A & (0x80)) ) return 1; else return 0; } char receiveChar() { // Return the char in the UDR0 register return UDR0; } void sendChar(char data) { int i = 0; // To send data with the USART put the data in the USART data register UDR0 = data; // Check to see if the global interrupts are enabled if(SREG & 0x80) { // Wait until the byte is sent or we count out while ( !(UCSR0A&0x40) && (i<10000) ) { i++; } } else // Wait until the byte is sent while( !(UCSR0A&0x40) ); // Clear the TXCflag UCSR0A=UCSR0A|0x40; } void sendString(char s[]) { int i = 0; while(i < 64) // don't get stuck if it is a bad string { if( s[i] == '\0' ) break; // quit on string terminator sendChar(s[i++]); } } void USARTinit() { // Increase the oscillator to 2 Mhz for the 19200 baudrate: CLKPR = (1<>8); UBRR0L = 12;//(unsigned char)baudrate; // Enable 2x speed change UCSR0A = (1< 6250) { OSCCAL--; // the internRC oscillator runs to fast, decrease the OSCCAL } else if (temp < 6120) { OSCCAL++; // the internRC oscillator runs to slow, increase the OSCCAL } else calibrate = 1;//TRUE; // the interRC is correct TCCR1B = (1<