Menu

8 bit Binary to BCD code debugging

Help
2008-04-10
2013-05-30
  • Nobody/Anonymous

    Hey there.  I'm trying to write some code for a 16f877a PIC that converts an 8 bit binary input to 2 outgoing (the 2 least significant) bcd digits.  The code is shown below.  Basically I am taking the input and modding it by ten to get the least significant digit.  then subtracting that off and dividing by ten and modding again to get the tens digit.  I have some test code in there to control an led just to make sure that the thing was compiling.  All of my outputs are low all the time no matter what input I give.

    Did I do something wrong in the syntax?

    Thanks for your help up front.

    '8 bit binary to BCD

    '-------------------------------
    #define in0 PORTB.7
    #define in1 PORTB.6
    #define in2 PORTB.5
    #define in3 PORTB.4
    #define in4 PORTB.3
    #define in5 PORTB.2
    #define in6 PORTB.1
    #define in7 PORTB.0

    #define f0 PORTD.7
    #define f1 PORTD.6
    #define f2 PORTD.5
    #define f3 PORTD.4
    #define g0 PORTD.3
    #define g1 PORTD.2
    #define g2 PORTD.1
    #define g3 PORTD.0
    #define led PORTC.2
    '-------------------------------

    '-------------------------------
    'Set pin direction
    dir in0 IN
    dir in1 IN
    dir in2 IN
    dir in3 IN
    dir in4 IN
    dir in5 IN
    dir in6 IN
    dir in7 IN

    dir f0 OUT
    dir f1 OUT
    dir f2 OUT
    dir f3 OUT
    dir g0 OUT
    dir g1 OUT
    dir g2 OUT
    dir g3 OUT

    dir led OUT
    '-------------------------------

    #chip 16F877A, 4

    dim binIn as byte
    dim ones as byte
    dim tens as byte

    DO
        wait 500 ms

        binIn.0 = in0 
        binIn.1 = in1 
        binIn.2 = in2 
        binIn.4 = in4
        binIn.5 = in5
        binIn.6 = in6
        binIn.7 = in7
       
       
        ones = binIn % 10
        binIn = (binIn - (binIn % 10))/10
        tens = binIn % 10
        binIn = (binIn - (binIn % 10))/10
       
        f0 = ones.0
        f1 = ones.1
        f2 = ones.2
        f3 = ones.3
       
        g0 = tens.0
        g1 = tens.1
        g2 = tens.2
        g3 = tens.3
       
       
        wait 500 ms

    wait 1 s
    set led on
    wait 1 s
    set led off

    LOOP

     
    • kent_twt4

      kent_twt4 - 2008-04-10

      Thats not quite how I read the modulo function.  Perhaps use something like this:

      Huns = BinIn/100
      Tens = (BinIn % 100)/10
      Ones = BinIn % 10

      Also if you are going to light up some 7-segment leds there's already a library for that in the online help section.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.