Update: Cleaned up logic so LEDs always get same current no matter how many are in use.
When you need to blink a lot of LEDs:
Here's the results of a current test using 32 LEDs on a 18F44K22 using internal oscillator at 64MHz.
Use byteActiveA-E variables to determine which bits you want toggled.Code:portLedA var PORTA ' Define Ports portLedB var PORTB portLedC var PORTC portLedD var PORTD portLedE var PORTE byteLoop VAR byte ' Define variables byteDelay VAR byte byteActiveA var byte ' Define active pins byteActiveB var byte byteActiveC var byte byteActiveD var byte byteActiveE var byte byteRXA var byte ' Define RX data byteRXB var byte byteRXC var byte byteRXD var byte byteRXE var byte portLedA = %00000000 ' Initialize Leds off portLedB = %00000000 portLedC = %00000000 portLedD = %00000000 portLedE = %00000000 byteActiveA = %11111110 ' Determine Active pins Pin A.0 not used byteActiveB = %11111111 byteActiveC = %00111111 ' RX / TX in use byteActiveD = %11111111 byteActiveE = %00000111 ' MCLR input only byteRXA = %11111110 ' RX data from Master PIC byteRXB = %11111111 byteRXC = %00111111 byteRXD = %11111111 byteRXE = %00000111 byteDelay = 35 ' Tweak this value to control current and frequency ' 1 LED active 668 Hz 2.7 mA LED 14.2 mA circuit ' 32 LEDs active 668 Hz 2.7 mA LED 99.0 mA circuit ' Using 2N2222 NPN transistor, 4K7 base with 680K pull-down, ' LED direct to collector from 5VDC. mainloop: For byteLoop = 0 to 7 ' Go through 8 bits if byteActiveA.0[byteLoop] = 1 then ' Check if pin is used portLedA.0[byteLoop] = byterxA.0[byteLoop] PAUSEUS byteDelay portLedA = %00000000 ' Faster than setting bit to 0 endif if byteActiveB.0[byteLoop] = 1 then portLedB.0[byteLoop] = byterxB.0[byteLoop] PAUSEUS byteDelay portLedB = %00000000 endif if byteActiveC.0[byteLoop] = 1 then portLedC.0[byteLoop] = byterxC.0[byteLoop] PAUSEUS byteDelay portLedC = %00000000 endif if byteActiveD.0[byteLoop] = 1 then portLedD.0[byteLoop] = byterxD.0[byteLoop] PAUSEUS byteDelay portLedD = %00000000 endif if byteActiveE.0[byteLoop] = 1 then portLedE.0[byteLoop] = byterxE.0[byteLoop] PAUSEUS byteDelay portLedE = %00000000 endif Next byteLoop Goto mainloop ' Go back to loop and blink LEDs forever
Use byteRXA-E variables to receive bits from a Master PIC.
Use byteDelay variable to tweak current and LED brightness.
For my testing, MCLR (can only be input), RX/TX (needed) and A0 (unused) were disabled.
Robert
![]()





Bookmarks