1. 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.
    Dismiss Notice

Junebug help??

Discussion in 'Microcontrollers' started by MrDEB, Apr 8, 2009.

  1. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    using LEDs in breadboard

    programming the PIC using the Junebug
    with following code timer 2 flashes (duh at 5ms)
    the timer 1 comes on for about 5 seconds then off for about same THEN a short burst then it comes on for 5 seconds again.
    going to add the other 3 timers, maybe have something to do with timer handler?
    the 2000ms is not 20000.
    I am just changing values to do some trial and error (maybe learn something?? learn by doing)
    Code (text):

    // 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,
       Timer2 = 1,
       Timer3 = 2,
       Timer4 = 3
         
    // OnTimer1 event...
    Event OnTimer1()
       Toggle(PORTB.0)//pin 8
    End Event
    Event OnTimer2()
       Toggle(PORTB.1)//pin 9
    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 = 5        // 50ms
    Timer.Items(Timer2).OnTimer = OnTimer2
      // timer event handler
    Timer.Items(Timer4).Interval = 200      // 2000ms, no event handler

    // enable the timers...
    Timer.Items(Timer1).Enabled = true
    Timer.Items(Timer2).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.0)//pin 8
          Toggle(PORTB.1)//pin 9
          Timer.Items(Timer4).Enabled = true
       EndIf
       Toggle(PORTB.1)//pin 8  
     
    Wend
     
     
  2. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    event handler has lots to do w/ this code

    I prg the PIC w/ orginal code that be80be posted, changed the B7 to 5ms
    no short bursts of LEDs so shortning the ONTIME values affects everything?
    pin 8 = 10-15 seconds on
    pin 9 = 60 seconds on
    pin 17 = 2 minutes on
    timer 4 =2000 havn't figured out this on as no LED (port) to connect to.
    Code (text):

    }
    // 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,
       Timer2 = 1,
       Timer3 = 2,
       Timer4 = 3
         
    // OnTimer1 event...
    Event OnTimer1()
       Toggle(PORTB.0)//pin 8
    End Event

    // OnTimer2 event...
    Event OnTimer2()
       Toggle(PORTB.1)//pin9
    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 = 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)//pin 13
       DelayMS(5)
       Low(PORTB.7)//pin 13
       DelayMS(5)
    Wend
     
    boy this is kinda fun doing detective work on a PIC code.
    I just need to learn more syntax and how to code properly.
    lots of thanks to all that have submitted suggestions on this long post but there is lots of knowledge in it.
     
  3. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    I think I figured it out

    ONTIMER# is basically the same as DELAYMS(#) but is more reliable and accurate??
    been changing vlues etc. Lots of fun discovering what a slight change can do.
    going to try and eliminate all the timers except #1 and 4
    apperaently #4 has something to do with event handler??
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn

    this is a polled timer, not event driven - check to see
    // if it has timeout...
     
    Last edited: Jun 9, 2009
  6. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    timer 4 goes for a long time

    I inserted a TOGGLE portb.3 then DELAYMS(2) then TOGGLE portB3 again.
    the LED flashes brieftly when timer4 gets going
    I wanted to see when it started and stoped.
    and yes I get 15-18 minutes when timer4 is set for 2000
    now I am trying to mix the random sound code with the timer code but I get the error can't load timermoduel4 bas file.
    I put the INCLUDE just like the timer code did.
    file name to long or ??
    see my junebug/icd
    I posted all the code from both programs.
     
  7. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    conditional program jump?

    problem = disable the PIR durring daylight and enable the timer
    plan is if doable = have the timer and PIR routines alternate due to the state of RB#(a phototransistor to change the state of this pin thus implementing a program jump (daylight = disable the PIR routine
    night time = disable the timer routine
    my problem is trying to write a short routine to make the jump.
    my thoughts are an IF THEN statement or a WHILE TRUE
    not sure how to go about it.
    trying to use Junebug to test but that charliexplexing throws a towel in the mix.
    any suggestions
     
  8. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    trying to code a routine skip?

    I am trying to dechipfer how to do a "conditional" routine on the Junebug where I flash LEDs but when the button B0 is pressed then change the flash rate.
    end result is to insert into my CRITTER RIDDER code so durring daylight the timer is enabled and the PIR is disabled(a photo transistor to take another port LOW thus disabliing the PIR and enablining the timer
    code I am playing with is for the Junebug just to try out.
    Code (text):

    Device = 18f1320
    Clock = 8
    Config OSC = INTIO2, WDT = OFF, LVP = OFF
    OSCCON = $62
    TRISA=%10111110         //A0 & 6 output
    PORTA=1
    porta.6=0
    portb.0=1
    while(true)
    if portb.0=0 //press button same as phototrans going low
       
       then
       porta.0=0
       delayms(60)
       porta.6=0
       delayms(60)
       
        //if portb.0=0 then false
        //if not portb.0=0 then
     
        endif
    If portb.0 = 1 then

    endif
    Toggle(PORTA.0)
    delayms(20)
     toggle(porta.6)
     delayms(200)

        //Toggle(PORTA.6)
          //  delayms=(200)
            //Toggle(PORTA.6)  
           // delayms=(400)                              //
                             //
    wend                  
       
         end
     
    code dosn't work. it skips directly to last code routine instead of running the first routine instead.
    I am missing something??
     
  9. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    You want some thing like this
    Code (text):

    If portB.0 =0 then
     Led0 =1
    elseif portB =1 then
     Led0 = 0
    Endif
     
    just use your ports for Led0 and the portX
     
  10. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    Mr deb take a look at this I no it's not what you want but your code will not run it's missing a lot to make it work this show you how to set the ports so you can read the switches
    Code (text):

    Device = 18f1320
    Clock = 8
    Config OSC = INTIO2, WDT = OFF, LVP = OFF
    Include "Utils.bas"
    OSCCON = $72
    SetAllDigital      //set digital so you can read RB0 or it will always read low
    INTCON2.7=0        // sets wpu on portb
    TRISB =%11111111  // sets all portb to input
    PORTB =%00000000  // set it low
    TRISA =%00000000   // sets porta to output
    PORTA =%00000000   //sets it low                      

    While(true)
        If (PORTB.0 =0)  Then     //test to see if it gos low sw1
        PORTA.0 = 1               //if low led1 and 6 will light
        DelayMS(20)               // debounce delay
        ElseIf (PORTB.0 =1) Then   // test if high
        PORTA.0 = 0                // if high leds will be out
        EndIf
    Wend                  
       
         End
     
     
    Last edited: Jun 11, 2009
  11. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    This is what you posted before

    to read the switches etc.
    and I am pretty sure it works?
    Code (text):

    Device = 18F1320
    Clock = 8 // 8MHz clock
    Config OSC = INTIO2, WDT = OFF, LVP = OFF
    Include "timer module 4.bas"

    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


         


    //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
    PORTA.0=1  
    If PORTB.1=0 Then //if button 1 pressed add PIR=1 here 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
    Else //otherwise
         Tone=5 //silence
         i=Rand(255) //make rand more random
          PORTA.0=0
    EndIf //end if condition
    Wend //end of while loop
     
    Ineed to add the timer module and the PIR disable code that you just posted
    I hope this is all of it??
     
  12. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    disable routine dosn't work??

    I get and END EXPECTED error
    Code (text):

    Device = 18f1320
    Clock = 8
    Config OSC = INTIO2, WDT = OFF, LVP = OFF

    TRISA=%10111110         //A0 & 6 output
    dim but as portb.0
    dim butTRIS as TRISB.0
    end if

    If portB.0 =0 then
     porta.0 =0
    elseif portB =1 then
     
    PORTA=1


    end if
    end  
     
    I added the ports A0 = first led
    A6 = second led
    B0 = button.
    I entered the code exactly as you posted =no worky
    I added the input B0 (I think by putting the trisa??)
     
  13. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    This "end if" looks out of place
    Code (text):
    Device = 18f1320
    Clock = 8
    Config OSC = INTIO2, WDT = OFF, LVP = OFF

    TRISA=%10111110         //A0 & 6 output
    dim but as portb.0
    dim butTRIS as TRISB.0
    [B]end if[/B]

    If portB.0 =0 then
     porta.0 =0
    elseif portB =1 then
      PORTA=1
    end if

    end
     
     
  14. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    That's because
    Code (text):

     Endif       // is right
    End if      //is wrong  
     
    Code (text):

     Device = 18f1320
    Clock = 8
    Config OSC = INTIO2, WDT = OFF, LVP = OFF

    TRISA=%10111110         //A0 & 6 output
    dim but as portb.0
    dim butTRIS as TRISB.0
    [COLOR="Red"]end if   // don't need this will not work[/COLOR]

    If portB.0 =0 then
     porta.0 =0
    elseif portB =1 then
      PORTA=1
    [COLOR="Red"]end if // this should be endif not end if[/COLOR]

    end
     
     
    Last edited: Jun 11, 2009
  15. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    no worky guys??

    get END EXPECTED error
     

    Attached Files:

  16. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    lol why is the First END there? remove it completely
    This looks more of what you are trying to do :D
    Code (text):

    Device = 18f1320
    Clock = 8
    Config OSC = INTIO2, WDT = OFF, LVP = OFF

    dim but as portb.0      //Define but as PORTB bit 0
    dim butTRIS as TRISB.0  //Define butTRIS as TRISB Bit 0

    OSCCON = $72            // 8 MHz clock

    TRISA=%10111110         //A0 & 6 output
    butTRIS = 1             //Set Button as Input

    If but = 0 then         //If but is 0 then
        porta.0 =0          //PORTA Bit 0 = 0
    elseif but = 1 then     //Else if but = 1 then
        porta.0=1           //PORTA Bit 0 = 1
    endif                   //end of if statement
     
    NOTE: I dont have Swordfish installed so cant test it :(
     
    Last edited: Jun 12, 2009
  17. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    NO it don't work?

    haven't a clue why as I am using the Junebug to test.
    maybe the charlieplexing is messing the code up??
     
  18. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,414
    Likes:
    23
    heres a thought = pullup resistors

    there are no pullup resistors on the pushbuttons on the junebug.
    this could be the problem??
     
  19. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
     
  20. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    BE posted the code that will fix the problem.

    There are pullup resistors inside the PIC for that port. BE's code enables them.
     
  21. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    You need to set your ports like this
    Code (text):

    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 but = 0 then        // checks button 1 on the junebug to see if low
        led =0                 // Turns off led
        delayMS(10)       // small debounce delay
    elseif but = 1 then  // checks button 1 on the junebug to see if high
       led =1                 // Turns on led
       delayMS(10)       // small debounce delay
    endif                     // closes your IF THEN statements
    wend

    END
     
     
    Last edited: Jun 13, 2009

Share This Page