//ICC-AVR application builder : 12/10/04 2:14:46 PM // Target : 2313 // Crystal: 10.000Mhz #include #include #define TRUE 1 #define FALSE 0 #define DELAY_LONG 25000 #define DELAY_SHORT 250 #define DELAY_MICRO 3 #define CLIP_SIZE 10 int ammo = 0, shot_delay = FALSE, clip = 0, cliptest = 0, pulse_cnt = 0, pulse_mode = FALSE, refresh = TRUE; void pulse_start(); //calls pulse_end when it's done void pulse_end(); void port_init(void) { PORTB = 0x00; DDRB = 0xFF; PORTD = 0x0b00000111; DDRD = 0x0b00000010; } //TIMER0 initialize - prescale:8 // desired value: 73KHz // actual value: 73.529KHz (0.7%) void timer0_init(void) { TCCR0 = 0x00; //stop timer TCNT0 = 0xEF; //set count } #pragma interrupt_handler timer0_ovf_isr:7 void timer0_ovf_isr(void) { //TIMER0 has overflowed pulse_cnt++; if (pulse_cnt == 4000) { TCCR0 = 0x00; //stop timer pulse_end(); } PORTB ^= BIT(0); TCNT0 = 0xEF; //reload counter value } //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts port_init(); timer0_init(); MCUCR = 0x00; GIMSK = 0x00; TIMSK = 0x02; SEI(); //re-enable interrupts //all peripherals are now initialized } void delay(int delay_type, int length); void play(int pitch, int length); void play_reload(); void play_shot(); void main() { init_devices(); while (1) { if (pulse_mode == FALSE) { if (PIND & BIT(0)) shot_delay = FALSE; if (!(PIND & BIT(0)) && shot_delay == FALSE && ammo > 0) //if D0 is low pulse_start(); if (clip != 1 && cliptest != 1) { cliptest = 1; DDRD &= ~BIT(1); //make D1 input DDRD |= BIT(2); //make D2 output PORTD |= BIT(1); //pull 1up = 1 PORTD &= ~BIT(2); //out = 0 } if (clip != 1 && !(PIND & BIT(1))) { //load clip 1 clip = 1; ammo = CLIP_SIZE; refresh = TRUE; play_reload(); } if (clip != 2 && cliptest != 2) { cliptest = 2; DDRD &= ~BIT(2); //make D2 input DDRD |= BIT(1); //make D1 output PORTD |= BIT(2); //pull 2 up = 1 PORTD &= ~BIT(1); //out = 0 } if (clip != 2 && !(PIND & BIT(2))) { //load clip 2 clip = 2; ammo = CLIP_SIZE; refresh = TRUE; play_reload(); } if (refresh == TRUE) { refresh = FALSE; if (ammo > 2) { PORTB &= ~BIT(5); //red off PORTB |= BIT(4); //green on } else if (ammo > 0) { PORTB |= 0b00110000; //red green on } else { PORTB &= ~BIT(4); //green off PORTB |= BIT(5); //red on } } } } } void pulse_start() { shot_delay = TRUE; ammo--; refresh = TRUE; //muzzle flash 1 on PORTB |= BIT(1); //enable IR flashing pulse_cnt = 0; pulse_mode = TRUE; TCCR0 = 0x02; //start timer } void pulse_end() { //disable IR flashing pulse_mode = FALSE; //sound and muzzle flash 1 off play_shot(); PORTB &= ~BIT(1); //shots two and three play(2,1); PORTB ^= BIT(1); play_shot(); PORTB ^= BIT(1); } void delay(int delay_type, int length) { int i, j; for (i=0; i