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. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,795
    Likes:
    134
    Location:
    morristown,tn
    ONLINE
    Thats one easy way to make a sine wave Thanks for the post.
    I can put that to uses .
    I tell you what's hard about learning to code a pic is there are many ways to do the same thing but a lot don't mix with what you started.
    The best is to stay with one way till you get it down pat.
    And for sure read the swordfish help till you know whats going on it's the same as there manual you can down load
     
    Last edited: Apr 14, 2009
  2. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    Another method is PWM sine wave generation. Google "magic sine waves"
     
  3. Sceadwian

    Sceadwian Banned

    Joined:
    Oct 27, 2006
    Messages:
    14,047
    Likes:
    141
    Location:
    Rochester, US
  4. dave

    Dave New Member

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


     
  5. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22

    now we are looking at a converter??

    I just want a simple 2 - 6 khz sound that goes up and down / back and forth
    I just thought the resistor ladder looked easy?
    just getting into this PIC stuff.
    trying to figure out a timer
    still need to salvage a speaker from old computer case
    getting farther behind
    want something simple, short program code etc.
    I would like to end up with just a PIC (programed with an interupt to detect the PIR and emit the sound for 5-10 seconds) , maybe a few resistors, a PIR and a small speaker.
     
  6. Sceadwian

    Sceadwian Banned

    Joined:
    Oct 27, 2006
    Messages:
    14,047
    Likes:
    141
    Location:
    Rochester, US
    Single tones are easy with a timer, sweeps are not. Would you be satisfied with simply alternating between two tones? Because that's a whole lot simpler.
     
  7. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    Do you need a sine wave?

    You can make a lot of sounds just buy turning a speaker on and off. A cap can be used to AC couple the speaker and take the edges off the output.

    3v0
     
  8. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    I want to cycle from 2khz to 6khz

    preferably 2, 6, 3, 5, 4, 4, 5, 3, 6, 2
    then repeat. total time on = 5-10 seconds
    then resets. trigger is a PIR module.
    be nice to randomly output the different sounds so it is never the same twice.
    I am new to this swordfish basic.
    can someone recommend a good book to read about?? n learn.
     
  9. Sceadwian

    Sceadwian Banned

    Joined:
    Oct 27, 2006
    Messages:
    14,047
    Likes:
    141
    Location:
    Rochester, US
    Languages are best learned from taking apart other people's code and seeing how it works. Just find every piece of swordfish code you can get your hands on, a few of them are likley to be commented, keep re-reading the code until you can't figure it out, then post the code you're getting stuck on and someone should be able to explain it for you. I've never been a big fan of structured learning with programming languages, it tends to teach the bad habbits of the programmer that's doing the teaching, while stifling creativity.
     
  10. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    You can do this on the pic with an array to select the frequency/timing, and a loop inside a loop sort of thing.
    My basic is rusty.
     
  11. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,011
    Likes:
    317
    Location:
    Brisbane Australia
    You may find this useful. It is a random number generator for the Junebug. It is setup here to flash LED1 with a random number of flashes from 1 to 6 whenever button 1 is pressed.
    Code (text):

    Device = 18F1320
    Clock = 8 // 8MHz clock
    Config OSC = INTIO2, WDT = OFF, LVP = OFF

    Dim NOT_RBPU As INTCON2.7
    Dim TMR1IE As PIE1.0
    Dim TMR1IF As PIR1.0
    Dim INT0IE As INTCON.4
    Dim INT0IF As INTCON.1

    //global variables
    Dim Seed As LongWord
    Dim i as byte

    Function Rand(Range As Byte) As Byte
    Dim i As Byte, feed As Bit, temp As Word
        For i = 0 To 7                      //generate 8 new bits
            Feed = Seed.30 Xor Seed.27      //make new bit
            Seed=Seed*2+Feed                //shift left and add new bit
        Next    
        Temp=(Seed And 255) * Range         //convert from 0-255
        Rand = Temp/256                     //to 0-Range
    End Function

    //main code starts here

    OSCCON = $72            // select 8MHz internal clock
    NOT_RBPU=0              //WPUs on port B
    ADCON1=$70              //all digital
    TRISA=$fe               //Bit 0 output
    PORTA=1                 //and high
    Seed=$12345678          //seed random number
    While true
        if (PORTB.0=0) then             //if button pressed
            for i = 1 to Rand(6)+1      //1 to 6 flashes
                TRISA.6=0               //light LED
                DelayMs(150)            //short delay
                TRISA.6=1               //turn off LED
                DelayMS(150)            //guess
            next
        else
            i=Rand(255)                 //makes more random
        endif
    Wend

    End
     
    It is of course Pseudo Random and produces better results when outside actions select when it is used. Hence the calling of Rand when the button isn't pressed.

    Mike.
     
    Last edited: Apr 14, 2009
  12. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Thanks will definality use

    I saw similar on Swordfish site but ??
    the flashing LED and switch press will help for sure
    just need to do a few changes and add the sounder code be80be did.
     
  13. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,011
    Likes:
    317
    Location:
    Brisbane Australia
    I had a piezo sounder laying around and so I hooked it up to RA3 and Gnd and had a play with Swordfish.

    This will play 10 random notes (2-6kHz) for 0.25 seconds each whenever button 1 is pressed. I must say it sounds awful.
    Code (text):

    Device = 18F1320
    Clock = 8                   // 8MHz clock
    Config OSC = INTIO2, WDT = OFF, LVP = OFF

    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/[COLOR="Red"]12000[/COLOR],2000000/[COLOR="red"]10000[/COLOR],2000000/[COLOR="red"]8000[/COLOR],2000000/[COLOR="red"]6000[/COLOR],2000000/[COLOR="red"]4000[/COLOR],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=$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)             //repeat forever
        If PORTB.0=0 Then       //if button 1 pressed
            For i = 1 To [COLOR="red"]10[/COLOR]     //play 10 tones
                Tone=Rand(5)    //each tone is random frequency
                DelayMS([COLOR="red"]250[/COLOR])    //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 of while loop

    End
     
    The frequencies (doubled), duration and number of notes to play are in red.

    Have fun.

    Mike.
     
    • Like Like x 1
  14. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Thanks Pommie!!

    I was trying to figure out the PWM thing using the PWM table writer but getting lost
    Your post will help tremendously
    was looking at mplab for the osscope section as pictured on the front of the JuneBug assembly manuel, figuring if the scope would tell me the actual frequency output??Ran across once before but how?? it might not even do what I want.
    Using your code plus the random number generator w/ the portb.0 interupt? I think this thing may just become alive.
    going to connect a 2n2222, 10k resistor and 8 ohm speaker to con5 output and give it a whirl.
    just need to salvage speaker from old computer. found a plug for con 5 yesterday.
    THANKS again
     
  15. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    Actually yes the logic tool can be used to determine frequency. You'll need to jumper a wire between RB3 and PGC or PGD.
    You would capture the waveform (just one frequency at a time to avoid confusion) and use the cursors to measure the period. 1/period will give you the frequency.
     
  16. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,795
    Likes:
    134
    Location:
    morristown,tn
    ONLINE
    While wend I was playing with the timer Mike done did it.
    Mike code will run you off.
    I say maybe them deer will not like the sound to.
    Mike I think that's what he want's it to sound awful he trying to run deer off.
    It's a lot of fun making sound with swordfish so many way to do it I made a orgin with a keypad and 10 tones it sounded good but them deer
    would want me to keep playing lol.
     
    Last edited: Apr 15, 2009
  17. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    I need to go purchase

    a small speaker from rat shack
    all the junk computers I have, none have a small speaker.
    did get a 3 conductor plug for the con 5 port(have a 4 cond but the 3 cond. is better)
    lets see if be80be can maybe play a sweet lulaby for them deer--LOL
     
  18. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    the logic tool is?

    in pickit2 or mplab.?
    need to find it again to yry it out.
    meanwhile going to rat shack to find speaker and might as well get the resistor etc.
     
  19. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    PICkit2 software has the logic tool.
     
  20. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,011
    Likes:
    317
    Location:
    Brisbane Australia
    I should point out that the code above uses A3, not B3. You can use any pin you like and just need to change these two lines,
    Code (text):

    Dim Speaker As PORTA.3
    Dim SpeakerTris As TRISA.3
     
    Mike.
     
  21. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    I noticed that after I got to my desk (the iphone is not the ideal platform for code)

    PS really nice bit I code Pommie.
     

Share This Page