Thanks to Pommie, Blueroom and be80be over at electrotech I have gotton this far. Please note that this programming is all new to me. Did some on the Comodore computers years ago. I want to add to SPEED a value of 50 per dip switch setting=all on = 175 where SPEED starts at 25
50 x 3 + 25=175 if I did my math right. have ra0,rb2 &rb3 already in use. Rb1 is presently working to change speed to 25
Code: Select all
DEVICE = 18F1320
CLOCK = 8 // 8MHz clock
CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF
DIM NOT_RBPU AS INTCON2.7
DIM TMR1IE AS PIE1.0
DIM TMR1IF AS PIR1.0
DIM TMR1 AS TMR1L.AsWord
DIM Speaker AS PORTB.3
DIM SpeakerTris AS TRISB.3
DIM Amp AS PORTA.1 // turns on amp power
DIM AmpTris AS TRISA.1
DIM dip3 AS PORTB.1
DIM dip3TRIS AS TRISB.1 //changes delay
DIM dip4 AS PORTB.0 //changes # of tones
DIM dip4TRIS AS TRISB.0
DIM PIR AS PORTB.2 // turns on PIR
DIM PIRTris AS TRISB.2
DIM speed AS BYTE
DIM TUNE AS BYTE
//global variables
DIM Seed AS LONGWORD, Tone AS BYTE
DIM i AS BYTE
//half period delays = clock speed divided by 2*frequency
CONST Tones(18) AS WORD = (2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,
2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000)
//interrupt routine
INTERRUPT MyInt()
T1CON.0=0 //stop timer
TMR1=-Tones(Tone) //reset period
T1CON.0=1 //restart timer
IF Tone=5 THEN //if silence
Speaker=0 //speaker off
Amp=0 // amp off
PIR=1 //PIR off
ELSE //otherwise
TOGGLE(Speaker) //make sound
toggle(amp)//turn amp on
ENDIF
TMR1IF=0 //clear interrupt flag
END INTERRUPT
FUNCTION Rand(Range AS BYTE) AS BYTE
DIM i AS BYTE, feed AS BIT, temp AS WORD
FOR i = 0 TO 7 //generate 8 bits
Feed = Seed.30 XOR Seed.27 //make new bit
Seed=Seed*2+Feed //shift seed left and add new bit
NEXT
Temp=(Seed AND 255) * Range //change Rand from 0 to 255
Rand = Temp/256 //to 0 to (Range-1)
END FUNCTION
//main code starts here
OSCCON = $72 //select 8MHz internal clock
NOT_RBPU=0 //WPUs on port B
ADCON1=$71 //all digital
T1CON = %10000001 //pre=1
Tone=5 //no sound please
TMR1IE=1 //enable timer 1 interrupt
ENABLE(MyInt) //set interrupt going
SpeakerTris=0 //Setup Port
PIRTris=1
Seed=$12345678 //seed random number
WHILE(TRUE) //repeat forever
WHILE(TRUE) //repeat forever
if PORTB.1=0 then // dip switch setting
speed=25
else
speed=200
endif
WHILE(TRUE) //repeat forever
if PORTb.0=0 then
speed=25
portb.1=0 then speed+50
else
speed=25
endif
IF PORTB.2=0 THEN //if PIR activated portRB2 is low
FOR i = 1 TO 200 //play 20 tones
Tone=Rand(5) //each tone is random frequency
DELAYMS(speed) //and for 25-200 seconds here is where I want to change
NEXT //end for loop
ELSE //otherwise
Tone=5 //silence
i=Rand(255) //make rand more random
ENDIF //end if condition
Tone=5 //silence
i=Rand(255) //make rand more random
WEND //end of while loop