PDA

View Full Version : activating i/o ports according to variable value



Picman
- 27th July 2010, 12:45
Hi all, I am trying to work out a way to generate a variable using a for .. to .. next loop and then send the i/o port (let's say b for arguments sake) corresponding to the numeric variable value high .. eg (for x=0 to 4: high portb.x: next x). Now this doesn't work one little bit but I was wondering if there was a way to do it ( different code ) same outcome....

Cheers
:)

JEC
- 27th July 2010, 15:21
FOR x = 0 to 5
HIGH PORTB.0[x]
Pause 100
Next x

Picman
- 28th July 2010, 00:27
Thanks JEC .. gave it a whirl but got a comiling error... :(

Bruce
- 28th July 2010, 00:59
Did you declare X VAR BYTE? Should work fine otherwise. What error do you get?

Charles Linquis
- 28th July 2010, 03:24
Here is a program I posted some time ago. It can be used as an example to do what you are trying to do.

http://www.picbasic.co.uk/forum/showthread.php?t=7986&highlight=port

Picman
- 28th July 2010, 11:11
Thanks Charles, I haven't had time to run a test but maybe I've spotted my problem whilst checking your code.

IF PortValue = 4 THEN
HSEROUT [10,13,"Port A4 is Open Collector!",13,10]
ENDIF
ADCON1 = $FF
TRISA.0(PortValue)= 0 'Make it an output
PauseUs 20
PORTA.0(PortValue) = 0 'Write a '0'
GOTO StateCheck
ENDIF


My understanding, according to the code, is that I should use round brackets not square brackets as I misunderstood? JEC's code to show???? I'll try and see if it works :)

mackrackit
- 28th July 2010, 11:30
What port and chip are you using?
Have you shut the ADC off if the port has one.

Picman
- 28th July 2010, 11:53
I'm using mainly 16f628a's or a spare f84a I found lying around...all been tested and humming nicely.
With the code for 16f628a I am setting porta.4 to standard i/o with CMCON = 7 ... but I tested with ports b 1 through 5 earlier when I got the compiler error.
Am I right about the brackets? (round vs square)??

Bruce
- 28th July 2010, 14:23
It works with [] or () for the bit index variable, but the high & low commands won't work with a variable bit index.

For bit indexing port pins you need to assign a 0 or 1 to the pin like this;


Main:
for x = 0 to 7
portb.0[x] = 1
pauseus 200
portb.0[x] = 0
pauseus 200
next

GOTO Main

mackrackit
- 28th July 2010, 14:46
If you are using LEDs to see what is happening you may want to change
PAUSEUS
to
PAUSE
in the Bruce's code.

Picman
- 29th July 2010, 09:08
Well that's great news - I will try the new code tonight assigning binary values as opposed to using the 'high' or 'low' statements.

Here is the test code I was using:

device = 16f628a
x var byte
loop:
for x=0 to 5
high portb.(x)
pause 500
low portb.(x)
pause 500
next x
goto loop

and here is the error message:

Error at line [9] in file [84test.bas] ***Unrecognised characters '(x)'! ***
Error at line [11] in file [84test.bas] ***Unrecognised characters '(x)'! ***

Hopefully assigning a value will work...I'll try it out in about 3 hours when I get the kids to bed and I'm sensing a breakthrough here :)

Picman
- 29th July 2010, 12:57
:( Still no joy. Now I get a missing operator '=' error at both the lines where I assign portvalues.. sorry about not posting code however my programming terminal has no net access and I forgot to put it into my flashdrive.)

Bruce
- 29th July 2010, 13:55
Cut & paste the example in post #9. It definitely works. And I would avoid the use of loop as a lable since this is a reserved word in version 2.6 and up.