1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

2 digit 7 segment display..

Discussion in 'Microcontrollers' started by overmind, Jan 15, 2008.

  1. overmind

    overmind New Member

    Joined:
    May 23, 2006
    Messages:
    55
    Likes:
    0
    hi everyone.. what im trying to do is that when i type a 1 or 2 digit no., it will appear on the 7 segment display.. i really cant figure out why its not working.. can anybody tell me whats wrong with my code? thanks..im using a 16f877.h pic.. here it is
    Code (text):


    #if defined(__PCM__)
    #include <16F877.h>
    #include <stdio.h>
    #include <stdlib.h>
    #fuses HS,NOWDT,NOPROTECT,NOLVP
    #use delay(clock=20000000)
    #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)  

    void findnumbers(int temp);
    void sendhex(int number, int digit);
    void displaynumbers(int tens, int ones);
    int getavgtemp();




    void main() {
      int temp;
       int i=0;
      set_tris_b(0);
     
      temp = 0;
      output_b (0x00);
      while(TRUE) {
        if(kbhit()) {
            temp=getc();
            delay_ms(500);
                findnumbers(temp);
        }
      }
    }




    // function to find number
    void findnumbers(int temp)
       {

       int tens = 0; // tens position
       int ones = 0; // ones position
       
       output_high(PIN_D0);
       if (temp < 100) // temp should never be over 100
       {
        tens = (temp/10)%(10);
        ones = (temp%10);
       }else{tens, ones = 2;}
       displaynumbers(tens,ones);
    }

    // this converts a number to the desired high/low combination
    // for the 7 seg led display
    void sendhex(int number, int digit)
       {
       output_high(PIN_D1);
       output_e (0x00);
       // send hex to port b to make number
       
       if (number == 0){output_b (0xC0);} // output 0 to port b
       if (number == 1){output_b (0xF9);} // 1
       if (number == 2){output_b (0xA4);}
       if (number == 3){output_b (0xB0);}
       if (number == 4){output_b (0x99);}
       if (number == 5){output_b (0x92);}
       if (number == 6){output_b (0x82);}
       if (number == 7){output_b (0xF8);}
       if (number == 8){output_b (0x80);}
       if (number == 9){output_b (0x90);}

       
       //turn on the digit(send high to common cathode via transistor)
       if (digit == 1){output_high(PIN_E0);}
       if (digit == 2){output_high(PIN_E1);}
       output_low(PIN_D1);
       
       }
       
    void displaynumbers(int tens, int ones)// send to led
       {
       int i = 0;
       output_high(PIN_D2);
       
       
       if(tens > 0)
       {
       sendhex(tens, 2);
       sendhex(ones, 1);
       }else{
       sendhex(ones, 2);
       }
       output_low(PIN_D2);
       }

     
     
  2. simrantogether

    simrantogether New Member

    Joined:
    May 22, 2006
    Messages:
    433
    Likes:
    4
    Location:
    Bangalore
    Well...

    Well,

    I suggest you to use IC 7447 or 7448 ... then your code will be as small as two or three lines...

    | microcontroller | --- > | 7447 | --- > | Seven Seg. |

    Regards,

    Simran..:)
     
  3. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    It's good practice to avoid using "glue" logic if you can. The 16F877 is a big chip, lots of I/O and can drive a seven-segment display.

    OP a schematic would be helpful.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. overmind

    overmind New Member

    Joined:
    May 23, 2006
    Messages:
    55
    Likes:
    0

    hi.. actually i just based my code on a post here somewhere.. however in his schematic diagram, he placed transistors between the input of the 7 segments going to the microcontroller. but i didnt placed any transistor in my project.. will it matter?
     
  6. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    Well the PIC can only sink or source 20 to 25ma. Generally the transistors are there to drive the CA or CC pins.
    [​IMG]
    Funny thing this schematic is from an 8051 site.
     
  7. overmind

    overmind New Member

    Joined:
    May 23, 2006
    Messages:
    55
    Likes:
    0
    hi.. i followed the schematic diagram and placed a temperature transducer at port A1. so what i want to do now is to display the temperature transducer in the two 7 segments.. however when i get a reading of 20 C. what happens is that it displays the digit 2 on the first 7 segment and the segments on the first 7 segment that did not light (segments f,c and dp) will light on the second 7 segment and then the digit 0 will be displayed on the first 7 segment and segments g and dp will light on the second seven segment.. its like it displays the digit 20 on two occasions.. can anybody tell me why is this happening? (i hope you understand the way i explain my problem..) please help..
     
  8. kchriste

    kchriste New Member Forum Supporter

    Joined:
    Jul 23, 2006
    Messages:
    3,677
    Likes:
    47
    Location:
    Victoria BC, Canada
    One problem is the following code:
    Code (text):
    //turn on the digit(send high to common cathode via transistor)
       if (digit == 1){output_high(PIN_E0);}
       if (digit == 2){output_high(PIN_E1);}
       output_low(PIN_D1);
    You really need to turn off one digit before turning on the other:
    Code (text):
    //turn on the digit(send high to common cathode via transistor)
       if (digit == 1){output_low(PIN_E1); output_high(PIN_E0);}
       if (digit == 2){output_low(PIN_E0); output_high(PIN_E1);}
       output_low(PIN_D1);
    BTW, what are you doing with PIN_D1?
     
    Last edited: Jan 15, 2008
  9. overmind

    overmind New Member

    Joined:
    May 23, 2006
    Messages:
    55
    Likes:
    0
    thanks for the reply.. actually, i have a customized kit.. and port d is connected to 8 LED's.. one for each pin..

    the code you gave me still doesn't solve my problem.. anymore ideas? please help.. thanks..
     
  10. overmind

    overmind New Member

    Joined:
    May 23, 2006
    Messages:
    55
    Likes:
    0
    btw.. im using two common anode 7 segments. will the figure above still be applicable or i have to create some modifications?
     
  11. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    Please post your schematic...
     
  12. penoy_balut

    penoy_balut New Member

    Joined:
    Sep 12, 2006
    Messages:
    171
    Likes:
    1
    Location:
    Bagong Silang
    The problem is the sendhex function.
     
  13. overmind

    overmind New Member

    Joined:
    May 23, 2006
    Messages:
    55
    Likes:
    0
    hi mike... i followed the one shown above..
     
  14. overmind

    overmind New Member

    Joined:
    May 23, 2006
    Messages:
    55
    Likes:
    0
    hi there.. can you please tell me whats wrong with my code? i really cant figure it out.. thanks..
     

Share This Page