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// speaker output port pin 18
dim SpeakerTris as TRISB.3
//Dim PIR As PORTB.2 // turns on PIR
//Dim PIRTris As TRISB.2
dim speed 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
else //otherwise
toggle(Speaker) //make sound
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=$7f //all digita
TRISB.0=0 //make output
PORTB.0=1 //and high
T1CON = %10000001 //pre=1
Tone=5 //no sound please
TMR1IE=1 //enable timer 1 interrupt
enable(MyInt) //set interrupt going
SpeakerTris=0 //Setup Port
Seed=$12345678 //seed random number
speed = 25 //changes via dip switches
// 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
// delayms(100)
//delayms(200)
// Enable(MyInt) //set interrupt going
// 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
// PORTB.0 = 0//to save power put to sleep
delayms(200)// MY LATEST ATTEMPT
TMR1IF=0 //clear interrupt flag
enable(MyInt) //set interrupt going
end //end of while loop