What the heck am I doing wrong?


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159

    Default What the heck am I doing wrong?

    I'm having some trouble figuring out something and hope someone here can help.

    I am doing a project to control the marker lights on a pace car for a race track. We will have
    strobes on the four corners and on the front of the mirrors, as well as four lights on the rear
    that will indicate 'pass left', 'pass right' and 'do not pass'.

    To that end, I have decided to use a 'time slice' scheme to control the lights. Based on switch
    inputs, there will be word or long-sized 'patterns' of bits to turn the outputs on or off, then a pause
    and move on to the next bit in the pattern.

    My problem is figuring out how to shift the bits out of the patterns to turn the output ports on and off.

    I had thought of using SHIFTRIGHT (>>) to shift the bits off the LSB end of the pattern to turn the
    port on or off based on it being a one or zero, but it would appear that SHIFTRIGHT discards the shifted
    bit and won't let it be used.

    My other thought was to use an index to address the bits individually, but I don't think this will work
    either. For example:

    Code:
    pattern var byte 'may be a word or even long in the actual program
    index var byte
    
    pattern = 01010101  'example pattern, not all will be this regular
    
    for index = 0 to 7
        PORTB.0 = pattern.index 'turn portb.0 on or off based on whether the bit is 0 or 1
        pause 100
    next index
    For some reason, the 'percent' sign vanished - it's supposed to be 'percent' 01010101

    I'm pretty sure that won't work either. What am I doing wrong? Any ideas how I can accomplish this? I know there
    must be a way, I'm just missing it. Perhaps a 'bit array'? Is that even possible?

    Thanks for any help,

    Andy
    Last edited by andywpg; - 13th March 2012 at 23:00.

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


    Did you find this post helpful? Yes | No

    Default Re: What the heck am I doing wrong?

    Andy, I can't help you with your code. But if you click on the first icon above the editor box, two As, you want to be in SOURCE mode, not WYSIWYG.

    Then this will always work:
    %01010101


    To set this by default, click on SETTINGS at top right, then GENERAL SETTINGS, then at Message Editor Interface, click on Standard Editor - Extra formatting controls.

    Robert

  3. #3
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: What the heck am I doing wrong?

    I had thought of using SHIFTRIGHT (>>) to shift the bits off the LSB end of the pattern to turn the
    port on or off based on it being a one or zero, but it would appear that SHIFTRIGHT discards the shifted
    bit and won't let it be used.
    Maybe not the best way, but "pattern" can be the high byte alias of a word variable,
    then when you shift it, the low byte will either be 00000000 or 10000000,
    which is either zero or not zero.

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: What the heck am I doing wrong?

    Hi,
    Try something like
    Code:
    Pattern = %11001010
    FOR index = 0 to 7
        PORTB.0 = pattern.0[index]  'turn portb.0 on or off based on whether the bit is 0 or 1
        pause 100
    NEXT index
    /Henrik.

  5. #5
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: What the heck am I doing wrong?

    Would a look up table work? Something like this:
    Code:
    starthere:
        IF gpio.3 = 1 THEN GOTO main
       
    GOTO starthere
     
    '------------------------------------flash them all to make it look cool
    main:
    FOR i = 1 TO 20
           RANDOM myword
           mybyte = (myword//20)
           IF previous_random = mybyte THEN GOTO main
           GOSUB high_led
           PAUSE delay
           gpio = %000000
           PAUSE delay
           previous_random = mybyte
    NEXT i
     
    gpio = 0
    trisio = %111111
    ' ----------------------------------------------------send it back to nap
    GOTO starthere
     
    high_led:
        LOOKUP mybyte,[%000001,%000010,%000001,%000100,%000001,%010000,%000001,%100000,%000010,%000100,%000010,%010000,%000010,%100000,%000100,%010000,%000100,%100000,%010000,%100000],gpiostate
        gpio = gpiostate 
        LOOKUP mybyte,[%111100,%111100,%111010,%111010,%101110,%101110,%011110,%011110,%111001,%111001,%101101,%101101,%011101,%011101,%101011,%101011,%011011,%011011,%001111,%001111],tristate
        trisio = tristate 
    RETURN
     
    END
    I cut this out of a program I was using to control 20 LEDs so you'd have to modify the table to suit your needs.

  6. #6
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: What the heck am I doing wrong?

    That worked, thanks!

  7. #7
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: What the heck am I doing wrong?

    I think this would work too - thanks! It amazes me how many ways there are to do the same thing.

  8. #8
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,154


    Did you find this post helpful? Yes | No

    Default Re: What the heck am I doing wrong?

    Wait, there's more, the brainiacs will post assembler solutions now.

    Robert

  9. #9
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: What the heck am I doing wrong?

    Quote Originally Posted by Demon View Post
    Wait, there's more, the brainiacs will post assembler solutions now.

    Robert
    Actually, before I saw Henrik's method, I came up with ANOTHER solution that worked too. I used byte-sized arrays. It worked great, only it used 160 bytes of 367 bytes of total RAM available. Using arrays the way Henrik showed me (same thing, only on the bit level) it's only a few bytes.

    Before I came up with the byte array solution, I was toying with an assembler subroutine that used BTFSC f,b (if bit 'b' in register 'f' is a one, the next instruction is executed, otherwise skipped).

    So I would have tested the bit, then next instruction would branch to a routine that set the light on, the instruction after would set the light off.

    I just KNEW that PBP would be able to do it though.

    The best part? The dang program worked first try! Only a couple of syntax errors to clear up, but IT WORKED! Tested everything on a breadboard, now all I have to do is build it in a box and make it look pretty......

    Thanks for all the help!

    Andy

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