please dont rip this site

PIC Microcontoller Input / Output LED Methods

Driving an LED bargraph display

From Regulus Berdin

Given a number in bin, from 1 to 30, this code will drive that number of LED's on.

; 1-30 to 30 leds 
;input:   bin 
;output:  led1,led2,led3,led4 

        clrf    led1 
        clrf    led2 
        clrf    led3 
        clrf    led4 
        incf    bin,f 
        decfsz  bin,f           ;do not include 0 
         goto   $+2 

        decf    bin,f 
        setc                    ;turn on led 
        rlf     led4,f 
        rlf     led3,f 
        rlf     led2,f 
        rlf     led1,f 
        goto    loop 


I have another solution, using the fact that 1-30 bar is equal to 2^y - 
1.  This routine is isochronous and faster on some values than the 

Here goes: 

        clrf    a1 
        clrf    a2 
        clrf    a3 
        clrf    a4 

        movlw   a4              ;2^bin 
        btfsc   bin,3 
         movlw  a3 
        btfsc   bin,4 
         addlw  -2               ;corrected from 'movlw  a1' 
        movwf   FSR 

        movlw   B'00000001' 
        btfsc   bin,1 
         movlw  B'00000100' 
        movwf   INDF 

        btfsc   bin,0 
         addwf  INDF,f 

        btfsc   bin,2 
         swapf  INDF,f 

        movlw   1               ;subtract by 1 
        subwf   a4,f 
         subwf  a3,f 
         subwf  a2,f 
         subwf  a1,f 

You don't need a 30bit number just because you want to drive a 30led bar :)  All you need is a number from 0 to 30, which boils down to a simple division, if your representation is linear. 0->0%->no led, 65535->100%->all (30) leds. that's 30 intervals for 65535, hence each interval (corresponding to one led more) is 65535/30... you divide your load by this number, and get the number of leds to switch on.


file: /Techref/microchip/ledbargraph.htm, 2KB, , updated: 2009/2/13 16:22, local time: 2024/7/18 04:58,

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> PIC Microcontoller Input / Output LED Method s</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2024 contributors:
o List host: MIT, Site host, Top posters @none found
- Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.

Welcome to!