be80be
Well-Known Member
Mr Deb I no the port pins don't match what your using If you re post what Circuit your using hopefully as a .png so we can look at all of it I think we can make this work and both learn from it. All this needs is the ports set right and it should work
The main code
the Sound.bas module
Set the ports and she should be good to go.
The main code
Code:
Device = 18F1320
Clock = 8 // 8MHz clock
Config OSC = INTIO2, WDT = OFF, LVP = OFF
Include "Sound.bas"
include "IntOSC8"
Dim day As Boolean
Dim lite As PORTB.3
Function checkPIR()As Boolean
If PORTB.4 =1 Then
checkPIR = true
Else checkPIR = false
EndIf
End Function
While true
If lite=1 Then
day= true
Else day = false
EndIf
If day = true Then
Sound.PlaySound
Else day = false
checkPIR
EndIf
If checkPIR = true Then
Sound.PlaySound
EndIf
Wend
Code:
Module Sound
Dim NOT_RBPU As INTCON2.7
Dim TMR1IE As PIE1.0
Dim TMR1IF As PIR1.0
Dim TMR1 As TMR1L.AsWord
Dim Speaker As PORTA.3
Dim SpeakerTris As TRISA.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(6) As Word = (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
Public Sub PlaySound()
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
If portb.0=0 Then //if button 1 pressed
For i = 1 To 10 //play 10 tones
Tone=Rand(5) //each tone is random frequency
DelayMS(250) //and for 0.25 seconds
Next //end for loop
Else //otherwise
Tone=5 //silence
i=Rand(255) //make rand more random
EndIf //end if condition
Wend
End Sub
End
Last edited: