PDA

View Full Version : How to use For Next Loop to run sequence light ?



Bracer
- 29th August 2010, 10:18
@ DEVICE pic16F628A, INTRC_OSC_NOCLKOUT, MCLR_OFF
CMCON = 7

Counter var Byte
Counter = 0

Routine:

For Counter = 0 to 6
High PortB.Counter
Next Counter

pause 500

For Counter = 0 to 6
Low PortB.Counter
Next Counter

pause 500

GoTo Routine


Apparently:

High PortB.Counter
and

Low PortB.Counter
is giving me errors, I could have done this with no errors:


For Counter = 0 to 6
High Counter
Next Counter

but this will be a PICBasic command and I will not be able to control PortA.

How do I use the For Next Loop to handle PortA too ?

Hi,

PortA.something ... something must be a CONSTANT ...

so, try indexing PortA



PortA.0[counter] = 1
; OR
HIGH PortA.0[counter]


Should compile fine.
Alain

Bracer
- 29th August 2010, 11:31
I've tried it, and it does compile :)

Unfortunately, when I put it in my circuit, only PortB.0 Blinks!
Ooo Nooooo!!!! :(



@ DEVICE pic16F628A, INTRC_OSC_NOCLKOUT, MCLR_OFF
CMCON = 7

Counter var Byte

SubRoutine:

For Counter = 0 to 2
high PortB.0[Counter]
high PortA.0[Counter]
Next Counter

Pause 500

For Counter = 0 to 2
Low PortB.0[Counter]
Low PortA.0[Counter]
Next Counter

pause 800
Goto SubRoutine

rsocor01
- 29th August 2010, 11:42
I've tried it, and it does compile :)

Unfortunately, when I put it in my circuit, only PortB.0 Blinks!
Ooo Nooooo!!!! :(



Include this lines in your program


TRISA = 0 ' Make them all outputs
TRISB = 0


Robert

Bracer
- 29th August 2010, 12:33
Thanks.

Ok Now all the LEDs light up.

BUT only PortB.0 and PortB.1 Blinks.

Again, this the code:


@ DEVICE pic16F628A, INTRC_OSC_NOCLKOUT, MCLR_OFF
CMCON = 7
TRISA = 0 ' Make them all outputs
TRISB = 0

Counter var Byte

SubRoutine:

For Counter = 0 to 2
high PortB.0[Counter]
high PortA.0[Counter]
Next Counter

Pause 600

For Counter = 0 to 2
Low PortB.0[Counter]
Low PortA.0[Counter]
Next Counter

pause 100
Goto SubRoutine

Acetronics2
- 29th August 2010, 17:00
Thanks.

Ok Now all the LEDs light up.

BUT only PortB.0 and PortB.1 Blinks.

Again, this the code:


@ DEVICE pic16F628A, INTRC_OSC_NOCLKOUT, MCLR_OFF
CMCON = 7
TRISA = 0 ' Make them all outputs
TRISB = 0

Counter var Byte

SubRoutine:

For Counter = 0 to 2
high PortB.0[Counter]
high PortA.0[Counter]
Next Counter

Pause 600

For Counter = 0 to 2
Low PortB.0[Counter]
Low PortA.0[Counter]
Next Counter

pause 100
Goto SubRoutine

Hi, Bracer

I suspect a RMW issue ... as HIGH and LOW already cares of driving inputs and outputs :rolleyes:

How much current do you send to your Leds ???

try to limit it to 5 mA per Led ... as a first try.

as a second try ...



@ DEVICE pic16F628A, INTRC_OSC_NOCLKOUT, MCLR_OFF
CMCON = 7
TRISA = 0 ' Make them all outputs
TRISB = 0

Counter var Byte

SubRoutine:

PORTA = PORTA | % 00000111
PORTB = PORTB | % 00000111
Pause 600

PORTA = PORTA & % 11111000
PORTB = PORTB & % 11111000

pause 100

Goto SubRoutine


a scheme of your circuit really would be welcome ...

Alain

Bracer
- 30th August 2010, 00:44
I am getting a


WARNING Line 20: Bad token "%".
WARNING Line 21: Bad token "%".
WARNING Line 24: Bad token "%".
WARNING Line 25: Bad token "%".
WARNING Line 24: 11111000 Numeric overflow, value truncated. (Test.pbp)
WARNING Line 25: 11111000 Numeric overflow, value truncated. (Test.pbp)

When I copy and paste the code your exact codes.


You are right Acetronics, I am using a +6V, but I did try the manual "High PortA.0" way to manually set each pin and it works.
Every single pins are all functional, so it couldn't have been a pin damage issue.

The schematic is really empty, Pin 5 to the negative and Pin 14 to the positive,
PortB.0 ~ PortB.2 to LEDs.
PortA.0 ~PortA.2 to LEDs, that's all.

rsocor01
- 30th August 2010, 02:29
I am getting a


WARNING Line 20: Bad token "%".
WARNING Line 21: Bad token "%".
WARNING Line 24: Bad token "%".
WARNING Line 25: Bad token "%".
WARNING Line 24: 11111000 Numeric overflow, value truncated. (Test.pbp)
WARNING Line 25: 11111000 Numeric overflow, value truncated. (Test.pbp)

When I copy and paste the code your exact codes.


You are right Acetronics, I am using a +6V, but I did try the manual "High PortA.0" way to manually set each pin and it works.
Every single pins are all functional, so it couldn't have been a pin damage issue.

The schematic is really empty, Pin 5 to the negative and Pin 14 to the positive,
PortB.0 ~ PortB.2 to LEDs.
PortA.0 ~PortA.2 to LEDs, that's all.

There is no space betweeen the % and the binaty number; Example: %00000111

Robert

Acetronics2
- 30th August 2010, 09:28
Hi, Bracer

sorry for the "space" ...

For your leds ... place a series resistor : 220 Ohms as a minimum @ 6v supply ...

I know some do not use resistors ... but it creates RMW issues !!!

You MUST keep current inside the specified limits ( ABSOLUTE limits: 25 ma - 100 mA per full port ) , but do not rely on the internal limitation ... or you'll get surprises !!!

Alain

Bracer
- 30th August 2010, 14:52
WOW Acetronics....
You are so cool!



Make PortA and PortB's 0~2 lights up.
PORTA = PORTA | %00000111
PORTB = PORTB | %00000111

Make PortA and PortB's 0~2 lights Down.
PORTA = PORTA & %11111000
PORTB = PORTB & %11111000

Completely works...wow...in just one line you have set 3 lights from PortB to light up...

It suddenly makes this whole "High PortB.0" look barbaric!
I don't exactly know "How" you set it, but I did try to experiment, I went ahead and try to make the light goes from the outside two lights to the inside one light, basically:


High PortB.0
High PortB.2
Low PortB.1

Pause 500

Low PortB.0
Low PortB.2
High PortB.1

Pause 500
Low PortB.0
Low PortB.2
Low PortB.1

Pause 500


But me...trying to "copy" your way, not knowing what I am doing, do this:

PORTA = PORTA | %00000101
PORTB = PORTB | %00000101
Pause 500

PORTA = PORTA & %00000010
PORTB = PORTB & %00000010

pause 500
Let's just say the lights acted weird, I have no idea what "|" in the code does either than it's probably "Or" ?
Not to mention "&" either than C's "And" ?

But they are single and not in pairs...some sort of bit shifters ?

Must trying to understand this concept...one line to control all Port Outputs....so powerful...but so mysterious to me...

Anyway thanks Acetronics, I use a 470 ohm for the LEDs under +6V :)

Acetronics2
- 30th August 2010, 19:39
But me...trying to "copy" your way, not knowing what I am doing, do this:



PORTA = PORTA | %00000101
PORTB = PORTB | %00000101
Pause 500

PORTA = PORTA & %00000010
PORTB = PORTB & %00000010

pause 500


PORTA = PORTA | %00000101 = Read PORTA and set to 1 ports A0 and A2

PORTA = PORTA & %00000010 = Read PORTA and clear ports A0 and A2

you use " | " ( OR ) to SET portspins and & ( AND ) to clear them ... but no mix allowed

you can try ...



PORTA = PORTA | %00000101
PORTB = PORTB | %00000101
.
.
.
.

Subroutine:

PORTA = PORTA ^ %00000111 ' FLIPS the bits with a " 1 "
PORTB = PORTB ^ %00000111

pause 500
GOTO Subroutine



see your manual " Bitwise Operators " section $4.16.17 ;)

