[COLOR="Red"]// your main code would look like this
Device = 18F1320
Clock = 8 // 8MHz clock
Config OSC = INTIO2, WDT = OFF, LVP = OFF
Dim day As Byte
Dim nite As Byte
Dim light As Byte
Dim checkPIR As Byte
Dim PlaySound As Byte
While true
If day = light Then PlaySound
ElseIf day = nite Then checkPIR
EndIf
If checkPIR = true Then PlaySound
EndIf
//Wend
end
// then just make three functions One for day and night
// One for PlaySound
// One for checkPIR
// PLAY SOUND ROUTINE
// Device = 18F1320
//Clock = 8 // 8MHz clock
//Config OSC = INTIO2, WDT = OFF, LVP = OFF
while Playsound then
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 PORTB.1 // turns on amp power
Dim AmpTris As TRISB.1//turns on amp pin 9
Dim Speed As Word
Dim dip1 As PORTA.0
Dim dip2 As PORTA.1
Dim dip3 As PORTA.2
Dim dip4 As PORTA.3
Dim dipRD As PORTB.0
//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 digital
TRISB.0=0 //make output
PORTB.0=1 //makes pin 8 high for dip switches
T1CON = %10000001 //pre=1
T1CON = %10000001 //pre=1
Tone=5 //no sound please
TMR1IE=1 //enable timer 1 interrupt
TRISA=%10111110 //A0 & 6 output
PORTA.7=1
While (true)
Toggle(PORTA.0)
DelayMS(200)
Wend
End
Enable(MyInt) //set interrupt going
SpeakerTris=0 //Setup Port
Seed=$12345678 //seed random number
TRISA =%00001111 //sets your inputs on porta
While(TRUE) //repeat forever
If dip1=1 Then //dip1 pin 1
Speed=25
EndIf
If dip2=1 Then // dip 2 pin 2
Speed=50
EndIf
If dip3=1 Then //dip3 pin 6
Speed=75
EndIf
If dip4=1 Then //dip4 pin
Speed=Rand(10)*15+15 //Speed = 25 to 250 random select speed
EndIf
If PORTB.1=0 Then PlaySound //if night then PIR is enabled
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
wend
//CHECK FOR DAYLIGHT OR DARK
Device = 18f1320
Clock = 8
Config OSC = INTIO2, WDT = OFF, LVP = OFF
Include "Utils.bas"
Dim but As PORTB.0
Dim led As PORTA.0
SetAllDigital //set digital so you can read RB0 or it will always read low
TRISA =%00000000 // if your using the junebug This is fool proof to get a led to lite
PORTA =%00000000 // Set's it all low
TRISB =%00000001 // Set's inputs and outputs
PORTB =%00000000 // Set's it all low
INTCON2.7=0 // sets wpu on portb turns on pullups on PORTB
OSCCON = $72 // 8 MHz clock
While true // this loops it so it keeps checking
If day = nite Then checkPIR // enables PIR
DelayMS(10) // small debounce delay
ElseIf day = light Then Timer.Initialize // triggers on timer
led =1 // Turns on led
DelayMS(10) // small debounce delay
EndIf // closes your IF THEN statements
Wend
End
//TIMER ROUTINE
// 18F1320@ 8MHz - they are just used here for clarity...
//Device = 18F1320
//Clock = 8
Include "ISRTimer.bas"
// constant ID to 4 * 16 bit timers...
Const
Timer1 = 0,
Timer4 = 3
// OnTimer1 event...
Event OnTimer1()
Toggle(PORTB.2)//pin 8
End Event
// activate the timer module...
//Timer.Initialize
// initialise the timers - refresh every 1000Hz (1ms)...
Timer.Items(Timer1).Interval = 50 // 50ms
Timer.Items(Timer1).OnTimer = OnTimer1 // timer event handler
// timer event handler
Timer.Items(Timer4).Interval = 200 // 2000ms, no event handler
// enable the timers...
Timer.Items(Timer1).Enabled = true
Timer.Items(Timer4).Enabled = true
// start processing all timers...
Timer.Start
// main program loop...
While true
// this is a polled timer, not event driven - check to see
// if it has timeout...
If day = light Then Timer.Items(Timer4).Enabled
Toggle(PORTB.2)//pin 17 trigger playsound
DelayMS(10) //change state
Toggle(PORTB.2)//pin 17
Timer.Items(Timer4).Enabled = true
EndIf
Wend// your main code would look like this
Device = 18F1320
Clock = 8 // 8MHz clock
Config OSC = INTIO2, WDT = OFF, LVP = OFF
Dim day As Byte
Dim nite As Byte
Dim light As Byte
Dim checkPIR As Byte
Dim PlaySound As Byte
While true
If day = light Then PlaySound
ElseIf day = nite Then checkPIR
EndIf
If checkPIR = true Then PlaySound
EndIf
//Wend
end
// then just make three functions One for day and night
// One for PlaySound
// One for checkPIR
// PLAY SOUND ROUTINE
// Device = 18F1320
//Clock = 8 // 8MHz clock
//Config OSC = INTIO2, WDT = OFF, LVP = OFF
while Playsound then
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 PORTB.1 // turns on amp power
Dim AmpTris As TRISB.1//turns on amp pin 9
Dim Speed As Word
Dim dip1 As PORTA.0
Dim dip2 As PORTA.1
Dim dip3 As PORTA.2
Dim dip4 As PORTA.3
Dim dipRD As PORTB.0
//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[/COLOR]
NOT_RBPU=0 //WPUs on port B
ADCON1=$7f //all digital
TRISB.0=0 //make output
PORTB.0=1 //makes pin 8 high for dip switches
T1CON = %10000001 //pre=1
T1CON = %10000001 //pre=1
Tone=5 //no sound please
TMR1IE=1 //enable timer 1 interrupt
TRISA=%10111110 //A0 & 6 output
PORTA.7=1
While (true)
Toggle(PORTA.0)
DelayMS(200)
Wend
End
Enable(MyInt) //set interrupt going
SpeakerTris=0 //Setup Port
Seed=$12345678 //seed random number
TRISA =%00001111 //sets your inputs on porta
While(TRUE) //repeat forever
If dip1=1 Then //dip1 pin 1
Speed=25
EndIf
If dip2=1 Then // dip 2 pin 2
Speed=50
EndIf
If dip3=1 Then //dip3 pin 6
Speed=75
EndIf
If dip4=1 Then //dip4 pin
Speed=Rand(10)*15+15 //Speed = 25 to 250 random select speed
EndIf
If PORTB.1=0 Then PlaySound //if night then PIR is enabled
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
wend
//CHECK FOR DAYLIGHT OR DARK
Device = 18f1320
Clock = 8
Config OSC = INTIO2, WDT = OFF, LVP = OFF
Include "Utils.bas"
Dim but As PORTB.0
Dim led As PORTA.0
SetAllDigital //set digital so you can read RB0 or it will always read low
TRISA =%00000000 // if your using the junebug This is fool proof to get a led to lite
PORTA =%00000000 // Set's it all low
TRISB =%00000001 // Set's inputs and outputs
PORTB =%00000000 // Set's it all low
INTCON2.7=0 // sets wpu on portb turns on pullups on PORTB
OSCCON = $72 // 8 MHz clock
While true // this loops it so it keeps checking
If day = nite Then checkPIR // enables PIR
DelayMS(10) // small debounce delay
ElseIf day = light Then Timer.Initialize // triggers on timer
led =1 // Turns on led
DelayMS(10) // small debounce delay
EndIf // closes your IF THEN statements
Wend
End
//TIMER ROUTINE
// 18F1320@ 8MHz - they are just used here for clarity...
//Device = 18F1320
//Clock = 8
Include "ISRTimer.bas"
// constant ID to 4 * 16 bit timers...
Const
Timer1 = 0,
Timer4 = 3
// OnTimer1 event...
Event OnTimer1()
Toggle(PORTB.2)//pin 8
End Event
// activate the timer module...
//Timer.Initialize
// initialise the timers - refresh every 1000Hz (1ms)...
Timer.Items(Timer1).Interval = 50 // 50ms
Timer.Items(Timer1).OnTimer = OnTimer1 // timer event handler
// timer event handler
Timer.Items(Timer4).Interval = 200 // 2000ms, no event handler
// enable the timers...
Timer.Items(Timer1).Enabled = true
Timer.Items(Timer4).Enabled = true
// start processing all timers...
Timer.Start
// main program loop...
While true
// this is a polled timer, not event driven - check to see
// if it has timeout...
If day = light Then Timer.Items(Timer4).Enabled
Toggle(PORTB.2)//pin 17 trigger playsound
DelayMS(10) //change state
Toggle(PORTB.2)//pin 17
Timer.Items(Timer4).Enabled = true
EndIf
Wend
How is that true or false Boolean is just true or falsewas looking at doing a boollean where
time = 1
delayms (200)
time = time + 1
if time = 20000 then end
Device = 18f1320
Clock = 8
Config OSC = INTIO2, WDT = OFF, LVP = OFF
OSCCON = $62
TRISA=%00000000 //A0 & 6 output
dim (x) as boolean
end
PORTB.0=1
PORTB.1=0
x=(1)
While (true)
Toggle(PORTB.0)
DelayMS(20)
IF (X>=200) then FALSE
DELAYMS(20)
X=X+1
endif
WEND
Toggle(PORTB.0)
DelayMS(10)
Toggle (PORTB.1)
DelayMS(30)
Toggle(PORTB.0)
DelayMS(10)
Toggle(PORTB.0)
DelayMS(10)
end
Device = 18F1320
Clock = 8
Config OSC = INTIO2
Config MCLRE = Off
Include "INTOSC8.bas" // set OSO
dim delaycounter as byte
Sub Longdelay()
delaycounter = 0
repeat // 10 sec delay
delayms(1000)
inc (delaycounter)
until delaycounter = 10
End Sub
output (portb.0)
while true
high (portb.0)
Longdelay //10 sec on
low (portb.0)
Longdelay // 10 sec off
wend
Device = 18F1320
Clock = 8
Config OSC = INTIO2
Config MCLRE = Off
Include "INTOSC8.bas" // set OSO
include "Utils.bas"
dim delaycounter as byte
Sub Longdelay()
delaycounter = 0
repeat // 60 sec delay
delayms(1000)
inc (delaycounter)
until delaycounter = 60
End Sub
SetAllDigital
trisa =%00000000
porta =%00000000
output (portA.0)
while true
high (portA.0)
Longdelay //60 sec on
low (portA.0)
Longdelay // 60 sec off
wend
Module IntOSC8
OSCCON = $72
Device = 18F1320
Clock = 8
Config OSC = INTIO2
Config MCLRE = Off
Include "INTOSC8.bas" // set OSO
Include "Utils.bas"
Include "bigdelay.bas"
SetAllDigital
TRISA =%00000000
PORTA =%00000000
Output (PORTA.0)
While true
High (PORTA.0)
longmindelay //3minute on
Low (PORTA.0)
Longdelay // 60 sec off
Wend
Module bigdelay
Dim delaycounter As Byte
Public Sub Longdelay()
delaycounter = 0
Repeat // 1 minute.delay
DelayMS(1000)
Inc (delaycounter)
Until delaycounter = 60
End Sub
Dim mindelaycounter As Byte
Public Sub longmindelay()
mindelaycounter = 0
Repeat // 3 minute.delay
Longdelay
Inc (mindelaycounter)
Until mindelaycounter = 2 //change this to make it longer
End Sub
// 18F1320@ 8MHz - they are just used here for clarity...
Device = 18F1320
Clock = 8
Include "ISRTimer.bas"
include "INTOSC8.bas"
// constant ID to 4 * 16 bit timers...
Const
Timer1 = 0,
Timer4 = 2
// OnTimer1 event...
Event OnTimer1()
Toggle(PORTB.0)//pin 8
DelayMS(10)
End Event
// OnTimer2 event...
Event OnTimer2()
Toggle(PORTB.1)//pin9
DelayMS(10)
End Event
// OnTimer3 event...
Event OnTimer3()
Toggle(PORTB.2)//pin 17
End Event
// activate the timer module...
Timer.Initialize
// initialise the timers - refresh every 1000Hz (1ms)...
Timer.Items(Timer1).Interval = 10 // 50ms
Timer.Items(Timer1).OnTimer = OnTimer1 // timer event handler
//
// timer event handler
Timer.Items(Timer4).Interval = 40 // 2000ms, no event handler
// enable the timers...
Timer.Items(Timer1).Enabled = true
Timer.Items(Timer4).Enabled = true
// start processing all timers...
Timer.Start
// main program loop...
While true
// this is a polled timer, not event driven - check to see
// if it has timeout...
If Not Timer.Items(Timer4).Enabled Then
DelayMS(30)
Toggle(PORTB.3)
Timer.Items(Timer4).Enabled = true
EndIf
// background flash LED...
High(PORTB.2)//pin 13
DelayMS(50)
Low(PORTB.2)//pin 13
DelayMS(50)
Wend
// 18F1320@ 8MHz - they are just used here for clarity...
Device = 18F1320
Clock = 8
include "INTOSC8.bas"
Include "ISRTimer.bas"
// constant ID to 4 * 16 bit timers...
Const
Timer1 = 0,
Timer2 = 1,
Timer3 = 2,
Timer4 = 3
// OnTimer1 event...
Event OnTimer1()
Toggle(PORTB.0)
End Event
// OnTimer2 event...
Event OnTimer2()
Toggle(PORTB.1)
End Event
// OnTimer3 event...
Event OnTimer3()
Toggle(PORTB.2)
End Event
// activate the timer module...
Timer.Initialize
// initialise the timers - refresh every 1000Hz (1ms)...
Timer.Items(Timer1).Interval = 50 // 50ms
Timer.Items(Timer1).OnTimer = OnTimer1 // timer event handler
Timer.Items(Timer2).Interval = 250 // 250ms
Timer.Items(Timer2).OnTimer = OnTimer2 // timer event handler
Timer.Items(Timer3).Interval = 500 // 500ms
Timer.Items(Timer3).OnTimer = OnTimer3 // timer event handler
Timer.Items(Timer4).Interval = 2000 // 2000ms, no event handler
// enable the timers...
Timer.Items(Timer1).Enabled = true
Timer.Items(Timer2).Enabled = true
Timer.Items(Timer3).Enabled = true
Timer.Items(Timer4).Enabled = true
// start processing all timers...
Timer.Start
// main program loop...
While true
// this is a polled timer, not event driven - check to see
// if it has timeout...
If Not Timer.Items(Timer4).Enabled Then
Toggle(PORTB.3)
Timer.Items(Timer4).Enabled = true
EndIf
// background flash LED...
//high(PORTB.7)
//delayms(500)
//low(PORTB.7)
//delayms(500)
Wend
Device = 18f1320
Clock = 8
Config OSC = INTIO2, WDT = OFF, LVP = OFF
OSCCON = $72
TRISA=%00000000 //A0 & 6 output
PORTB.0=1
PORTB.1=1
PORTB.2=1
PORTB.3=1
While (true)
Toggle(PORTB.0)
DelayMS(20)
Toggle (PORTB.3)
DelayMS (50)
Toggle(PORTB.2)
DelayMS(20)
Toggle (PORTB.1)
DelayMS (50)
Wend
you popping a lot. I'm sure you can hurt a pic but it's harder then you think. And if you use the junebug like the pickit2 you can power the target board and if you hook it up wrongI thought I poped something??
don'tknow what I did but got the two to communicate again.
// 18F1320@ 8MHz - they are just used here for clarity...
Device = 18F1320
Clock = 8
include "IntOSC8.bas" // you need this to set the osc.
Include "ISRTimer.bas"
// constant ID to 4 * 16 bit timers...
Const
Timer1 = 0,
//Timer2 = 1,
//Timer3 = 2,
Timer4 = 3
// OnTimer1 event...
Event OnTimer1()
Toggle(PORTB.0)
End Event
portB.1=1// TEST of progression
delayms(50)
portb.1=0
// OnTimer2 event...
//Event OnTimer2()
// Toggle(PORTB.1)
//End Event
// OnTimer3 event...
//Event OnTimer3()
// Toggle(PORTB.2)
//End Event
// activate the timer module...
Timer.Initialize
// initialise the timers - refresh every 1000Hz (1ms)...
Timer.Items(Timer1).Interval = 200 // 50ms
Timer.Items(Timer1).OnTimer = OnTimer1 // timer event handler
//Timer.Items(Timer2).Interval = 50 // 250ms
//Timer.Items(Timer2).OnTimer = OnTimer2 // timer event handler
//Timer.Items(Timer3).Interval = 50 // 500ms
//Timer.Items(Timer3).OnTimer = OnTimer3 // timer event handler
Timer.Items(Timer4).Interval = 200 // 2000ms, no event handler
portB.1=1// TEST of progression
delayms(50)
portb.1=0
// enable the timers...
Timer.Items(Timer1).Enabled = true
//Timer.Items(Timer2).Enabled = true
//Timer.Items(Timer3).Enabled = true
Timer.Items(Timer4).Enabled = true
// start processing all timers...
Timer.Start
portB.1=1// TEST of progression
delayms(50)
portb.1=0
// main program loop...
While true
// this is a polled timer, not event driven - check to see
// if it has timeout...
If Not Timer.Items(Timer4).Enabled Then
Toggle(PORTB.3)
Timer.Items(Timer4).Enabled = true
EndIf
// background flash LED...
high(PORTB.2)
delayms(500)
low(PORTB.2)
delayms(500)
Wend
Module delaytimer
Dim delaycounter As Byte
Public Sub Longdelay()
delaycounter = 0
Repeat // 1 minute.delay
DelayMS(10) // 1000 = 1 minute
Inc (delaycounter)
Until delaycounter = 60
End Sub
Dim mindelaycounter As Byte
Public Sub longmindelay()
mindelaycounter = 0
Repeat // 3 minute.delay
Longdelay
Inc (mindelaycounter)
Until mindelaycounter = 12000 //change this to make it longer
End Sub
Dim delaycounter As Byte
Public Sub Longdelay()
delaycounter = 0
Repeat // 1 minute.delay
DelayMS(1000)
Inc (delaycounter)
Until delaycounter = 60 //set for desired seconds
End Sub
Dim mindelaycounter As Byte
Public Sub longmindelay()
mindelaycounter = 0
Repeat // 3 minute.delay
Longdelay
Inc (mindelaycounter)
Until mindelaycounter = 20 //change this to make it longer
End Sub
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?