Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

coding a GOTO or BRA to??

Status
Not open for further replies.
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
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
the Sound.bas module
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
Set the ports and she should be good to go.
 
Last edited:
Bert,

You definitely dont want a while true ... wend in a function as it will never return.

It should simply be,
Code:
Function checkPIR()As Boolean
    If PORTB.4 =1 Then
        checkPIR = true
    Else 
        checkPIR = false
    EndIf
End Function
or,
Code:
Function checkPIR()As Boolean
    checkPIR = (PORTB.4=1)
End Function

Mike.
 
Thanks Mike I changed it I see what your saying now. I was thinking there is even a better way to do this. Just do the switch checking at start of program and let it just jump over what switches are false and then play the sound. that way it will just keep rolling a long LOL
 
present circuit diagram in png

found a 3 terminal phototransistor for a better digital output but is it really needed?
need to conserve battery as well. can a SLEEP be implemented into code but cycling every 20 minutes??
 

Attachments

  • Capture6-16-2009-6.41.32 AM.png
    Capture6-16-2009-6.41.32 AM.png
    135.6 KB · Views: 274
  • Capture6-16-2009-6.49.21 AM.png
    Capture6-16-2009-6.49.21 AM.png
    99.7 KB · Views: 271
Can a SLEEP be implemented into code but cycling every 20 minutes??
Yes I was wanting to see what switches it had with the phototransistor and all .
You may need two sound Functions so day it will play sound for 1 min and sleep 20 maybe. But in the assembly view it looked like it let me use the Sound module two times. Maybe Swordfish ideal of
you cannot make recursive subroutine or functions calls.
Not the same as mine LOL.
 
Last edited:
same or two seperate sound modules?

trying to decipher how to complie all the modules etc and see two different sound codes.
one has the dip switches and second one does not.
you mentioned that maybe I have to have two different codes?
one for day and one for night?
using a perf board and LEDs I have been programming a PIC and expereminting with the code to maybe learn what is going on.
learn by doing is my thing
 
What i was thinking is make a Sound module have two functions one for day and one for night. You can reuse a module. When you build it adds the code in so it still all inline. See what you need is to start at day if it's not day it will go to night that way your loop will keep checking "It's all about flow.
 
what is include "IntOSC8"

I think its a mistypo or ??
we have the sound module, timer module and need to include the PIR/daylight module I think?
this could be included (should be ) with the timer??
 
It's a module that set the osc so it will start full speed if you use module some time your osc will not start at full speed till all the module load and that can some times take a long time. if it not up to speed.
 
Last edited:
where is this module?

I looked in all of swordfish but never saw this file. I get an invalid file error.
I got the timer module as a module I think .
the error of invalid file for ocs8 comes up first.
had (a bad treminal) to fix my ribon cable.
the pickkit2 couldn't find it.
the ground pin didn't get soldered in the plug??
could have sworn I soldered all them pins.
I wonder if it would be easier to debug code using the con3 port on the junebug?(8 pin terminal) then just prg the junebug with desired code .
get it working then prg an off board chip.
presently I have a perf board w/ the 5 pin header w/ ribbon cable going to the junebug.have leds connected to pins 8,9,18,17.
should have bi colored leds so I could tell if a port is HIGH or LOW.
maybe order some on my next order.
maybe design a board so I can see what each port is doing.
the charlieplexing = I don't like. can't see what each indivual port is doing.
My next project that I am contemplating is a simple led candle flickering.
I have seen one where it just turns on and off 4 leds. I am thinking PWM.
but first this project.
 
coding a subroutine??

here is most of the code to date but I get an error trying to institute the routines like Playsound
how to do a subroutine. as I need to call Playsound, checkPIR
I tried a WHILE Playsound, IF Playsound THEN, FUNCTION etc??
Code:
// 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
 
I posted you some thing to go by even if your code runs it would take 10 to 20 min before the osc starts up your code flow is crazy
Every thing in red has to happen before it get's to the osc setting.
Code:
[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
evey thing in red has to happen betfore it get's to the osc setting.
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top