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