PDA

View Full Version : Leds on/off



Leonardo
- 29th August 2009, 02:07
Hello forum friends,

I have 4 buttons connected to PORTB and 4 LEDs to PORTB, I turn on and off with the same button for the LED but independently, with this code does not work properly any suggestions?.


start:
IF PORTB.0=0 then
HIGH PORTB.4
WHILE PORTB.0=0 : Wend : pause 50
ENDIF

IF PORTB.0=0 then
LOW PORTB.4
WHILE PORTB.0=0 : Wend : pause 50
ENDIF


iF PORTB.1=0 then
HIGH PORTB.5
WHILE PORTB.0=0 : Wend : pause 50
ENDIF

IF PORTB.1=0 then
LOW PORTB.5
WHILE PORTB.0=0 : Wend : pause 50
ENDIF

iF PORTB.2=0 then
HIGH PORTB.6
WHILE PORTB.0=0 : Wend : pause 50
ENDIF

IF PORTB.2=0 then
LOW PORTB.6
WHILE PORTB.0=0 : Wend : pause 50
ENDIF

iF PORTB.3=0 then
HIGH PORTB.7
WHILE PORTB.0=0 : Wend : pause 50
ENDIF

IF PORTB.3=0 then
LOW PORTB.7
WHILE PORTB.0=0 : Wend : pause 50
ENDIF


goto start

mackrackit
- 29th August 2009, 02:54
How do you have the TRIS set for this?

Leonardo
- 29th August 2009, 03:03
How do you have the TRIS set for this?

TRIS Series I have not configured but the code does not work well.

mackrackit
- 29th August 2009, 03:10
You will need to set the TRIS for the port. That will tell what pins are input or out puts.
Try adding this near the top of your code.


TRISB = %00001111

That will make PORTB 0 to 3 an input and 4 to 7 an output.

Leonardo
- 29th August 2009, 03:21
Hello mackrackit,

I have added TRISB =% 00001111 but as usual the code does not work well.

Thanks
Leonardo



You will need to set the TRIS for the port. That will tell what pins are input or out puts.
Try adding this near the top of your code.


TRISB = %00001111

That will make PORTB 0 to 3 an input and 4 to 7 an output.

mackrackit
- 29th August 2009, 03:34
Can you post the whole code and exactly what is it doing or not doing?

Archangel
- 29th August 2009, 04:15
iF PORTB.1=0 then
HIGH PORTB.5
WHILE PORTB.0=0 : Wend : pause 50
ENDIF

Why not use Toggle ?


IF PORTB.1=0 then
toggle PORTB.5
WHILE PORTB.0=0 : Wend : pause 50
ENDIF

Do not know what you are doing with PORTB.0 or why it is always low in each
if then loop . . . or is that a copy paste error, and is low only in the first loop ?
maybe should be PortB.1 in this loop ? Select case could tidy this up even more . . .

sayzer
- 29th August 2009, 08:01
If you are simulating it as seen in your schematic, then the simulator might be giving false simulation because you do not have resistors on the leds.

I say this is 98% possibility of the error you are getting.

------------------

Acetronics2
- 29th August 2009, 09:13
Hi,

I do not remember the 16F84 had an internal oscillator ...

none aboard the ( censored ) simulation ...

But I'm just getting 53 ...

Alain

PS: Ahhhhh ... Da Vinci Code !!!!

phoenix_1
- 29th August 2009, 09:48
Hi,

I do not remember the 16F84 had an internal oscillator ...

none aboard the ( censored ) simulation ...

But I'm just getting 53 ...

Alain

PS: Ahhhhh ... Da Vinci Code !!!!

16F84 and 16F84A do not have internal OSC...

sayzer
- 29th August 2009, 09:54
16F84 and 16F84A do not have internal OSC...


Hi,

I do not remember the 16F84 had an internal oscillator ...

none aboard the ( censored ) simulation ...

But I'm just getting 53 ...

Alain

PS: Ahhhhh ... Da Vinci Code !!!!


On his simulator, it is not required to connect crystal or resonator etc.
It will run normally as if those were connected.

---------------------------

Acetronics2
- 29th August 2009, 10:02
Hi, Sayzer

I do not want to use sims ... lol

BTW :




...

IF PORTB.0=0 then
HIGH PORTB.4
WHILE PORTB.0=0 : Wend : pause 50
ENDIF

IF PORTB.0=0 then
LOW PORTB.4
WHILE PORTB.0=0 : Wend : pause 50
ENDIF

...



This kind of structure can't work properly ... as the second part never can be reached !!! ( or toggling button in less than 1µs ... )

Button state AND its corresponding output state must be tested at the same time ... to take the appropriate action.

Alain

Acetronics2
- 29th August 2009, 12:34
Sorry ...



or toggling button in less than 1µs ...


was not 1µs but 50 ms ...

but result is the same !!!

Alain

chatchai
- 31st August 2009, 10:34
' Test I/O Ports Input and Output

SW1 var PORTB.0
SW2 var PORTB.1
SW3 var PORTB.2
SW4 var PORTB.3

LED1 VAR PORTD.0
LED2 VAR PORTD.1
LED3 VAR PORTD.2
LED4 VAR PORTD.3

i var byte

DEFINE OSC 48


'------- Initial IO Ports -------


TRISB = $FF
TRISD = 0

CMCON = 0
ADCON1 = 7

'------- Main Function -------
main:
LED1 = SW1
LED2 = SW2
LED3 = SW3
LED4 = SW4

goto main

Fredrick
- 31st August 2009, 11:37
Try this



start:
IF PORTB.0=0 then
toggle PORTB.4
WHILE PORTB.0=0 : Wend : pause 50
ENDIF


IF PORTB.1=0 then
toggle PORTB.5
WHILE PORTB.1=0 : Wend : pause 50
ENDIF

IF PORTB.2=0 then
toggle PORTB.6
WHILE PORTB.2=0 : Wend : pause 50
ENDIF

IF PORTB.3=0 then
toggle PORTB.7
WHILE PORTa.3=0 : Wend : pause 50
ENDIF


goto start

aratti
- 31st August 2009, 12:13
Place a 330 ohms in series to each leds (as suggested in previuos post), and use Toggle command (as suggested in previuos post).


start:
Pause 50
IF PORTB.0=0 then
Toggle PORTB.4
ENDIF

IF PORTB.1=0 then
Toggle PORTB.5
ENDIF

IF PORTB.2=0 then
Toggle PORTB.6
ENDIF

IF PORTB.3=0 then
Toggle PORTB.7
ENDIF

HoldLoop:
If PortB & %00001111 != 15 then HoldLoop

goto start



Al.

Leonardo
- 2nd September 2009, 21:36
Thanks Aratti


Place a 330 ohms in series to each leds (as suggested in previuos post), and use Toggle command (as suggested in previuos post).


start:
Pause 50
IF PORTB.0=0 then
Toggle PORTB.4
ENDIF

IF PORTB.1=0 then
Toggle PORTB.5
ENDIF

IF PORTB.2=0 then
Toggle PORTB.6
ENDIF

IF PORTB.3=0 then
Toggle PORTB.7
ENDIF

HoldLoop:
If PortB & %00001111 != 15 then HoldLoop

goto start



Al.