View Full Version : Set bits of Word problem
pdegior
- 3rd September 2005, 05:19
I am trying to set individual bits of a word to correspond to the state of input pins so that I can test the word as a decimal value. Here is a snipet of the code.
dip var byte
input porta.1
input porta.2
input porta.3
dip.0 = porta.1
dip.1 = porta.2
dip.2 = porta.3
if dip = 0 then
high portb.0
enif
if dip = 1 then
high portb.1
endif
...etc.
I've also tried to use Select Case instead of the If thens. If anyone sees something I'm doing wrong, any help would be greatly appreciated. Thanks in advance.
Acetronics2
- 3rd September 2005, 08:01
Hi, Pdegior
I saw something in the manual ....$ 4.17.14 ...
Have a look to it ...
Alain
mister_e
- 3rd September 2005, 10:56
TRISA=255 ' Set all PORTA pins to Input
dip var byte
Start:
dip=PORTA
select case dip
case 0
'
' Code for PORTA=0
'
case 1
'
' Code for PORTA=1
'
case 2
'
' Code for PORTA=2
'
end select
goto start
And you're using a BYTE sized variable to do it.
peterdeco1
- 3rd September 2005, 11:24
Hi Pdegior. I tested this program with only part of PORTA and it works. Hope this helps.
'CODE COMPILED FOR 16F818
OSCCON = $60 'set int osc to 4mhz
adcon1 = 7 ' set inputs to digital
TRISA = %11111111 'ALL PORTA INPUTS
TRISB = %00000000 'ALL PORTB OUTPUTS
PORTB = 0 'ALL OUTPUTS LOW
@ DEVICE MCLR_ON, INTRC_OSC, WDT_ON, LVP_OFF, BOD_ON, PWRT_ON, PROTECT_ON
DIP VAR BYTE
START:
PORTB = 0 'off all leds
LET DIP = 0 'clear the variable DIP
LET DIP.BIT0 = 0
IF PORTA.0 = 1 Then LET DIP.BIT0 = 1 'dip bits are 0 unless PORTA bit is high
LET DIP.BIT1 = 0
IF PORTA.1 = 1 Then LET DIP.BIT1 = 1
LET DIP.BIT2 = 0
IF PORTA.2 = 1 Then LET DIP.BIT2 = 1
LET DIP.BIT3 = 0
IF PORTA.3 = 1 Then LET DIP.BIT3 = 1
IF DIP = 0 Then High PORTB.0 'light corresponding LED
IF DIP = 1 Then High PORTB.1
IF DIP = 2 Then High PORTB.2
IF DIP = 3 Then High PORTB.3
Pause 1000 'wait a second
PORTB = 0 'off led
GoTo START 'do it again
pdegior
- 3rd September 2005, 22:44
Thanks,
that seems to have done it. Still not too sure why the way I had it doesn't work but oh well.
Powered by vBulletin® Version 4.1.7 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.