You want them all to come on at the same time and drop out at different rates?
Something like that?Code:PORTB = %11111111 PAUSE XXX PORTB = %11111110 PAUSE XXX PORTB = %11111100 ... LOOP OVER...
You want them all to come on at the same time and drop out at different rates?
Something like that?Code:PORTB = %11111111 PAUSE XXX PORTB = %11111110 PAUSE XXX PORTB = %11111100 ... LOOP OVER...
Dave
Always wear safety glasses while programming.
Have as many LED's as you have pins...
Homework Done! Melanie's off down the pub...Code:LoopALED var BYTE LoopBLED var BYTE LoopCLED var BYTE LoopDLED var BYTE LoopELED var BYTE LoopFLED var BYTE LoopGLED var BYTE LoopHLED var BYTE ' LoopACON con 50 LoopBCON con 22 LoopCCon con 38 LoopDCon con 75 LoopECon con 17 LoopFCon con 40 LoopGCon con 62 LoopHCon con 13 ' TRISB=0 Loop: If LoopALED=0 then LoopALED=LoopACON Toggle PortB.0 else LoopALED=LoopALED-1 endif ' If LoopBLED=0 then LoopBLED=LoopBCON Toggle PortB.1 else LoopBLED=LoopBLED-1 endif ' If LoopCLED=0 then LoopCLED=LoopCCON Toggle PortB.2 else LoopCLED=LoopCLED-1 endif ' If LoopDLED=0 then LoopDLED=LoopDCON Toggle PortB.3 else LoopDLED=LoopDLED-1 endif ' If LoopELED=0 then LoopELED=LoopECON Toggle PortB.4 else LoopELED=LoopELED-1 endif ' If LoopFLED=0 then LoopFLED=LoopFCON Toggle PortB.5 else LoopFLED=LoopFLED-1 endif ' If LoopGLED=0 then LoopGLED=LoopGCON Toggle PortB.6 else LoopGLED=LoopGLED-1 endif ' If LoopHLED=0 then LoopHLED=LoopHCON Toggle PortB.7 else LoopHLED=LoopHLED-1 endif ' Pause 10 Goto Loop
Now repeat the above (excluding definitions) in less than 8 Lines of Code... (yes it can be done)!
mack, the LEDs need to blink all at the same time, so that will not work.
Mel, use arrays and a for-next loop - along these lines?
Code:Loop: for x = 0 to 7 If LoopLED[x] = 0 then LoopLED[x] = LoopCON[x] Toggle PORTB.x else LoopLED[x] = LoopLED[x]-1 endif next x Pause 10 Goto Loop
Yes, pretty much, but I don't think PortB.x will work, so...
Where ShadowPort is a BYTE.Code:ShadowPortB=0 Loop: for x = 0 to 7 If LoopLED[x] = 0 then LoopLED[x] = LoopCON[x] ShadowPortB.0(x)=ShadowPortB.0(x)^1 else LoopLED[x] = LoopLED[x]-1 endif next x PortB=ShadowPortB Pause 10 Goto Loop
Just to be different ...
And because she said "less than 8 Lines of Code" ...
Code:TRISB = 0 Main: x = (x + 1) // 8 PORTB.0(x) = !(LoopLED(x) < LoopCON(x)) LoopLED(x) = (LoopLED(x) + 1) // (LoopCON(x) << 1) if x=7 THEN PAUSE 10 GOTO Main
DT
OUCH!
After running my loop in the SIM.
I realized the timing was Waaaayyyyy OFF.
Fortunately, the other examples were "OFF" too ... just not as far.
I've always said PAUSE is the worst statement in the BASIC language.
It got me again.
Here's a version that should compensate ...
It uses the CCP module in "Compare" mode to create a precise 10mS interval.
So when you give a delay value of 50, it actually takes 500mS, on the nose.
Sorry, it's more than 8 lines now.
Cheers,Code:DEFINE OSC 4 DEFINE BLINKYFREQ 100 ; 10mS periods DATA 50,22,38,75,17,40,62,13 ; default periods for each Output ;---------------------------------------------------------------- CCPR1val CON EXT : @CCPR1val = (OSC*1000000/4)/ BLINKYFREQ CCPR1 VAR WORD EXT : @CCPR1 = CCPR1L Timer1 VAR WORD EXT : @Timer1 = TMR1L CCPIF VAR PIR1.2 LoopLED VAR BYTE[8] LoopCON VAR BYTE[8] x VAR BYTE ;----[Initialize]------------------------------------------------ FOR x = 0 to 7 ; load the periods from EEPROM READ x, LoopCON(x) NEXT X ;-- setup CCP1 and Start Timer1 -- CCPR1 = CCPR1val ; set compare value CCP1CON = %00001011 ; compare mode, special event Timer1 = 0 ; clear Timer1 T1CON.0 = 1 ; start Timer1 TRISB = 0 ; PORTB all OUTPUT ;----[Main Program Loop]---------------------------------------- Main: x = (x + 1) // 8 PORTB.0(x) = !(LoopLED(x) < LoopCON(x)) LoopLED(x) = (LoopLED(x) + 1) // (LoopCON(x) << 1) IF x != 7 THEN Main Waiting: IF !CCPIF THEN Waiting CCPIF = 0 GOTO Main
Last edited by Darrel Taylor; - 26th April 2010 at 04:59. Reason: Minor modifications
DT
Darrel, that algorithm is slick!
Does PBP allow assignments in expressions like C? For example;
Code:while(1) { x = (x+1)&7; portb ^= ((!(LoopLED[x]=(LoopLED[x]+1)%LoopCON[x])) << x); delay_us(2500); }
Bookmarks