Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Variable expansion with i/o ports?

Status
Not open for further replies.

eTech

Well-Known Member
Is there a way to use variable expansion (in PIC Basic) with i/o ports?

In other words, is there a way to do something like this:

Dim a as byte

a = 1

If PORTB.(a) = 1 then
'do something
Endif

eT
 
I think it will work. I have not used pic basic recently. The answer probably is on the PIC Basic forum. Do you get an error when you try it?
 
I think it will work. I have not used pic basic recently. The answer probably is on the PIC Basic forum. Do you get an error when you try it?

yes.

Error:
"There has to be at least one variable in the test expression"

I'm trying to write a procedure I can pass a value to flash an LED. The variable represents the port number.

So now i'm trying this approach:

Symbol led_1 = PORTC.1

Call flash_led(led_1)

Proc flash_led(ledno As Bit)
If ledno = 0 Then
High ledno
WaitUs 100
Low ledno
WaitUs 100
Else
Low ledno
WaitUs 100
High ledno
WaitUs 100
Endif
End Proc
 
Last edited:
Pic Basic Pro

OK..I guess Oshonsoft Basic isn't as flexible as other versions.

I can't do this:

Dim i As Byte
For i = 0 To 7 'loop for 8 counts
PORTB = %00000000 'make all pins low
PORTB.0 = 1 'make pin number i high <----compiler errors "Constant Arg cannot be used"
WaitMs 500 'pause for half a second
Next i

I need to be able to reference I/O pins with a variable.

Guess I'll look for a different compiler...

Thanks

eT
 
hi,
This demo works on Oshonsoft

Code:
Dim i As Byte
Dim p As Byte

TRISB = 0x00

loop1:

PORTB.0 = 1
WaitMs 10''''''''''''''''''''added
For i = 0 To 7  'loop for 8 counts
PORTB = ShiftLeft(PORTB, 1)
WaitMs 10
Next i

Goto loop1
 
Last edited:
hi,
This demo works on Oshonsoft

Code:
Dim i As Byte
Dim p As Byte

TRISB = 0x00

loop1:

PORTB.0 = 1
WaitMs 10''''''''''''''''''''added
For i = 0 To 7  'loop for 8 counts
PORTB = ShiftLeft(PORTB, 1)
WaitMs 10
Next i

Goto loop1

OK...I'm not making the connection... :)

I need to be able to reference an I/O pin using a variable as the pin identifier.

I was hoping to be able to do this:

If PORTB.(i) = 0 Then
High PORTB.(i)
Endif

Is there a way to this?

eT
 
You only need to write a simple procedure to manipulate a port..

I just wrote a function to change any pin without altering any others

Code:
main:
	PORTB = writeport(LATB, 3, 1)
	PORTC = writeport(LATC, 2, 0)
	PORTB = writeport(LATB, 6, 1)
	PORTC = writeport(LATC, 1, 0)
Goto main
End                                               

Function writeport(port As Byte, pin As Byte, condition As Byte) As Byte
Dim idx As Byte
Dim buf As Byte
	idx = LookUp(1, 2, 4, 8, 16, 32, 64, 128), pin  'POWER OF 2
	buf = port  'READ PORT ( LATCH )
	idx = Not idx  'INVERT ALL BUT PIN
	buf = buf And idx  'AND TO CLEAR BIT
	If condition = 0 Then  'IF PIN NEEDS CLEARING FIN HERE
			writeport = buf  'RETURN WITH ONLY THAT PIN CLEARED
			Exit
	Endif
	idx = Not idx  'OTHERWISE WE NEED TO
	buf = buf Or idx  'SET JUST THAT PIN
	writeport = buf ' ALL DONE
End Function
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top