PDA

View Full Version : Stupid Blinking LED Question



bradb
- 6th September 2011, 16:13
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:


'* 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:


'* 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?

HenrikOlsson
- 6th September 2011, 16:32
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.

bradb
- 6th September 2011, 19:26
Thank you! that was it...