Simulating LEDs

LED Maestro

Senior Member
Hello,
Bit of a daft question I know. In the sim' I have some code to cycle through 16 leds two at a time but the sim goes one after the other if you see what I mean. Here is the code in question. I know it's a bit cumbersome, please forgive me.

Code:
main:high c.3, c.4
low c.3, c.4
high c.2, c.5
low c.2, c.5
high c.1, c.6
low c.1, c.6
high c.0, c.7
low c.0, c.7
high b.0, b.7
low b.0, b.7
high b.1, b.6
low b.1, b.6
high b.2, b.5
low b.2, b.5
high b.3, b.4
low b.3, b.4 goto main
Any suggestions would be greatly appreciated
Many thanks
 

westaust55

Moderator
You have no pause command between each step so yes it will (even in the PE) be quite fast.
The PE does have the option (under options/simulation I recall) to slow down a simulation. Alternatively use the single step mode in the PE.

Even for actual operation you will need pauses as each command only take approx 0.25 msec to execute at 4 MHz and less at faster clock speeds.
 

Paix

Senior Member
@LED Maestro,
In the sim' I have some code to cycle through 16 leds two at a time but the sim goes one after the other
I have looked at your code and have assumed that you are using a 28X2, to get two clear ports of 8 pins each for output. If I'm wrong, then I'm a bad guesser. I'm sure that you will be able to work around it or put me right if it is a significant oversight.

I see the pattern that you are looking for and have duplicated it, so if it isn't right, then it's probably your fault :p

I think that in the simulator, there are significant delays (I'm not sure if you have a degree of control of how fast it runs) and as your pins were being set serially, then this was reflected in the staggered setting of each pin of the selected pair.

If I was a little cleverer and had more time, I would take the four bits and attempt to work out a loop for the progression 1, 2, 4, 8 flip it and multiply by 8. Even Algol-68 had some interesting features :)

This has to be considered pseudo code to illustrate some of my thinking and don't expect it to work

Code:
#picaxe 28X2
#terminal 4800

symbol MOM = 100 ; milliSeconds
dirsC = %11111111 ; all pins port set as C outputs
dirsB = %11111111 ; all pins port set as B outputs

pinsC = %00000000 ; set port C pins all low
pinsB = %00000000 ; set port B pins all low

do
    pause MOM 
    for b0 = 1, 2, 4, 8  ; %0001, %0010, %0100, %1000
        b1 =flip b0        ; %1000, %0100, %0010, %0001
        b2 = b1 * 8        ; %10000000, %01000000, %00100000, %00010000
        b3 = b2 + b0      ; %10000001, %01000010, %00100100, %00011000
        pinsC = b3         ; light up the port C LED pattern
        pause MOM        ; delay 100mSeconds
    next

    for b4 = 8, 4, 2, 1
        b5 = flip b4
        b6 = b5 * 8
        b7 = b6 + b4
        pinsB = b7
        pause MOM
    next
loop

loop

This is a little more mundane, but should work just fine.
The binary notation is used to illustrate the working.[/B]
Code:
#picaxe 28X2
#terminal 4800

symbol MOM = 100  ; moment is 100mS
dirsB = %11111111 ; all pins port set as C outputs
dirsC = %11111111 ; all pins port set as B outputs

pinsC = %00000000 ; port C all pins low
pinsB = %00000000 ; port B all pins low

main:
	do
		pinsC = %00011000 ; C3 C4 high
		pause MOM
		pinsC = %00100100 ; C2 C5 high
		pause MOM
		pinsC = %01000010 ; C1 C6 high
		pause MOM
		pinsC = %10000001 ; C0 C7 high
		pause MOM
		pinsB = %10000001 ; B0 B7 high
		pause MOM
		pinsB = %01000010 ; B1 B6 high
		pause MOM
		pinsB = %00100100 ; B2 B5 high
		pause MOM
		pinsB = %00011000 ; B3 B4 high
		pause MOM
	loop
Sorry Dippy, I don't seem to do clarity or brevity :)
All untested of course.
 

hippy

Technical Support
Staff member
@ LED Maestro : I think what you are seeing is a simulation effect. "high c.3, c.4" is actually two high commands, one after the other "high c.3 : high c.4" and simulation will show that when stepping through the code. In practice the PICAXE runs so quickly that both will appear to turn on simultaneously.

If you really want both LED's to turn on simultaneously you need to use "pins=" as Paix suggests but most often your original code will be suitable.

You will however need to put PAUSE commands between the steps or on a real PICAXE things will be set then cleared so quickly you won't see the LED's being set sequentially, it will all be a dim blur.
 

LED Maestro

Senior Member
Thanks for the suggestions. I will have a play around in the sim and put the pauses in. Trial and error I think is the phrase.
Thanks again
 

John West

Senior Member
I certain cases (depending on voltage drop across the LED's or available voltage) one could just hardwire the LED pairs in series. That would solve any timing problem and keep the code very simple for the benefit of a code newbie.
 
Top