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,364
    Likes:
    22
    I read the code you posted

    I am pretty sure the one I posted was the one you posted but maybe not??
    will copy n paste the posted code and give it a try.
    now CON 5 is marked RB3, U5V, GRD
    am assuming the RB3 goes to the speaker and grd goes to other side of speaker??
     
  2. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    that will work but try the code i just posted it works better
     
  3. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    I want to see if I can include

    the frequency cals from this
    Swordfish Wiki | SwordfishUser / PWM2 browse
    we conversed about this but never got into details
    I want 2-6khz back n forth but the sequence changes as per Random Number Generator
    use the RNG from the swordfish site, mix in the pwm function and set the RNG output to change the frequency sequence.In other words the RNG variable changes the frequency variable to output different frequencies.
    never the same sound basically.
    shouldn't be too hard--lol
     
  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

    I took the RNGcode + sound code

    combined both, changed the magicA & B variables as well as the magic C maxium value
    I then compiled in swordfish but need a speaker to test using little Juney bug
    any suggestions
    according to swordfish compiler it is ok.
    the RNG is supposed to be for a 18F452 and the sounder part is for the 18F1320
    need to delete the end after the RNG ?? pretty sure??
    {
    *****************************************************************************
    * Name : UNTITLED.BAS *
    * Author : [select VIEW...EDITOR OPTIONS] *
    * Notice : Copyright (c) 2009 [select VIEW...EDITOR OPTIONS] *
    * : All Rights Reserved *
    * Date : 4/7/2009 *
    * Version : 1.0 *
    * Notes : *
    * : *
    *****************************************************************************
    }
    {
    *****************************************************************************
    * Name : RandGen.BAS *
    * Author : Ahmed Lazreg (Octal) *
    * octal@pocketmt.com Pocket MicroTechnics *
    * Notice : Copyright (c) 2007 *
    * : All Rights Reserved *
    * Date : 06/10/2007 *
    * Version : 1.0 *
    * *
    * Notes : A Rudimentary Pseudo Random Number Generator (Modulo based) *
    * *
    * Usage : Call Initialize() to initialize the Initial value of the *
    * generator. The generator gives better values when the initial *
    * seed value is a Prime Number. *
    * For the same Initial Seed, you will get the same serie of *
    * generated values. This let you repeat some experiences (and *
    * this is why it's called a PSEUDO-random number generator. *
    * If you need an automatic different initial value each time you *
    * start the number generator, you can set the initial value to *
    * the read of an ADC value on a FLOATING Analog PIN of a PIC. *
    * Call Rand() to get/generate a new random value *
    * *
    * You can try to change the Magic Values to change the *
    * Pseudo-Random Number Generator Behaviour *
    * *
    *****************************************************************************
    }

    Module RandGen

    Const MagicA = 40, // was 7 for magic a variable
    MagicB = 10, //was 7 for magic b
    MagicC = 100 //was 255

    Private Dim Seed As Byte

    {
    ****************************************************************************
    * Name : Rand() *
    * Purpose : Return a new Pseudo Random Number each time called *
    ****************************************************************************
    }
    Public Function Rand() As Byte
    Seed = (MagicA * Seed + MagicB) Mod MagicC
    result = Seed
    End Function

    {
    ****************************************************************************
    * Name : Rand() *
    * Purpose : Initialize the Random number generator *
    * The initial value could be a Value read from a Floating Analog *
    * PIC Pin.
    ****************************************************************************
    }
    Public Sub Initialize(ByVal InitialSeed As Byte)
    Seed = InitialSeed
    Seed = Rand()
    End Sub
    (
    RandGen Module... a better implementation (using LongWord vars)

    This is my favourite implementation. This implementation use LongWord variables ported from an old BSD Linux Kernel random number generator For X86 platforms(this explains the LongWord usage And big Magic constants). I did Not found the old link from where I ported it first time (To Fortran) when I needed it at university, but this link describes the backgrounds (based on Free BSD also) http://www.panix.com/~elflord/cpp/random/ and a very nice description can be found here HowStuffWorks "How can a totally logical computer generate a random number?" (the version implemented below) .

    This implementation introduces RndMax propertie that you could set in the Initialize subroutine. This values sets the interval in which generated values are constrained (normalized). By default, RandMax is set To 255 (max value For a Byte).

    This implementation is better than the implementation using Byte variables. It uses two magic constants that makes generated values really fine (seems truly random).

    If you really need a nice generator For PIC, use this one (And keep RandMax limited To 255 so that you can assign the result of Rand() Function directly To any Byte variable (the previous sample Program can be reused As is).


    *****************************************************************************
    * Name : RandGen.BAS *
    * Author : Ahmed Lazreg (Octal) *
    * octal@pocketmt.com Pocket MicroTechnics *
    * Notice : Copyright (c) 2007 *
    * : All Rights Reserved *
    * Date : 06/10/2007 *
    * Version : 1.0 *
    * *
    * Notes : A Rudimentary Pseudo Random Number Generator (Modulo based) *
    * *
    * Usage : Call Initialize() to initialize the Initial value of the *
    * generator. The generator gives better values when the initial *
    * seed value is a Prime Number. *
    * For the same Initial Seed, you will get the same serie of *
    * generated values. This let you repeat some experiences (and *
    * this is why it's called a PSEUDO-random number generator. *
    * If you need an automatic different initial value each time you *
    * start the number generator, you can set the initial value to *
    * the read of an ADC value on a FLOATING Analog PIN of a PIC. *
    * Call Rand() to get/generate a new random value *
    * *
    * You can try to change the Magic Values to change the *
    * Pseudo-Random Number Generator Behaviour *
    * *
    *****************************************************************************
    }

    Module RandGen

    Const MagicA = 1103515245,
    MagicB = 12345

    Private Dim RndMax As LongWord // Maximum number generated by the generator

    Private Dim Seed As LongWord
    {
    ****************************************************************************
    * Name : Rand() *
    * Purpose : Return a new Pseudo Random Number each time called *
    ****************************************************************************
    }
    Public Function Rand() As LongWord
    Seed = Seed * MagicA + MagicB
    result = LongWord(Seed >> 16) Mod RndMax
    End Function

    {
    ****************************************************************************
    * Name : Rand() *
    * Purpose : Initialize the Random number generator *
    * The initial value could be a Value read from a Floating Analog *
    * PIC Pin.
    ****************************************************************************
    }
    Public Sub Initialize(ByVal InitialSeed As LongWord, ByVal pRndMax As LongWord = 255)
    RndMax = pRndMax
    Seed = InitialSeed
    End Sub

    {
    ****************************************************************************
    * Name : SetRndMax() *
    * Purpose : Sets the Max Value that the Random number gen can generate *
    ****************************************************************************
    }
    Public Sub SetRndMax(ByVal pRndMax As LongWord)
    RndMax = pRndMax
    End Sub
    *****************************************************************************
    * Name : UNTITLED.BAS *
    * Author : [Select VIEW...EDITOR OPTIONS] *
    * Notice : Copyright (c) 2009 [Select VIEW...EDITOR OPTIONS] *
    * : All Rights Reserved *
    * Date : 3/31/2009 *
    * Version : 1.0 *
    * Notes : hook your sound up from PORTB.3 *
    * : *
    *****************************************************************************
    }
    Device = 18F1320
    Clock = 8 // tells the compiler the FOSC speed
    Config OSC = INTIO2, WDT = OFF, LVP = OFF
    Include "Utils.bas"
    Include "PW.bas" // import PWM module..
    Dim Duty As Byte
    OSCCON = %01111111 // select 8MHz internal oscillator
    SetAllDigital
    // main program...
    PWM.SetFreq(5000) //this set it for a 5000khz

    While true
    Duty = 40 // this starts it at 2000khz
    Repeat
    PWM.SetDutyPercent(Duty)
    Inc(Duty)
    DelayMS(25)
    Until Duty > 100 // takes it up to 5000
    Repeat
    PWM.SetDutyPercent(Duty)
    Dec(Duty)
    DelayMS(25)
    Until Duty = 40 // lower it back down


    Wend
     
  6. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    You've got to start using CODE tags when adding code so the formatting is retained. Go Advanced you'll see a # icon.

    But hey I think you're starting to get the hang of it.
     
  7. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    I tell you what the speaker is going to do click one time when you power it on. I thought
    the pwm module would work remove the (DelayMs25)
    if you want sound
    Code (text):

    Device = 18F1320
    Clock = 8 // tells the compiler the FOSC speed
    Config OSC = INTIO2, WDT = OFF, LVP = OFF
    Include "Utils.bas"
    Include "PW.bas" // import PWM module..
    Dim Duty As Byte
    OSCCON = %01111111 // select 8MHz internal oscillator
    SetAllDigital
    // main program...
    PWM.SetFreq(5000) //this set it for a 5000khz

    While true
    Duty = 80 // this starts it at 2000khz
    Repeat
    PWM.SetDutyPercent(Duty)
    Inc(Duty)
    Until Duty > 100 // takes it up to 5000
    Repeat
    PWM.SetDutyPercent(Duty)
    Dec(Duty)
    Until Duty = 60 // lower it back down


    Wend
     
    That makes sound that drive me cazy
     
    Last edited: Apr 11, 2009
  8. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    you refer to code

    will look at advanced.
    the stuff I posted was just copied n pasted but not real sure of whats going on.
    can you recommend a source to refer to the syntax in swordfish-tried locating but ??
    stuff like movlw, TRISA etc??
    will take out the delay be80be
    do I copy n paste the code you just posted or is it the same as the last sound code you posted?
     
  9. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    Well TRIS = Tristate direction register.
    0 = Output
    1 = Input

    By default all I/O pins are Input.

    Swordfish BASIC will set the TRIS direction to Output automatically with LOW(x) or HIGH(x) commands.
     
  10. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
  11. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    Just copy and paste in swordfish and build hook you a 8 ohm speaker from RB3 like this
    circuit
    And try this code to
    Code (text):

    Device = 18F1320
    Clock = 8 // tells the compiler the FOSC speed
    Config OSC = INTIO2, WDT = OFF, LVP = OFF
    Include "Utils.bas"
    Dim out As PORTB.3
    OSCCON = %01111111
    SetAllDigital

     Low (PORTB.3)
     While true
     
        High (out)
        DelayUS (1400)   // justs change this to change sound
        Low (out)
        DelayUS (700)  // and this to
       
       
     Wend
     
     

    Attached Files:

  12. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    what happened to the pwm stuff?

    in an earlier post you had a pwm code section in it that I pasted under the RNG section
    using magic A and magic B (found on swordfish site) as variables so the frequency would change as per the RNG.
     
  13. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    Did you connect a speaker with transistor to RB.3 ? The earlier PWM program should work. You also must download the module PW.BAS from the swordfish site and put it in the UserLibrary folder.
     
  14. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    have several 18F1320 chips on order.

    program one to try this circuit out do I connect a cable from CON2 to a bread board?
    am assuming so as it has really only has 5 pins.
    found a website that has a video on using the pickit2 with swordfish. pretty informative 17 minutes.
     
  15. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    Post the link. The PWM (and servo) connector is the 3 pin male connector on the right side of the Junebug.

    PS Gramo's (a member here) site has lots of Swordfish examples and schematics. Well worth a look (the link in on the links page of my site)
     
  16. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    I think that be80be included it

    in the post he did several days ago
    will check.
    now (need speaker but) connect to RB3 and ground I assume??
    I have some 8 inch duel cone SPARK-O-MATIC speakers (where I got them??) but I think I can pull one from an old computer case.
     
  17. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    here is the code

    that I copied n pasted. and yes I looked in the library and PW.bas is there



    Device = 18F1320
    Clock = 8 // tells the compiler the FOSC speed
    Config OSC = INTIO2, WDT = OFF, LVP = OFF
    Include "Utils.bas"
    Include "PW.bas" // import PWM module..
    Dim Duty As Byte
    OSCCON = %01111111 // select 8MHz internal oscillator
    SetAllDigital
    // main program...
    PWM.SetFreq(5000) //this set it for a 5000khz

    While true
    Duty = 40 // this starts it at 2000khz
    Repeat
    PWM.SetDutyPercent(Duty)
    Inc(Duty)
    DelayMS(25)
    Until Duty > 100 // takes it up to 5000
    Repeat
    PWM.SetDutyPercent(Duty)
    Dec(Duty)
    DelayMS(25)
    Until Duty = 40 // lower it back down


    Wend
     
  18. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    The 20ma RB3 can source is not enough to drive a speaker, you need to add the simple resistor and transistor driver as shown by be80be.

    What you're going to find out is with programming there are almost unlimited ways of doing something. It's confusing at first but stick with it and you'll get the hang of it.
     
  19. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    looking at my present code

    the RNG defines magic A and magic B but in the pwm section I pasted in it doesn't have a way of knowing that magic A or B = Duty
    need to figure it out.
    definably going to rob a speaker from an old computer case - duh I have 4 laying around here.
     
  20. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    Rob a npn and 10k resistor why your at it and remove the
    Code (text):

    DelayMS(25)
     
    you want to learn about Alias and Modifiers
     
    Last edited: Apr 11, 2009
  21. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    I want to learn about all this code

    Now what is an/are Alias and Modifiers??
    I need to locate a book or find a website that lists all the commands/syntax and explains them.
    any suggestions?
     

Share This Page