PDA

View Full Version : Still Struggling with IF THEN



Russ Kincaid
- 24th March 2007, 19:09
I have an LED on GPIO.0, I want it to light when GPIO.1 is low. In this program the LED comes on for 1 second at the beginnig, goes off for 1/2 sec then comes on and stays on. It does not matter if GPIO.1 is high or low. How do I make it work only when GPIO.1 is low?
rem device = 12F675

high GPIO.0
PAUSE 1000

ANSEL = 0 'GPIO.0 TO GPIO.3 SET AS DIGITAL
OPTION_REG = 0 'WEAK PULLUPS ENABLED
GPIO = %00111110 'PORT GPIO.0 SET LOW INITIALLY
WPU = %00110110 'SETS WEAK PULLUP ON DIGITAL INPUTS


rem define variables
time1 var byte: time2 var byte: time4 var byte: time8 var byte
time var word
LOW GPIO.0
PAUSE 500
IF NOT GPIO.1 THEN
HIGH GPIO.0
ELSE
LOW GPIO.0
ENDIF
END

malc-c
- 24th March 2007, 20:23
The first thing that appears to me to be missing is the config settings for the device

@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON

I would also suggest that you use external pullup resistors of around 10K rather than rely on the internal WPU

Russ Kincaid
- 24th March 2007, 21:43
Thanks. the internal pullup seems to be working OK. I set the configuration at programming time, I thought that was sufficient?

picster
- 24th March 2007, 22:54
What if instead of

IF NOT GPIO.1 THEN

You use

IF GPIO.1=0 THEN

? just curious to see if there's any difference



picster

malc-c
- 24th March 2007, 23:09
I always find it good practice to set the config bits in the code..

The following turns on an LED on GPIO.0 when GPIO.1 is grounded (external 10K pullup) - Hope it helps


@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON

ANSEL = 0
CMCON=7
TRISIO = %101010

LED var GPIO.0

main:
IF GPIO.1=0 THEN
LED=1
ELSE
LED=0
ENDIF
goto main:

Russ Kincaid
- 24th March 2007, 23:18
What if instead of

IF NOT GPIO.1 THEN

You use

IF GPIO.1=0 THEN

? just curious to see if there's any difference



picster

Yes, I tried that, no difference.

malc-c
- 24th March 2007, 23:20
It may not be the most structured of codes, but this also demonstrates the IF...Then...Else option



@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON

ANSEL=0
CMCON=7
TRISIO = %101010

LED var GPIO.0

main:
IF GPIO.1=0 THEN
goto flash
ELSE
LED=0
ENDIF
goto main:

Flash:
LED=1
If GPIO.1=1 then main:
pause 100
LED=0
pause 100
goto flash:


I think your original problem was in setting the PIC up as digital and turning off the analogue functions, hence the ANSEL and CMCON settings

Wow... I'm only a newbie and I've managed to pass on some advice... maybe my status should change from newbie to novice :) ;)

Russ Kincaid
- 24th March 2007, 23:34
I always find it good practice to set the config bits in the code..

The following turns on an LED on GPIO.0 when GPIO.1 is grounded (external 10K pullup) - Hope it helps


@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON

ANSEL = 0
CMCON=7
TRISIO = %101010

LED var GPIO.0

main:
IF GPIO.1=0 THEN
LED=1
ELSE
LED=0
ENDIF
goto main:


Thanks, that works, now if I can work it into my program! I think the thing that makes it work is CMCON=7. I tried that earlier but misspelled it so got an error.

Russ Kincaid
- 25th March 2007, 00:08
The IF THEN statements compile OK if I comment out the TRISIO line. The error message is: Warning line 15: $111110 numeric overflow. TRISIO never gave me a problem before, what is wrong?

rem device = 12F675
CMCON = 7
ANSEL = 0 'GPIO.0 TO GPIO.3 SET AS DIGITAL
OPTION_REG = 0 'WEAK PULLUPS ENABLED
TRISIO = $111110 'GPIO.0 SET AS OUTPUT, ALL OTHERS INPUT
GPIO = %111110 'PORT GPIO.0 SET LOW INITIALLY
WPU = %110110 'SETS WEAK PULLUP ON DIGITAL INPUTS
rem define variables
time1 var byte: time2 var byte: time4 var byte: time8 var byte
time var word
IF GPIO.1 = 0 THEN
time1 = 1
ELSE
TIME1 = 0
ENDIF
END

malc-c
- 25th March 2007, 00:14
The error message is: Warning line 15: $111110 numeric overflow. TRISIO never gave me a problem before, what is wrong?

Needs to be % not $
try
TRISIO = %111110

Russ Kincaid
- 25th March 2007, 04:33
Thanks, I should have noticed that!
My program runs now, not quite right yet, but I am getting there.

malc-c
- 25th March 2007, 10:47
Excellent... the 12F675 can be a finiky thing to configure right... especially for us newbies ;)