Random in PIC12F683


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2010
    Posts
    25

    Default Random in PIC12F683

    I want to have two times randomized, space and pulse, but they seem to be the same (=short) all the time.
    What have I done wrong?



    'Define ADCIN parameters
    ANSEL = %00001101 'GP4 = AN3
    CMCON0 = %00000111 'Deactivate the comparator
    DEFINE ADC_BITS 8 'Set number of bits in result
    DEFINE ADC_CLOCK 5 'Set clock source
    DEFINE ADC_SAMPLEUS 50 'Set sampling time in us

    OSCCON = %01110101 'Sets internal osc to 8 Mhz and stable
    DEFINE OSC 8 'Clock = 8 MHz

    TRISIO = %11111111 'Defining PORT (all inputs)

    orange var byte 'Value for base colour
    colour var byte 'Value for colour
    space var byte 'Value for time between pulses
    spacet var byte 'Counter for space time
    spacet1 var word 'Value for random space time
    pulse var byte 'Value for pulse time
    pulset var byte 'Counter for pulse time
    pulset1 var word 'Value for random pulse time

    orange = 45
    space = 100
    pulse = 16
    colour = orange


    loop4:
    'spacet = space
    'pulset = pulse

    loop1: Pause 10 'Wait 10 ms
    colour = colour + 1 'Increase colour by 1
    pulset = pulset - 1 'Decrease pulse time by 1
    hpwm 1, colour, 22000 'channel 1, duty cycle, freq
    if pulset = 0 then goto loop2 'Go to loop2
    goto loop1

    loop2: Pause 10 'Wait 10 ms
    colour = colour - 1 'Decrease colour by 1
    hpwm 1, colour, 22000 'channel 1, duty cycle, freq
    if colour = orange then: random spacet1'Randomize value in spacet1
    spacet = spacet1 / 655
    spacet = spacet + 10
    goto loop3 'Go to loop3
    goto loop2

    loop3: Pause 10 'Wait 10 ms
    colour = orange
    spacet = spacet - 1 'Decrease space time by 1
    hpwm 1, colour, 22000 'channel 1, duty cycle, freq
    if spacet = 0 then: random pulset1'Randomize value in pulset1
    pulset = pulset1 / 4096
    goto loop4 'Go to loop4
    goto loop3
    end


    PS. There are some unused code from an older application using ADC, but that should not interfere.

  2. #2
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,598


    Did you find this post helpful? Yes | No

    Default Re: Random in PIC12F683

    Loop 2 and 3 cannot loop?

    2 gotos in succession.

    Robert

  3. #3
    Join Date
    Aug 2010
    Posts
    25


    Did you find this post helpful? Yes | No

    Default Re: Random in PIC12F683

    Hm, strange.

    Shouldn't I get an error report if there is a loop error?

    If I add an ELSE, I get a syntax error.

    I have changed some and now it works, sort of.
    Some pulses seem to be too long....
    I think I will have to do some measurements to see what parameters I really want.

    Here's the new program:


    'Define ADCIN parameters
    ANSEL = %00001101 'GP4 = AN3
    CMCON0 = %00000111 'Deactivate the comparator
    DEFINE ADC_BITS 8 'Set number of bits in result
    DEFINE ADC_CLOCK 5 'Set clock source
    DEFINE ADC_SAMPLEUS 50 'Set sampling time in us

    OSCCON = %01110101 'Sets internal osc to 8 Mhz and stable
    DEFINE OSC 8 'Clock = 8 MHz

    TRISIO = %11111111 'Defining PORT (all inputs)

    orange var byte 'Value for base colour
    colour var byte 'Value for colour
    space var byte 'Value for time between pulses
    spacet var byte 'Counter for space time
    spacet1 var word 'Value for random space time
    pulse var byte 'Value for pulse time
    pulset var byte 'Counter for pulse time
    pulset1 var word 'Value for random pulse time

    orange = 45
    space = 100
    pulse = 16
    colour = orange

    spacet = space
    pulset = pulse





    loop1: Pause 10 'Wait 10 ms
    colour = colour + 1 'Increase colour by 1
    pulset = pulset - 1 'Decrease pulse time by 1
    hpwm 1, colour, 22000 'channel 1, duty cycle, freq
    if pulset > 0 then goto loop1 'Go to loop1


    loop2: Pause 10 'Wait 10 ms
    colour = colour - 1 'Decrease colour by 1
    hpwm 1, colour, 22000 'channel 1, duty cycle, freq
    if colour > orange then goto loop2
    random spacet1'Randomize value in spacet1
    spacet = spacet1 / 655
    spacet = spacet + 10
    random pulset1'Randomize value in pulset1
    pulset = pulset1 / 4096



    loop3: Pause 10 'Wait 10 ms
    colour = orange
    spacet = spacet - 1 'Decrease space time by 1
    hpwm 1, colour, 22000 'channel 1, duty cycle, freq
    if spacet = 0 then: goto loop1 'Go to loop1


    goto loop3
    end
    'Define ADCIN parameters
    ANSEL = %00001101 'GP4 = AN3
    CMCON0 = %00000111 'Deactivate the comparator
    DEFINE ADC_BITS 8 'Set number of bits in result
    DEFINE ADC_CLOCK 5 'Set clock source
    DEFINE ADC_SAMPLEUS 50 'Set sampling time in us

    OSCCON = %01110101 'Sets internal osc to 8 Mhz and stable
    DEFINE OSC 8 'Clock = 8 MHz

    TRISIO = %11111111 'Defining PORT (all inputs)

    orange var byte 'Value for base colour
    colour var byte 'Value for colour
    space var byte 'Value for time between pulses
    spacet var byte 'Counter for space time
    spacet1 var word 'Value for random space time
    pulse var byte 'Value for pulse time
    pulset var byte 'Counter for pulse time
    pulset1 var word 'Value for random pulse time

    orange = 45
    space = 100
    pulse = 16
    colour = orange

    spacet = space
    pulset = pulse





    loop1: Pause 10 'Wait 10 ms
    colour = colour + 1 'Increase colour by 1
    pulset = pulset - 1 'Decrease pulse time by 1
    hpwm 1, colour, 22000 'channel 1, duty cycle, freq
    if pulset > 0 then goto loop1 'Go to loop1


    loop2: Pause 10 'Wait 10 ms
    colour = colour - 1 'Decrease colour by 1
    hpwm 1, colour, 22000 'channel 1, duty cycle, freq
    if colour > orange then goto loop2
    random spacet1'Randomize value in spacet1
    spacet = spacet1 / 655
    spacet = spacet + 10
    random pulset1'Randomize value in pulset1
    pulset = pulset1 / 4096



    loop3: Pause 10 'Wait 10 ms
    colour = orange
    spacet = spacet - 1 'Decrease space time by 1
    hpwm 1, colour, 22000 'channel 1, duty cycle, freq
    if spacet = 0 then: goto loop1 'Go to loop1


    goto loop3
    end
    Last edited by mikebike; - 22nd March 2013 at 08:56.

  4. #4
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,598


    Did you find this post helpful? Yes | No

    Default Re: Random in PIC12F683

    I would either connect an lcd or add rs232 link to a pc to display the values as they are being generated, and add pauses to review data.

  5. #5
    Join Date
    Oct 2011
    Location
    East Coast USA
    Posts
    16


    Did you find this post helpful? Yes | No

    Default Re: Random in PIC12F683

    Why is there a colon after 'then'?

  6. #6
    Join Date
    Oct 2011
    Location
    East Coast USA
    Posts
    16


    Did you find this post helpful? Yes | No

    Default Re: Random in PIC12F683

    To the best of my knowledge, PB does not expect a colon after 'then'.


    >> if spacet = 0 then: random pulset1'Randomize value in pulset1

    Also, is one of those consecutive gotos supposed to be a gosub?

  7. #7
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Random in PIC12F683

    From the manual
    4.13. Multi-statement Lines

    In order to allow more compact programs and logical grouping of related commands, PBP supports the use of the colon ( : ) to separate statements placed on the same line. Thus, the following two examples are equivalent:

    W2 = W0
    W0 = W1
    W1 = W2

    is the same as:

    W2 = W0 : W0 = W1 : W1 = W2
    This does not, however, change the size of the generated code.

    Dave
    Always wear safety glasses while programming.

Similar Threads

  1. RPM of CPU fan PIC12f683
    By mrmodest in forum General
    Replies: 3
    Last Post: - 3rd September 2012, 04:12
  2. Help with PICBasic and PIC12F683
    By plyrathrt in forum General
    Replies: 4
    Last Post: - 8th July 2008, 00:23
  3. pic12f683 setup
    By erice1984 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 4th July 2007, 11:48
  4. Pic12f683 Mclre
    By jmbanales21485 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 30th March 2007, 23:37
  5. Using the pic12F683
    By TonyA in forum General
    Replies: 2
    Last Post: - 6th April 2006, 12:46

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts