12f675_fuse_about_to_blow!


Closed Thread
Results 1 to 40 of 929

Hybrid View

  1. #1
    Join Date
    Feb 2010
    Location
    I live in the UK
    Posts
    562


    Did you find this post helpful? Yes | No

    Default

    Ah, have I got 'PULSIN' in the right place in the program? Might it be better in front of HIGH PORTA.2 ?

    Nope, still outputting zero's.

    The manual says of zero's:

    If the pulse edge never happens or the width of the pulse is too great to measure, Var is set to zero
    Now I'm thinking the pulse must happen because the LED lights and with a 'RANDOM' delay but could it be to big to measure?

    How big is too big to measure for a pulse?

    Dave
    Last edited by LEDave; - 8th July 2010 at 23:20.

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


    Did you find this post helpful? Yes | No

    Default

    As far as I know PULSIN is for reading a pulse on a pin from the outside not a pulse from the inside.

    It has been a long day so maybe I am missing something... What exactly are you wanting to do?
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default

    Hi Dave,
    I see you've been making a lot of progress, good job!

    Remember that PBP executes one statement at the time. In this case you set the pin high, then you pause, then you start the pulsein statement which waits for a positive going signal which isn't coming since the pin is already set high so it times out and returns zero.

    On top of that, as Mackrackit says, PULSIN is meant to be used on pins configured as inputs, I'm not sure if it works like you have it.

    And, why do you need to measure it? Why not simply "print" the value returned by the RANDOM command? I mean PAUSE works with units of ms if RANDOM returns 100 you divide that by 14 and get 7 so you'll get a pause of 7ms, if RANDOM returns 34567 you divide that by 14 and you'll get 2469ms pause. I don't see the point in trying to measure that but if you really need to do that then look into using one of the PICs hardware timers. Reset it, set the pin high and start the timer. Pause x, set the pin low, stop the timer and read its value.

    /Henrik.

  4. #4
    Join Date
    Feb 2010
    Location
    I live in the UK
    Posts
    562


    Did you find this post helpful? Yes | No

    Default

    Hi Henrik,

    Sorry for the late reply.

    I see you've been making a lot of progress, good job!
    That'll be down to mackrackit, yourself and a few others, thanks for sticking with me, it's very much appreciated.

    Remember that PBP executes one statement at the time. In this case you set the pin high, then you pause, then you start the pulsein statement which waits for a positive going signal which isn't coming since the pin is already set high so it times out and returns zero.
    I was running the program through my mind as I was going of to sleep last night and realised
    just that.

    On top of that, as Mackrackit says, PULSIN is meant to be used on pins configured as inputs, I'm not sure if it works like you have it.
    Another mistake, I got the PULSIN idea into my mind and rushed to write some code and overlooked setting the the REGISTER pin to INPUT.Good practice would be to set the I/O REGISTER then start writing code, I'll try and stick with that from now on.

    And, why do you need to measure it? Why not simply "print" the value returned by the RANDOM command?
    Another gap in my knowledge base I'm afraid, I didn't know you could 'print' the value.

    PAUSE works with units of ms if RANDOM returns 100 you divide that by 14 and get 7 so you'll get a pause of 7ms, if RANDOM returns 34567 you divide that by 14 and you'll get 2469ms pause.
    Spot on Henrik, just what I needed to know.

    I'm trying to write a program for a project which must have a RANDOM delay of zero to three seconds.

    So how about this:

    65535 / 21.845 gives 3000ms as a Maximum value. Any number generated will be lower than 65535 therefore shorter than 3 seconds!


    Code:
    ANSEL   = %00000000    'Disable analog select so ports work as digital i/o.
    CMCON0  = %00000111    'Disable analog comparators.
    TRISA   = %00000000    'Set PORTA as OUTPUT.
    PORTA   = %00000000    'Set PORTA pins all low.
    TRISC   = %00000000    'Set PORTC as OUTPUT.
    PORTC   = %00000000    'Set PORTC pins all low.
    
    DEFINE OSC 4
    
    
    X VAR WORD  'Number between 1 & 65535
    Y var WORD  'Scaled down value of X 
    Z VAR BYTE  'The PAUSED value in 10us Increments
    
    main:        
    RANDOM X            'Random number between 1 & 65535
    LET Y = (x /22)     'Scaled down value of X
    PAUSE Y             'LED on for time period of RAN_X_OVER_22
    HIGH PORTA.2        'LED PORTA.2 lights up
    PAUSE 500
    low PORTA.2          'LED off
    GOTO MAIN:           'Do it all again
    Dave
    Last edited by LEDave; - 9th July 2010 at 13:33.

  5. #5
    Join Date
    Feb 2010
    Location
    I live in the UK
    Posts
    562


    Did you find this post helpful? Yes | No

    Default

    Another quick question:

    I've rounded up the Value 21.845 to 22 see below

    Code:
    LET Y = (x /22)     'Scaled down value of X
    When I used the value of 21.845 and try and compile it I get an error saying bad variable modifier. Is there any way of making the program use 21.845 for super accuracy?

    Dave

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default

    Hi Dave,
    Another gap in my knowledge base I'm afraid, I didn't know you could 'print' the value
    Sure, the value returned by Random is just as any other variable stored in WORD size variable.

    That should work. Obviously you'd have to change Pause 500 to Pause Y. Another small note, there's really no need for the Y variable in this case, it's perfectly OK to do:
    Code:
    TRISA.2 = 0
    Random X
    SEROUT 2 PORTC.3, 16780, [DEC X, 13, 10]   'Send value out
    X = X / 22   'Scale down, no need for the Y variable.
    PortA.2 = 1
    Pause X
    PortA.2 = 0
    I remember reading that RANDOM in reality is far from that. Being given a certain seed it returns the same "random" number, in the same order every time the program is run, or something like that. Search the forum a bit and I believe you'll find some info regarding that.

    /Henrik.

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


    Did you find this post helpful? Yes | No

    Default

    http://www.picbasic.co.uk/forum/show...ht=random+seed

    An MCU does not handle anything other than integer math with out some heavy manipulation. Most times it is better to find away around using decimals. So in your case I would stick with 21.

    Or you could dive into this
    http://melabs.com/resources/fp.htm
    Dave
    Always wear safety glasses while programming.

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default

    Hi Dave,
    I missed that second post of yours but Mackrackit covered it. The PIC only deals with integer numbers but there are several tricks around that, here's one:
    Code:
    X VAR WORD
    Random X
    X = X * 1000
    X = DIV32 21845
    SEROUT2, PORTC.3, 16780, [DEC Y, 13, 10]
    As you can see here, the X = X*1000 most likely produces a result that won't fit within a WORD, however PBP (even before the arival of LONG variable) allows you to use the intermediate 32bit result for calculations like this one. So, we multiply X by 1000 and then divide it by 1000*21.845 which is the same as dividing X by 21.845 in the first place. It will truncate whatever is to the right of the decimal point but in this case it means that you'll be off by no more than 1ms.

    Thanks Mackrackit for digging up the posts on RANDOM!

    /Henrik.
    Last edited by HenrikOlsson; - 9th July 2010 at 14:22.

Members who have read this thread : 0

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