#include <90s8515.h> #include #include #include // sprintf #include // delay_ms #define t0 20 #define t1 1 #define t2 5000 #define t3 1 #define t4 1 #define LCDwidth 16 //characters /* Use an 1x16 alphanumeric LCD connected to PORTC as follows: [LCD pin] [8515] 1 GND - GND 2 +5V - Vcc 3 VLC 10k trimpot wiper (trimpot ends go to +Vcc and GND) 4 RS - PC0 5 RD - PC1 6 EN - PC2 11 D4 - PC4 12 D5 - PC5 13 D6 - PC6 14 D7 - PC7 */ #asm .equ __lcd_port=0x15 #endasm #include // LCD driver routines void checkuhit(void); void checkohit(void); void transmithit(void); void transmiter(void); void reset(void); void clearl2(void); void noise(void); void task2(void); int count, time4, noisel, norec, on; //a number to display on the LCD int uhit, ohit, time0, reload, time1, counter, callreset, time2, run, transmit, tbit, time3, bitcount, bitcorrect; int silence, chirp, syllength, sillength, chirpsilence, note, numsyl; char anipos; //move a character around flash int notes[4] = {1777/2, 1600/2, 1333/2, 2666/2}; //********************************************************** //timer 0 overflow ISR interrupt [TIM0_OVF] void timer0_overflow(void) { //reload to force 1 mSec overflow TCNT0=reload; //Decrement the three times if they are not already zero if (time0>0) --time0; if (time1>0) --time1; if (time2>0) --time2; if (time3>0) --time3; if (time4>0) --time4; } interrupt [TIM1_COMPA] void cmpA_overflow(void) { int n; n=PORTA; if((n & 0x01)==1) PORTA=PORTA & 0xfe; else PORTA=PORTA|0x01; } void main(void) { DDRB=0x00; // PORTB is to check detector (input) and reset button PORTB=0xff; DDRD=0x00; // PORTD is to check radio receiver (input) PORTD=0xfe; // fe DDRA=0xff; // PORTA is an output for transmitter and speaker //set up timer 0 reload=256-62; //value for 1 Msec TCNT0=reload; TIMSK=2; //turn on timer 0 overflow ISR TIMSK=TIMSK | 0x40; //turn on timer 1 compare match interrupt TCCR1B = 0; //disable timer 1 until song starts TCCR0=3; //prescalar to 64 TCNT1 = 0; time0=t0; time1=t1; time2=t2; time3=t3; time4=t4; counter=ohit=uhit=run=transmit=tbit=bitcount=0; callreset=noisel=on=0; norec=1; numsyl=0; note = 0; chirp = 1; syllength = 10; sillength = 1; chirpsilence = 10; // initialize the LCD for 16 char wide lcd_init(LCDwidth); //initialize the display lcd_clear(); //clear the display lcd_gotoxy(8,1); //print next stuff at position 7, line 2 lcd_gotoxy(0,0); //position to upper left on display lcd_putsf("You: 0"); lcd_gotoxy(8,0); lcd_putsf("Opp: 0"); lcd_gotoxy(15,0); lcd_putsf("*"); count=0; anipos = 0; //crank up the ISRs #asm sei #endasm // count display loop while (1) { if (time0==0) checkuhit(); if (time1==0 && norec==1) checkohit(); if(callreset==1) reset(); if (time2==0 && run==1) clearl2(); if(transmit==1 && time3==0) transmithit(); // if(time3==0 && transmit==0) transmiter(); if(time4==0 && noisel==1) noise(); } } void checkuhit(void) { char buffer[5]; time0=t0; if(PINB.0==0) { if(uhit<=10){ lcd_gotoxy(0,1); lcd_putsf("You got hit!!"); time0=250; uhit++; itoa(uhit, buffer); lcd_gotoxy(5,0); lcd_puts(buffer); time2=t2; run=1; }else { lcd_clear(); lcd_gotoxy(0,0); lcd_putsf("Game over!! You"); lcd_gotoxy(0,1); lcd_putsf("lost."); delay_ms(400); callreset=1; run=0; } norec=0; transmithit(); noisel=1; time4=t4; } } void checkohit(void) { char buffer[5]; time1=t1; if(PIND.0==1) bitcorrect++; else bitcorrect=0; if(bitcorrect>=10) { bitcorrect=0; if(ohit<=10){ ohit++; itoa(ohit, buffer); lcd_gotoxy(13,0); lcd_puts(buffer); time1=230; lcd_gotoxy(0,1); lcd_putsf("Nice shot!!"); callreset=0; time2=t2; run=1; lcd_gotoxy(15,0); lcd_putsf(" "); }else { lcd_clear(); lcd_gotoxy(0,0); lcd_putsf("Game over!! You"); lcd_gotoxy(0,1); lcd_putsf("won."); delay_ms(400); callreset=1; run=0; } } } void transmiter(void) { int n; n=PORTA; time3=t3; if(on==0){ //PORTA=0xff; PORTA=0xfe|n; on=1; }else{ PORTA=0x01&n; //PORTA=0x00; on=0; } } void transmithit(void) { int n; n=PORTA; transmit=1; time3=t3; if(tbit==0) // PORTA=0xff; PORTA=0xfe|n; else if(tbit==80) { //PORTA=0x00; PORTA=0x01&n; tbit=-1; transmit=0; norec=1; } tbit++; } void reset(void) { if(PINB.1==0) { uhit=ohit=0; lcd_clear(); lcd_gotoxy(0,0); //position to upper left on display lcd_putsf("You: 0"); lcd_gotoxy(8,0); lcd_putsf("Opp: 0"); callreset=0; noisel=0; TCCR1B=0; }else{ callreset=1; lcd_gotoxy(5,1); noisel=0; TCCR1B=0; lcd_putsf("Play again?"); } } void clearl2(void) { time2=t2; lcd_gotoxy(0,1); lcd_putsf(" "); run=0; noisel=0; TCCR1B=0; } void noise(void) { time4=t4; task2(); } //******************************* //Task 2, create syllable, and chirp void task2(void) { if(numsyl < chirp){ if(silence == 0){ // OCR1A=notes[note]; TCCR1B = 9; time4=syllength; silence=1; numsyl++; }else{ //silence b/w syllables time4=sillength; silence=0; TCCR1B=0; } //numsyl++; } else{ time4 = chirpsilence; numsyl = 0; TCCR1B=0; } }