Connect with us

7 specific voltages to display 1,n,2,3,4,5,6 in 7 Segment Display

Discussion in 'Electronic Basics' started by [email protected], Feb 8, 2006.

Scroll to continue with content
  1. Guest

    This is kind of a repost, someone was extemely helpful in getting me
    code but it was for 6800 assembler.......

    Here is what I got.

    My motorcycle has an output voltage on the gear position sensor as
    1st gear = 1.782v
    2nd gear = 2.242v
    3rd gear = 2.960v
    4th gear = 3.630v
    5th gear = 4.310v
    6th gear = 4.660v
    Neutral = 5.000v

    I am awaiting a PIC16F873A and programming board, that should arrive

    What would the code be in order to take the voltage on an analog input
    and convert it to the pinouts on the output side to display my various
    numbers on a 7 segment LED display. I currently program in VBA so
    understand the logic, jsut not sure about the specific syntaxes and

    Just like the link below, however I am only going to use a 7-segment
    for simplicity. Maybe later I will try a 15 segment. I also will have
    no reverse input and no button to reprogram as I am going to make the
    code accept a reange of voltage for a given gear.

    Any help would be appreciated, as I am quit new to this.
  2. Byron A Jeff

    Byron A Jeff Guest

    [Analog voltages snipped]
    What kind of programmer did you get? Where did you pick up your
    parts? If you have an ongoing relationship with PIC style parts then
    Glitchbusters is a good reasource. Randy Jones is a good guy with
    good prices. You can find him at
    I have a piece of code that has many of the elements that you need.
    It's a sunrise/sunset outdoor light controller with a pot/switch
    input interface and a 7 segment LED display. So it has code elements
    for setting and reading the ADC, converting hex to 7 segment and
    managing multiple displays. You can find it here in my projects
    section of my PIC page:

    One additional piece of advise that I have for you is to take some
    time and learn how to use the linker. My code was written in absolute
    assembler several years ago. Relocatable code is easier to write and
    manage. But it does take a bit of reading to get started. Take a read
    of the MChip tutorial on its linker while you wait for your stuff:

    Hope these resources help.

  3. You begin the with core initialization. This involves setting the
    internal choices for all things that you cannot alter on the fly with
    program instructions, like clock choices, but must be set at the time
    of program load. You will have to study the data sheet for the
    particular PIC you have chosen.

    The program will probably begin with I/O initialization. Each pin has
    many possible functions, so you have to set up each pin to do the
    things you need them to do. There may also be some other choices you
    can make that remain in effect, like which analog input the internal
    A/D converter will be connected to through the selection mux (if you
    will be digitizing only one input signal).

    Then the code drops into the timing loop it will remain in as long as
    it is powered. Inside that loop, the code might:

    Start the analog to digital conversion for the voltage input, and
    waits for the conversion to complete.

    Run the output of the conversion through a recirculating filter to
    smooth the noise. Something like
    Result = (New input + Last Result * factor)/(1 + factor)
    If factor is 100, then it takes 100 cycles for the Result to get half
    way from where it started to where New input is.

    Run through a string of comparisons between Result and a string of
    constants that represent all the boundaries for the gear number,
    jumping out to one of 8 display driver sections, when the comparisons
    have learned which one to use.

    E.g. If result is greater than 4.7 (or the binary number out of the
    A/D converter that represents that voltage) go to Display "N".
    Else, if result is greater than 4.55, go to Display "6".
    If Result is greater than 2, jump to display "2", else Display "1".

    Each Display branch turns on the outputs to light the right segments
    and turns off all the others, and ends in a go to start of the loop to
    repeat the process.
  4. Guest

    Ok I have been muddling my way through this and have got a semi basic
    programming util and simulator

    here is the code

    Symbol ad_action = ADCON0.GO_DONE 'set new name for A/D conversion
    start bit
    Symbol display = PORTB 'set new name for PORTB used to display the
    conversion results
    Symbol adcvar = ADRESH 'variable for analog
    TRISB = %00000000 'set PORTB pins as outputs
    TRISA = %111111 'set PORTA pins as inputs
    ADCON0 = 0xc0 'set A/D conversion clock to internal source
    ADCON1 = 0 'set PORTA pins as analog inputs
    High ADCON0.ADON 'turn on A/D converter module
    Gosub getadresult 'go to conversion routine
    If adcvar >= 248 Then display = 84 'neutral
    If adcvar <= 247 And adcvar <= 230 Then display = 125 '6th Gear
    If adcvar <= 204 And adcvar >= 229 Then display = 109 '5th Gear
    If adcvar <= 169 And adcvar >= 203 Then display = 102 '4th Gear
    If adcvar <= 134 And adcvar >= 168 Then display = 79 '3rd Gear
    If adcvar <= 104 And adcvar >= 133 Then display = 59 '2nd Gear
    If adcvar >= 102 Then display = 6 '1st Gear
    Goto main 'repeat forever
    getadresult: 'conversion routine
    High ad_action 'start the conversion
    While ad_action 'wait until conversion is completed

    so can anyone tell me why I only get "1" or "n" when I adjust my analog

    I am using the 16F873A, and their is a little slider I can adjust my
    input to and I can only get the two gears, whic happen to be the bottom
    voltage and the top voltage.

    I thought that I could figure it out
  5. as everyone must...
    If you have set the A/D up to left justify the result (8 most
    significant bits in ADRESH and least two significant bits in ADRESL)
    then this will put an 8 bit result in adcvar.
    Some of your < and > are reversed. And you sometimes test the high
    value first and sometimes the low value. A number can't ever be lower
    than the lower limit and at the same time be higher than the higher
    limit, so those tests never produce a display.
    You would, eventually. When programming at such a basic level, you
    either end up very attentive to details or you go into another line of
    work. ;-)
  6. Guest

    Just so everyone knows, it works!!!!

    here it is;

    Symbol ad_action = ADCON0.GO_DONE 'set new name for A/D conversion
    start bit
    Symbol display = PORTB 'set new name for PORTB used to display the
    conversion results
    Symbol adcvar = ADRESL
    TRISB = %00000000 'set PORTB pins as outputs
    TRISA = %111111 'set PORTA pins as inputs
    ADCON0 = 0xc0 'set A/D conversion clock to internal source
    ADCON1 = %10000010 'set PORTA pins as analog inputs
    High ADCON0.ADON 'turn on A/D converter module
    Gosub getadresult 'go to conversion routine

    If adcvar >= 248 Then
    display = 84 'neutral
    Goto main
    If adcvar >= 230 Then 'display = 125 '6th Gear
    display = 125
    Goto main
    If adcvar >= 204 And adcvar <= 229 Then 'display = 109 '5th Gear
    display = 109
    Goto main
    If adcvar >= 169 And adcvar <= 203 Then 'display = 102 '4th Gear
    display = 102
    Goto main
    If adcvar >= 134 And adcvar <= 168 Then 'display = 79 '3th Gear
    display = 79
    Goto main
    If adcvar >= 104 And adcvar <= 133 Then 'display = 91 '2nd Gear
    display = 91
    Goto main
    If adcvar <= 103 Then
    display = 6 '1st Gear
    Goto main

    Goto main 'repeat forever
    getadresult: 'conversion routine
    High ad_action 'start the conversion
    While ad_action 'wait until conversion is completed

    thanks to all
  7. (snip)
  8. Guest

    it was fun, one last question,

    I am asumming that the 16f873a is common cathode?
  9. Byron A Jeff

    Byron A Jeff Guest

    It's neither, because common cathode applies to LED displays.

  10. Displays are common cathode or common anode. Micros are just
    programmable I/O. However, they are not quite symmetrical. Most
    micros (PICs included) pull down better than they pull up, so if you
    want to drive an LED directly with the outputs, it is better to use a
    common anode display, with the outputs active low and a separate
    current limit resistor between each output and each of the LED segments.

    However, if you want to add a buffer transistor between outputs and
    LEDs, output active high is more common, because n-channel mosfets
    like BS160 or 2N7000 are very common buffers which can be directly
    connected as high output current drivers. But since the transistors
    invert the outputs, you still use a common anode LED display.
  11. Guest

  12. I think you are referring to:

    This schematic shows a common anode display (note the single power
    connection to +) and either a 68 or 47 ohm resistor between the
    segment and the processor output. One of the notes at the bottom says
    that + means positive 5 volts from the regulator. If we assume the
    red LEDs drop about 1.5 volts, then the current is limited to no more
    than (5-1.5)/68=51mA or (5-1.5)/47=74mA. Those are pretty high
    segment currents, but in full sunlight, an LED has to be very bright
    to be visible. The big question is, how much voltage drop do each of
    the processor outputs have while carrying something close to this
    amount of current, and is the processor negative supply pin rated for
    the total of all segments' current (that will be on at one time)?

    From the notes at the bottom, the output drop must be a significant
    part of 5 volts, since they say that these resistor values are
    intended to put the output current not greater than 18 mA.

    You will have to refer to the PIC16F873 data sheet to find out these
    two values (output pull down effective resistance and power pin
    current limit). You may also have some trouble finding LED digit
    displays rated for current this high. A better solution might be to
    find one that is rated for lower current, but has very high
    efficiency, for sunlit operation.

    There is another problem with passing such large currents through the
    PIC power pins. It alters the internal supply voltage compared to the
    external rails. So it distorts the voltage measurements made by the
    A/D converter (unless you configure the A/D to use one or two of the
    analog input pins as references for the converter, and connect them to
    the supply rails. I prefer to buffer such currents with external
    transistors like the BS170 or 2N7000. One per segment as all the
    parts that are added. But you will have to raise the current limiting
    resistors (and they will be in much better control of the current),
    because these larger devices will drop very little voltage when on.
    Their on-resistance is on the order of 2 ohms.
  13. Guest

  14. Guest

  15. Bob Monsen

    Bob Monsen Guest

    A 2N3906 is a PNP transistor. Search on your link for 2761617, which are
    NPN transistors.

    You'll also need current-limiting resistors for these. Use a 1k resistor
    between the PIC port and the transistor base pin.

    2N3904 transistors have pinouts of EBC, meaning that if you hold them with
    the flat side towards you and the pins hanging down, then the emitter will
    be on the left, the base will be in the middle, and the collector will be
    on the right.

    Note that the link that JP posted is a common anode device, I think, so to
    turn on a segment, you pull it to ground. The resistor values should still
    be used, but instead of terminating at the PIC, terminate at the
    collector, and connect the emitter to ground. Then, instead of setting the
    port to 0 to turn on the segment, you set the port to 1, which turns on
    the transistor, which pulls its collector to 0V.

    Bob Monsen

    We should take care not to make the intellect our god; it has, of
    course, powerful muscles, but no personality.
    Albert Einstein
  16. Guest

    Sorry, didn't see those ones(3904)

    So, to recap, My PIC output goes to the base(B), and should be set in
    my code to put out a 1 on a the specific pin(s).(which it is)
    The collector(C) goes to the 1k resistor(s) and then to the segment(s).
    Then the emitter(E) goes directly to ground.

    Sound right?
  17. Byron A Jeff

    Byron A Jeff Guest

    The problem is that these current values are well above the PIC's maximum
    ratings both per pin (20 mA) and per port (200 mA). There is going to have
    to be some type of buffer between the PIC pins and the LED to support that
    current load.
    It's out of spec, so the processor outputs can drop as must as they like
    or more likely burn out.

    No the processor's negative supply pin is not rated for that current load.

    This is clearly a case of a novice design that did not take current limits
    into consideration.

    I believe that's a miscalculation. Your values at the top of this post
    are much more inline with typical LED values.

    Not sure of the second. But as stated on the page, the PIC has absolute
    maxmimums of 20/25 mA.

    Simply put the 68/47 are not the correct values for the resistors. I would
    never ever put any LED current limiting resistor of less that 200 ohms on a
    PIC output. If the brightness is insufficient then some type of buffer is
    required. NPN transistors with 470 ohm or so base resistors should be well
    more than stiff enough to drive the LEDs. Of course you'd need a dozen of
    them added to the circuit.

    Very true. Or you may need to cycle the display in order to support the
    higher current load.

    All right on point. While the given circuit has simplicity to it, it
    simply doesn't take into account the realities of the massive amounts of
    current you are proposing to transfer through it.

  18. Byron A Jeff

    Byron A Jeff Guest

    Not a problem.

    Via a base resistor. The value isn't too critical. It needs to be low
    enough to make sure that the transistor turns on completely, but not
    too low in order to exceed the PICs maximum ratings. The previous poster
    suggested 1K resistors. This is quite sufficient for the task because it
    limits to the PIC current to 5mA and with a hfe of at least 20 it'll be
    able to sink well over 100 mA of current.

    Also note that you'll need your present current limiting resistors between
    the LED cathodes and the collectors of the transistors.

    BTW that's the maximum current ratings for the LEDs. with your 68/47 combos
    you'll be pulling over 50 mA. Are they rated for continuous use at 50-70mA?
    If not you'll have to up the resistor value until the current drops to a
    safe rating. The current passing through the LED current limiting resistor is

    Iled = (Vin - Vled) / R

    So as John showed, with a 5V input, an LED voltage drop of 1.5V and a 68
    ohm resistor, the LED current is

    (5V - 1.5V) / 68 ohms = 51.47 mA

    Most LEDs only have a max continous current rating of 20-25mA. This 51 mA
    value well exceeds that. On your page you state 18 mA. How did you compute
    that value?
    No. You need two resistors per LED now. The 1K resistor goes between
    the PIC output pin and the base of the transistor. It limits the
    base-emitter current to 5 mA @ 5V. The second is your current LED
    current limiting resistors, which goes between the LED cathode and the
    collector of the transistor. I would suggest testing with resistor values
    of about 220 ohms, which limits the current through the LED segments to a
    safe: (5V - 1.5V)/220 = 15.9 mA. If it turns out that's too dim, then you
    can rethink how to address that issue later.
    After adjustments ;-)

  19. Guest

    Awesome, clear now, thanks BAJ
  20. Guest

    Awesome, clear now, thanks BAJ
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day