Alain

Bracer
- 31st August 2010, 09:40
Thank you Acetronics, thanks to you, I now understand everything I need to understand for my Build from scratch Tricorder Project.

This is the note I have compiled based on what you have explained.


'Make them all outputs, Remember Pin 4, PortA.5 will always be Input Only
'And Pin 3, PortA.4 needs a pull up resistor.

'Understand that these two lines will make High, all Pins
'meaning the LEDs will ALL light up.
TRISA = 0 'Set all PortA Pins as Outputs
TRISB = 0 'Set all PortB Pins as Outputs

'Make All PortA and PortB's lights Down, the "&" here means "Clear".
PORTA = PORTA & %00000000
PORTB = PORTB & %00000000

Make PortA and PortB's .0 lights up, the "|" here means "Set".
PORTA = PORTA | %00000001
PORTB = PORTB | %00000001

[This is a powerful concept Bracer, with this you no longer need to do the:
High PortB.0 crap.
A single line to clear all and a single line to set all, the ONLY THING YOU MUST
take note is to line up your LEDs from PortB.7 to PortB.0 in your device instead
of 0 to 7, this is because as you can see, this one line code is backward, setting
the last digit to 1 actually set Port*.0 and %10000000 sets Port*.7 to high.
So to make it easier for you to visualize your
lights animations [I'm repeating myself here], set your LEDs on your device from
Port*.7 [Or whatever is the highest port number you use for output] to lowest.
]

[This also means that setting:
PORTA = PORTA | %00100000
is useless because PortA.5 [Pin 4] can only be use for input detection,
setting it to high means nothing.
]

You can see my Tricorder project here:
http://www.bracercom.com/wip/content/tricorder/index.html

It's still very much in WIP but I will be sure to give you credit when I've come to the electronic part of the Tricorder.

Acetronics2
- 2nd September 2010, 12:21
You can see my Tricorder project here:
http://www.bracercom.com/wip/content/tricorder/index.html

It's still very much in WIP but I will be sure to give you credit when I've come to the electronic part of the Tricorder.

Hi, Bracer

May be ... you should have a look to NOKIA graphic screens ...costless and ... marvellous color effects ...:)

Alain

Bracer
- 3rd September 2010, 00:09
NOKIA graphic screens with 16F628a ?

Wow...I have like no idea how...

I wouldn't even know how to begin with that.

Archangel
- 5th September 2010, 01:55
Try this Google link to search the forum, I think it has been done in here already.
http://www.google.com/custom?hl=en&cof=AH%3Aleft%3BS%3Ahttp%3A%2F%2Fwww.picbasic.co.u k%2Fforum%3BL%3Ahttp%3A%2F%2Fwww.crownhill.co.uk%2 Flogo.gif%3BLH%3A37%3BLW%3A174%3B&domains=picbasic.co.uk&q=&btnG=Search&sitesearch=