Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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 //changes delay
Dim dip4 As PORTB.0 //changes # of tones
//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
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=$70 //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
Seed=$12345678 //seed random number
While(TRUE) //repeat forever
If PORTB.2=1 Then //if PIR activated portRB2 is high
If PORTB.1=1 Then speed=200
Else speed = 25
For i = 1 To 200 //play 20 tones
Tone=Rand(5) //each tone is random frequency
DelayMS(speed) //and for 1.00 seconds
Next //end for loop
[COLOR="Red"]Else //otherwise[/COLOR]
Tone=5 //silence
i=Rand(255) //make rand more random
EndIf //end if condition
Wend //end of while loop
[COLOR="Blue"]While[/COLOR](TRUE) //repeat forever
[COLOR="Red"]If[/COLOR] PORTB.0=0 Then //if button 1 pressed
[COLOR="Lime"]For[/COLOR] i = 1 To 200 //play 10 tones
Tone=Rand(5) //each tone is random frequency
DelayMS(250) //and for 0.25 seconds
[COLOR="lime"]Next[/COLOR] //end for loop
[COLOR="red"]Else[/COLOR] //otherwise
Tone=5 //silence
i=Rand(255) //make rand more random
[COLOR="red"]EndIf [/COLOR] //end if condition
[COLOR="blue"]Wend[/COLOR] //end of while loop
and
tags
did I post it right?
[code]While(TRUE) //repeat forever
If PORTB.2=1 Then //if PIR activated portRB2 is high
PORTB.1=1 speed=200
PORTB.0=1 TUNE=50
For i = 1 To 200 //play 20 tones
Tone=Rand(5) //each tone is random frequency
DelayMS(speed) //and for 1.00 seconds
Next //end for loop
While True
If Then //tab end 1 time
Else //tab end 3 times
EndIf // same tab as if so you can see you you closed it
wend
// thats easy to see what you did and make sure you put the right tags
// this is hard to see what you did
While True
If Then
Else
EndIf
If Then
ElseIf
Wend
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 //changes delay
DIM dip4 AS PORTB.0 //changes # of tones
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
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=$70 //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
Seed=$12345678 //seed random number
WHILE(TRUE) //repeat forever
IF PORTB.2=1 THEN //if PIR activated portRB2 is high
PORTB.1=1 speed=200
PORTB.0=1 TUNE=50
FOR i = 1 TO 200 //play 20 tones
Tone=Rand(5) //each tone is random frequency
DELAYMS(speed) //and for 1.00 seconds
NEXT //end for loop
ELSE //otherwise
Tone=5 //silence
i=Rand(255) //make rand more random
ENDIF //end if condition
WEND //end of while loop
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 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=0 //PIR 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=$70 //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
IF PORTB.2=1 THEN //if PIR activated portRB2 is high
PORTB.1=1 speed=200
PORTB.0=1 TUNE=50
FOR i = 1 TO 200 //play 20 tones
Tone=Rand(5) //each tone is random frequency
DELAYMS(speed) //and for 1.00 seconds
NEXT //end for loop
ELSE //otherwise
Tone=5 //silence
i=Rand(255) //make rand more random
ENDIF //end if condition
WEND //end of while loop
If you're programming a pic on an external board then all dip switches should be OFF on the Junebug.
Mike.