here is my work in progress. not pretty but still moving things around so I need no sub routines (don't want spagetti)
the tabs etc? what is the perfered layout?
I am attempting to comment and seperate the different routines.
one for sound
one for timer,
one for daylight/disable PIR night time/enable PIR
finding its diffcult to locate duplicate items as I copy n paste alot. lose track where I am at.
wish the left side gutter had a search function I don't know about. It shows some but not all??
Code:
Device = 18F1320
Clock = 8 // 8MHz clock
Config OSC = INTIO2, WDT = OFF, LVP = OFF
Include "Utils.bas"
Include "ISRTimer.bas"
Dim but As PORTB.2 //input for photo diode day disable
Dim led As PORTb.4//triggers PIC inplace of PIR
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
//dip switch settings
Dim dip2 As PORTA.1
Dim dip3 As PORTA.2
Dim dip4 As PORTA.3
Dim dipRD As PORTB.0
Const
Timer1 = 0,
Timer4 = 3
//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
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 but = 0 Then // checksFOR DAYLIGHT CONDITION on pin 17 B2
led =1 // takes pin 10 HIGH
DelayMS(10) // small debounce delay
led=0 // pin 10 low
//+++++++++++++++++
//timer routine daylight condition
//++++++++++++++++++
// constant ID to 4 * 16 bit timers...
//Const
//Timer1 = 0,
// Timer4 = 3
// OnTimer1 event...
Event OnTimer1()
PORTB.2=1//pin 17=
//daylight present triggers timer
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 = 2000 // 2000ms, no event handler
// about 20 minutes// 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
Toggle(PORTB.4)//pin 10 for testing
Toggle(PORTB.1)//pin 9
Timer.Items(Timer4).Enabled = true
EndIf
Toggle(PORTB.1)//pin 8
Wend
//+++++++++++++
// DARK ROUTINE PIR enabled
//++++++++++++++++
If PORTB.1=0 Then //PIr ENABLED = DARK pin 9
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
EndIf // closes your IF THEN statements
Else //otherwise
Tone=5 //silence
i=Rand(255) //make rand more random
EndIf //end if condition
Wend //end of while loop
EndIf // closes your IF THEN statements
Wend
End