unsigned char getDebugChar(void) { int i = 0; int dicr; char c; if (!remoteDebugInitialized) { remoteDebugInitialized = 1; debugInit(38400); } /* diable RX int. */ dicr = tx3927_sioptr(0)->dicr; tx3927_sioptr(0)->dicr = 0; do { slow_down(); i++; if (i>TIMEOUT) { break; } } while (tx3927_sioptr(0)->disr & TXx927_SIDISR_UVALID) ; c = tx3927_sioptr(0)->rfifo; /* clear RX int. status */ tx3927_sioptr(0)->disr &= ~TXx927_SIDISR_RDIS; /* enable RX int. */ tx3927_sioptr(0)->dicr = dicr; return c; }
/* fly by attack, flies at the enemy firing and turns around once past and does it again */ void maneuver_fly_by(struct _ship *ship) { float dist; if (!ship->target) { #ifndef NDEBUG printf("maneuver_fly_by() called but ship has no target\n"); #endif return; } /* we do this maneuver by checking distances - if we're a certain distance from the target, turn and face it, if we're within distance, aim and fire, however, if we're within distance but the target is generally behind us (like we just passed it), then we fly out of distance. */ dist = get_distance_sqrd(ship->world_x, ship->world_y, ship->target->offender->world_x, ship->target->offender->world_y); /* 500 real world units but we're using squared distances for speed */ if (dist > 250000.0f) { int facing; /* turn and go for the enemy */ facing = ai_turn_towards(ship, ship->target->offender->world_x, ship->target->offender->world_y); /* slow down to turn */ if (facing) ship->accel++; else slow_down(ship); } else { /* we're within range, find our angle to the target */ float angle; angle = get_angle_to(ship->world_x, ship->world_y, ship->target->offender->world_x, ship->target->offender->world_y); /* whether we're escaping or attacking, we should go fast */ ship->accel++; if (((ship->angle + 60) > angle) && (ship->angle - 60) < angle) { /* we're heading toward the ship, continue to do so and fire if close enough */ /* turn toward the enemy unless we're close (within 175 real units) */ if (dist > 40000) ai_turn_towards(ship, ship->target->offender->world_x, ship->target->offender->world_y); if (dist < 360000) { if (ship->w_slot[0] != -1) fire(ship, 0); if (ship->w_slot[1] != -1) fire(ship, 1); } } else { /* we're within range, but not heading toward the target, so it's most likely we just passed it, in which case keep heading away until we get out of range (so this maneuver can restart) */ int escape_angle = (int)angle + 180; escape_angle %= 360; if (escape_angle > (ship->angle + 15)) turn_ship(ship, 0); else if (escape_angle < (ship->angle - 15)) turn_ship(ship, 1); } } }
void prom_putchar(const unsigned char c) { unsigned char ch; int i = 0; do { ch = com1[SER_CMD]; slow_down(); i++; if (i > TIMEOUT) break; } while (0 == (ch & TX_BUSY)); com1[SER_DATA] = c; }
int putDebugChar(unsigned char c) { int i = 0; if (!remoteDebugInitialized) { remoteDebugInitialized = 1; debugInit(38400); } do { slow_down(); i++; if (i>TIMEOUT) { break; } } while (!(tx3927_sioptr(0)->cisr & TXx927_SICISR_TXALS)); tx3927_sioptr(0)->tfifo = c; return 1; }
int main(void) { // open the Character LCD port char_lcd_dev = alt_up_character_lcd_open_dev ("/dev/LCD"); /* Initialize the character display */ alt_up_character_lcd_init(char_lcd_dev); // Initially writes the start time of timer to lcd write_time_to_buffer(top_row, seconds, minutes, hours, am_pm_mode); hex_write_date(month, day, year); // Initialize the switches int * sw_ptr = (int *) SW_BASE; int sw_values; int oldvalue = 0x00000000; // Masks for individual switches int MASK_17 = 0x00020000; int MASK_16 = 0x00010000; int MASK_1 = 0x00000002; int MASK_0 = 0x00000001; int is_fast = 0; //use to tell other function if sped up, 0 = slow, 1 = fast int clk_modify = 0; //if 0, clock isn't being changed, if 1 clock is being changed int alarm_modify = 0; //if 0 alarm isn't being changed, if 1, alarm is being changed // Initialize the Timers init_timer_0(&tenths); // Tracker to see when the time changes int old_tenths = 0; // Initialize the KEY port init_button_pio(); // continually while(1) { // check the state of the context integer updated by various ISR functions // Act accordingly, which means // Update the switch_values sw_values = *(sw_ptr); //check if sw17 is up and if it is, then speed up the timer if((sw_values & MASK_17) == 0x00020000 && oldvalue == 0x00000000){ speed_up(); oldvalue = sw_values & MASK_17; is_fast = 1; } //check if sw17 is down and if it is then slow down the timer else if ((sw_values & MASK_17) == 0x00000000 && oldvalue == 0x00020000) { slow_down(); oldvalue = sw_values & MASK_17; is_fast = 0; } // Allow user to change the time if SW0 is up if((sw_values & MASK_0) == 0x00000001){ clk_modify = 1; } else{ clk_modify = 0; } // Buttons increment the hours, minutes, and seconds, respectively to Key3, Key2, and Key1 if(clk_modify == 1 && alarm_modify == 0 && alarm == 0){ // Handle if a key was pressed if (edge_capture) { handle_key_press_time(); } } // Allow user to change the alarm if SW1 is up if((sw_values & MASK_1) == 0x00000002){ alarm_modify = 1; alt_up_character_lcd_set_cursor_pos(char_lcd_dev, 0, 1); alt_up_character_lcd_string(char_lcd_dev, bot_row); } else{ alarm_modify = 0; alt_up_character_lcd_set_cursor_pos(char_lcd_dev, 0, 1); alt_up_character_lcd_string(char_lcd_dev, " "); } // Buttons increment the hours, minutes, and seconds, respectively to Key3, Key2, and Key1 if(alarm_modify == 1 && clk_modify == 0 && alarm == 0){ // Handle if a key was pressed if (edge_capture) { handle_key_press_alarm_set(); } } // Check if alarm should go off yet if(hours == alarm_hours && minutes == alarm_minutes && seconds == 0){ alarm = 1; init_timer_1(&half_second); } // While alarm is going off if( alarm == 1 ){ if (half_second % 2) { // Turn hex on hex_on(); } else { // Turn hex off hex_off(); } if( edge_capture) { handle_key_press_alarm(); } } else { stop_timer_1(); } // Check SW16 for "AM_PM" enable or "24" mode enable // If the switch is enabled, then we turn on 24 hour mode // Else we turn on AM / PM Mode // TODO: Optimize so that it doesn't assign something every loop cycle. Maybe we could slim it down if((sw_values & MASK_16) == MASK_16 ) { am_pm_mode = 0; } else { am_pm_mode = 1; } // Update the clock if (tenths != old_tenths) { // Call the util.h function to update the time update_time(top_row, &old_tenths, &tenths, &seconds, &minutes, &hours, &day, &month, &year, am_pm_mode, 0); // Write the updated time to the display alt_up_character_lcd_set_cursor_pos(char_lcd_dev, 0, 0); alt_up_character_lcd_string(char_lcd_dev, top_row); } } return 0; }
int main(void) //beginning of main function { DDRA=asmfunction(); //set PORTA to write DDRB=0xFF; //set PORTB to write DDRC=0xFF; //set PORTC to write DDRD=0b10111011; //set PIND2 and PIND6 to read and the rest of PORTD to write PORTB=PORTB&0b01111111; //set PINB7 to logic zero (turn on green LED) PORTB=PORTB|0b01000000; //set PINB6 to logic one (turn off red LED) PORTD=0b01000100; //enable pull-up resistors of PIND2 and PIND6 lcd_init(); //initialize LCD display lcd_clear(); //clear LCD display lcd_home(); //set cursor of LCD to the first character position /*The next two lines configure T/C0 and T/C2 to set OCR0 and OCR2, respectively on compare match when the T/C subsystem's respective counting register is counting up and to clear OCR0 and OCR2 when counting down. Configured as Fast PWM, Phase-Correct with a prescalar of one.*/ TCCR2=(1<<WGM20)|(1<<COM21)|(1<<COM20)|(1<<CS20); TCCR0=(1<<WGM00)|(1<<COM01)|(1<<COM00)|(1<<CS00); /*The next two lines configure TCNT1 to increment every clock cycle; Configure Enable Input Capture Interrupt to trigger on rising edge; Enable Input Capture Noise Canceller; Locally enable T/C 1 Input Capture Interrupt and T/C 1 Overflow Interrupt.*/ TCCR1B=(1<<ICES1)|(1<<CS10)|(1<<ICNC1); TIMSK=(1<<TICIE1)|(1<<TOIE1); MCUCR=(1<<ISC00)|(1<<ISC01);//configure external interrupt 1 to trigger on rising edge GICR=(1<<INT0); //locally enable external interrupt 1 sei(); //set global interrupt flag unsigned long int button=0; //unsigned long integer (32 bits--sent by IR remote) uint8_t led=0; //state of LED (used to confirm 34-bit transmission by IR remote) int speed=0; //speed of motors at maximum int lights=off; lcd_printf("Waiting"); //print "Waiting" on LCD (wait for button to be pushed on IR remote) PORTB=PORTB|0b00000010; while(1) //infinite loop { if(bit==34) //wait until bit=34--Input Capture ISR called 34 times (1 start bit, 32 data bits, and 1 stop bit sent by IR remote) { button=decipher(remote); //decipher the 32 data bits as either 1 or 0 depending on TCNT1 values; //pass remote as parameter and set button equal to return value bit=0; //reset bit equal to 0 (prepare to receive a new command) if(led==0) //if the state of led is 0, turn off green LED and turn on red LED { //then switch the state of led to 1 PORTB=PORTB&0b10111111; PORTB=PORTB|0b10000000; led++; } else //if the state of led is not 0 (state is 1), turn off red LED and turn on green LED { //then switch the state of led to 0 PORTB=PORTB&0b01111111; PORTB=PORTB|0b01000000; led--; } switch(button) //check the value of button (button that was pushed on remote) { case up_arrow: //if up arrow was pushed, make robot go forward forward(); break; case down_arrow: //if down arrow was pushed, make robot go backward backward(); break; case left_arrow: //if left arrow was pushed, make robot go left left(); break; case right_arrow: //if right arrow was pushed, make robot go right right(); break; case channel_up: //if channel up was pushed, speed robot up (speed is parameter sent to speed_up function); speed equals returned value speed=speed_up(speed); break; case channel_down: //if channel down was pushed, slow robot down (speed is parameter sent to speed_down function); speed equals returned value speed=slow_down(speed); break; case mute: //if mute was pushed, toggle lights from off to on or on to off; pass lights as parameter and set lights equal to returned value lights=toggle_lights(lights); break; default: //if any other button was pushed, stop robot stop(); break; } OCR0=speed; //set the speed of the left motor depending on value of speed OCR2=speed; //set the speed of the right motor depending on value of speed } } }