Stupid Blinking LED Question


Closed Thread
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Aug 2008
    Posts
    13

    Default Stupid Blinking LED Question

    I'm using PBP V3 and a PIC18F46K22.

    Before I do anything I make a simple program to flash between 2 different LEDs to make sure I have valid code, and I'm running into a simple/stupid problem.


    This code works:
    Code:
    '* Notes : PIC18F46K22 *
    #CONFIG
    CONFIG FOSC = HSMP
    CONFIG PLLCFG = ON
    CONFIG FCMEN = OFF 
    CONFIG BORV = 285
    CONFIG HFOFST = OFF
    CONFIG MCLRE = EXTMCLR
    CONFIG LVP = OFF
    #ENDCONFIG
    Define OSC 64 
    TRISA.0 = 0
    ANSELA.0 = 0
    TRISA.1 = 0
    ANSELA.1 = 0
     
    YLED VAR PortA.0
    LOW YLED 
    GLED VAR PortA.1
    LOW GLED
     
    X VAR LONG
    CLEAR
    :Main
    x=x + 1
    if x > 100000 then
    low YLED 
    high GLED 
    else
    high YLED 
    LOW GLED
    endif
    if x > 200000 then
    x=0
    endif
    goto Main
    This code doesn't:
    Code:
    '* Notes : PIC18F46K22 *
    #CONFIG
    CONFIG FOSC = HSMP
    CONFIG PLLCFG = ON
    CONFIG FCMEN = OFF 
    CONFIG BORV = 285
    CONFIG HFOFST = OFF
    CONFIG MCLRE = EXTMCLR
    CONFIG LVP = OFF
    #ENDCONFIG
    Define OSC 64 
    TRISA.0 = 0
    ANSELA.0 = 0
    TRISA.1 = 0
    ANSELA.1 = 0
     
    YLED VAR PortA.0
    LOW YLED 
    GLED VAR PortA.1
    LOW GLED
     
    X VAR LONG
    CLEAR
    :Main
    x=x + 1
    if x > 100000 then
    YLED = 0
    GLED = 1 
    else
    YLED = 1
    GLED = 0 
    endif
    if x > 200000 then
    x=0
    endif
    goto Main
    The only thing I'm changing is setting GLED or YLED = 0 or 1 vs using High and Low

    WTF am I missing?
    Last edited by bradb; - 6th September 2011 at 16:15.

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


    Did you find this post helpful? Yes | No

    Default Re: Stupid Blinking LED Question

    Hi,
    It's the old Read-Modify-Write problem. HIGH/LOW takes a little longer to execute (because it also writes to TRIS every time) so it doesn't suffer in this case but direct port.bit access may suffer from the RMW issue.

    Try writing to the port latch instead of the port itself by aliasing your YLED/GLED to LATA.0 and LATA.1

    /Henrik.

  3. #3
    Join Date
    Aug 2008
    Posts
    13


    Did you find this post helpful? Yes | No

    Default Re: Stupid Blinking LED Question

    Thank you! that was it...

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