# something not right??

Status
Not open for further replies.

#### MrDEB

##### Well-Known Member
want to cycle in a FOR NEXT loop but can't even get one led to come on unless I just go for portb.0 = 1
want to dimension the leds to work with the ports but use leds(x)
pretty basic but ??
I tried portb.0 = leds(0) but it won't work.

leds(8) as byte

leds(0) = portb.0

leds(0) = 1
delayms(1000)
leds(0) = 0

#### JonSea

##### Well-Known Member
It's pretty much impossible to figure out what you're trying to do with that....um....code.... you posted. Perhaps you could explain it in some detail.

Taking a guess, if you have LEDs connected to all of the pins on port B, and you're trying to light each in turn, you could use the rotate shift
command which is indicated by a double greater-than or less-than symbols.

I always have to fiddle with the syntax because I don't use it often.

If you rotates shift 1 bit left, starting with

%00000001

yields

%00000010

then

%00000100

then

%00001000

and so on.

The Swordfish Basic syntax is the same as the C syntax as I recall.

Note: This shift command should not be confused with shifting out data to a serial shift register. That's a totally different usage.

Last edited:

#### Pommie

##### Well-Known Member
If I understand correctly, you need a pointer to achieve what you want.

Blue touch paper lit and standing well back.

Mike.

#### JonSea

##### Well-Known Member
I used the wrong term above. The correct command is the bitwise-operator shift. The bits in a byte are shifted right(>>) or left(<<) the number of places the number after the command specifies.

#### Dr_Doggy

##### Well-Known Member
my guess too is that this line: leds(0) = portb.0 just passes a single value one time only.

not sure exactly how myself, but you need to define it, something like:
#define leds(0) = portb.0
... then the compiler will understand that when you say leds(0), you actually mean portb(0)

#### atferrari

##### Well-Known Member
Is the doubt about the algorithm or the syntax?

For many years now, all I code is previously outlined in a flow diagram. Double advantage: the algorithm becomes clear prior coding and writing the code becomes just a clerical work. It works for me.

#### Cicero

##### Active Member
Wait, what language is this?

#### JonSea

##### Well-Known Member
This will make LEDs connected to Port B light up sequentially 1 at a time for a tenth of a second. Note that the shifted bit falls off the end, so you have to replace the bit to start over.

LEDs on when the port pin is low? Shift a zero across instead of a 1.

Code:
PortB = 1
Delayms(100)
For I = 1 to 7
PortB = Port B << 1
Delayms(100)
Next

#### MrDEB

##### Well-Known Member
Didn't explain in right context

led(7) as byte I think

what I want to do is use a FOR NEXT loop to access all the leds

for x = 0 to 7

led(x) = 1

delayms (500)

led(x) = 0

next

I tried this

DEVICE = 18F2221

CLOCK = 8

INCLUDE "INTOSC8.bas"

//INCLUDE"InternalOscillator.bas"

INCLUDE "SetDigitalIO.bas"

DIM led(7) AS BYTE

// alias to port pin...

'DIM LED(1) AS PORTb.0

DIM x AS BYTE

led(1)=portb.1

trisb=\$00000000

setalldigital

WHILE true

led(1) = 1

DELAYMS(2000)

led(1)=0

DELAYMS(1000)

WEND

but no led comes on
will look at shifting the bits but ??

#### JonSea

##### Well-Known Member
Underlines, bold and italic is certainly a new look for code. Makes it hard as hell to read but it's very festive.

Your explanation of what you are trying to do is as much a mystery as that string of "code" you posted.

If you can explain exactly what you are trying to do, and why

Dim LED0 as PortB.0
Dim LED1 as PortB.1
Dim LED2 as PortB.2

Dim LED7 as PortB.7

#### atferrari

##### Well-Known Member
Is the doubt about the algorithm or the syntax?

For many years now, all I code is previously outlined in a flow diagram. Double advantage: the algorithm becomes clear prior coding and writing the code becomes just a clerical work. It works for me.
Hola MrDEB,

Could you reply to my question? It is not rhetorical.

Could you?

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
If I understand correctly, you need a pointer to achieve what you want.

Blue touch paper lit and standing well back.

Mike.
Ha ha!!!! Beleive it or not.. Oshonsoft HAS pointers!!!!! Not to sure about Swordfish!

How about a union??? ( Standing further back! )

##### Well-Known Member
Could we / I get a tutorial on pointers here ! I always have to mess around (C) after compiler complains like.. ' using an int as a pointer without a cast' ... what ever that means, still seems to work, eventually error goes away usually don't know how i fixed it .. I understand the principal , ( perhaps) but where do I put the * ... perhaps a different thread Ian ?

#### MrDEB

##### Well-Known Member
in post 9 I used openoffice but I guess it was not a good choice.
I want to dimension the array of led(x) and swt(x)
basically so I can use
led(0) as portb.0
led(1) as portb.1
using same context
for x = 0 to 7
led(x) = 1
delayms(500)
led(x) = 0
next
this is supposed to blink the led on portb.0 and b.1
am mad at myself that I can't get this to work. I have a 18f4520 pic with edited code for the 18f2221 but it refusses to work. Decided to try a truth table where I have 8 swtiches and 8 leds. when swt(1) goes LOW then led(1) goes high.
upon making swt(1) LOW a second time then led(1) goes LOW
here is a snippet of the code I was hacking on.
"IF swt0 = 1 AND led0= 0
THEN
led0 = 0
END IF

IF swt0 = 0// AND led0 = 0 // button press
THEN
led0 = 1
DELAYMS(500) // switch debounce
END IF

IF swt0 = 0 AND led0 = 1 'second time button press
THEN
DELAYMS(500)
led0 = 0
END IF"
sorry but I tried using insert code but it won' allow it.

#### JonSea

##### Well-Known Member
You've rather unclearly stated what you expect code to do, but not what you want the switches and LEDs to do.

In the second part of your post, I believe you're trying to toggle each LED when its button is pressed. You’re making this way too difficult.

Code:
 While 1 = 1
If S1 = Pressed then
toggle (LED1)
DelayMS(500)
End If

If S2 = Pressed then
toggle(LED2)
DelayMS(500)
End If
.....

If S8 = Pressed then
toggle (LED8)
DelayMS(500)
End If
Wend

#### Pommie

##### Well-Known Member
I think what's needed is a subroutine that will toggle LED(x).

Something like,
Code:
Sub light(led As Byte)
if(led<8) then
porta = porta xor (1<<(led and 7))
elseif(led<16) then
portb = portb xor (1<<(led and 7))
elseif(led<24) then
portc = portc xor (1<<(led and 7))
endif
End Sub
0-7 will do porta
8-15 portb
16-25 portc

Mike.

#### JonSea

##### Well-Known Member
Very ingenious and very nice Mike!

By the way... You sir are evil!

#### Cicero

##### Active Member
Is this kind of thing what you want?

Code:
// Write LED
sub LEDw(pBit as byte, state as bit)
PORTB.Bits(pBit) = state
end sub

WHILE true
LEDw(2,1)
DELAYMS(2000)
LEDw(2,0)
DELAYMS(1000)
WEND
Not sure if read works but if you need to read then perhaps:
Code:
// Read LED
function LEDr(pBit as byte) as Bit
LEDr = PORTB.Bits(pBit)
end function

Status
Not open for further replies.