PWM Module for Swordfish

    Download

    This module implements a simple modulo-based PSEUDO Random Number Generator for Swordfish Basic. It's difficult to generate true random numbers from a deterministic machine like PIC. Hopefully, some mathematical methods let us have a series of numbers that seems really random, although they have all a fixed cycle upon which they restart to give the same series of values. this is why they are all called Pseudo random number generators. The cycling behaviour has the advantage to give you the possibility to repeat the same experience despite the fact that it's based on (pseudo) random numbers.

Usage example

    Call the RandGen.Initialize(pInitialValue) one time with the initial value (value read from an analogue floating PIC pin for example).

    Call the RandGen.Rand() function to get a new random value.

The following program gives an example on how to use the pseudo-random number generator.

 

Variable Frequency PWM Example

Device = 18F4620
Clock = 20

Include "usart.bas"
Include "convert.bas"
Include "RandGen.bas"

Dim RandomNumber As Byte

SetBaudrate(br9600)

// initialize the generator Initial Seed Value to 2
// for Full Random value you can use an ADC read on
// a floating AN PIC Pin
RandGen.Initialize(2)                                               

TRISB = 0 'All output

While true
   // generates a number
  
RandomNumber = RandGen.Rand()   

   // write to RS233
   Write(DecToStr(RandomNumber),13,10)      

   // visualise number on PORTB leds (on my EasyPic Board)
   LATB = RandomNumber

   DelayMS(600)
Wend

Written by Ahmed Lazreg (octal). A simple Pseudo Random Number Generator module (modulo based).

    Link: What is a Swordfish Library?

Skip Navigation Links.

Collapse Site Tutorial IndexSite Tutorial Index
Expand 16F PIC Examples16F PIC Examples
Expand 18F PIC Examples18F PIC Examples
Expand Handy TipsHandy Tips