7 Segment Display

Discussion in 'Embedded Systems and Microcontrollers' started by harami, Jan 17, 2012.

  1. harami

    Thread Starter Member

    Jun 20, 2011
    66
    0
    im trying to write a code in either C or ASM which will give an output to display a number on LED display. as an input im having two push buttons (Button 1 and Button 2). if B1 is pressed followed by B2 the number increases and shown on LED display and if B2 is pressed followed by B1 number decreases until it show 0 on LED display.

    im new to programming so im really struggling. can any one help me on writing this programme. PLEASE HELP ME!!!
     
  2. RRITESH KAKKAR

    Senior Member

    Jun 29, 2010
    2,831
    89
    No information provided on which you will be implementing this??
     
  3. harami

    Thread Starter Member

    Jun 20, 2011
    66
    0
    any chance you can share your code with me or help me write the programme... PLEASE
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    First you must tell us which microcontroller you plan to use. And if you plan to write in C. Some hints on which C compiler you plan to use will be helpful. As it stands now. nobody will be able to help you. Remember good questions draw good answers
     
  5. harami

    Thread Starter Member

    Jun 20, 2011
    66
    0
    I will be using PIC18F452 and I am using MPLAB IDE v7.10. I have done my previous on Assembly language. Now i want to use C language to write this programme but i am struggling.
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Ok this is better regarding information. You say you struggle with C coding. Can you tell us what you have done so far (like posting your code). And also why and how you struggle
    Also before you post your code read this
    http://forum.allaboutcircuits.com/showthread.php?t=37117
     
  7. harami

    Thread Starter Member

    Jun 20, 2011
    66
    0
    this is what i have done so far.

    A single seven segment display connected to PORTD as
    a->PD7
    b->PD6
    c->PD5
    d->PD4

    e->PD3
    f->PD2
    g->PD1
    DP->PD0
    Code ( (Unknown Language)):
    1.  
    2. #include <avr/io.h>
    3. #include <util/delay.h>
    4.  
    5. #define SEVEN_SEGMENT_PORT PORTD
    6. #define SEVEN_SEGMENT_DDR DDRD
    7.  
    8.  
    9. void SevenSegment(uint8_t n,uint8_t dp)
    10. {
    11.  
    12.    if(n<10)
    13.    {
    14.       switch (n)
    15.       {
    16.          case 0:
    17.          SEVEN_SEGMENT_PORT=0b00000011;
    18.          break;
    19.  
    20.          case 1:
    21.          SEVEN_SEGMENT_PORT=0b10011111;
    22.          break;
    23.  
    24.          case 2:
    25.          SEVEN_SEGMENT_PORT=0b00100101;
    26.          break;
    27.  
    28.          case 3:
    29.          SEVEN_SEGMENT_PORT=0b00001101;
    30.          break;
    31.  
    32.          case 4:
    33.          SEVEN_SEGMENT_PORT=0b10011001;
    34.          break;
    35.  
    36.          case 5:
    37.          SEVEN_SEGMENT_PORT=0b01001001;
    38.          break;
    39.  
    40.          case 6:
    41.          SEVEN_SEGMENT_PORT=0b01000001;
    42.          break;
    43.  
    44.          case 7:
    45.          SEVEN_SEGMENT_PORT=0b00011111;
    46.          break;
    47.  
    48.          case 8:
    49.          SEVEN_SEGMENT_PORT=0b00000001;
    50.          break;
    51.  
    52.          case 9:
    53.          SEVEN_SEGMENT_PORT=0b00001001;
    54.          break;
    55.       }
    56.       if(dp)
    57.       {
    58.          SEVEN_SEGMENT_PORT&=0b11111110;
    59.       }
    60.    }
    61.    else
    62.    {
    63.         SEVEN_SEGMENT_PORT=0b11111101;
    64.    }
    65. }
    66.  
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,435
    3,360
    Will work but not efficient.
    Create and array of 10 values. The number n (0-9) is the index to the array.
     
  9. harami

    Thread Starter Member

    Jun 20, 2011
    66
    0
    i will have two inputs, B1 and B2 (push button 1 and 2). how do i add these two inputs on the programme.
     
  10. codehead

    Member

    Nov 28, 2011
    56
    11
    Hi harami...

    I'm not completely sure of what you want to do, from your explanation. "if B1 is pressed followed by B2 the number increases and shown on LED display and if B2 is pressed followed by B1 number decreases until it show 0 on LED display."—it's unclear whether if you mean that B2 is pressed and held, then you press B2, for instance...

    Either way, this is a great opportunity for you to learn programming, since it's a lot easier when you have something specific you want to solve. I suggest that you look at the problem in two pieces:

    1) solve the problem by making a variable increment up or down in response to the buttons

    2) create a display routine that accepts that variable and display the appropriate number

    That may already be obvious to you, but I just want to make sure that you're treating the calculation and the display of that calculation separately, conceptually and in your code.

    You showed you code for the display; that will work, but this is easier to write, read, and debug: use a table lookup instead of control logic to translate your value to a bit pattern for the segments. I assume that the "dp" is "decimal point")...

    I did this quickly, no testing and not even careful about the coding, but I hope you get the idea:

    Code ( (Unknown Language)):
    1.  
    2. void SevenSegment(uint8_t n,uint8_t dp)
    3. {
    4.   static const uint8_t segBits = { 0b00000011, 0b10011111, 0b00100101, 0b00001101, 0b10011001, 0b01001001, 0b01000001, 0b00011111, 0b00000001, 0b00001001, 0b11111101 };
    5.  
    6.   if (n > 10)
    7.     n = 10;
    8.  
    9.   SEVEN_SEGMENT_PORT = segBits[n];
    10.  
    11.   if ((n<10) && dp)
    12.     SEVEN_SEGMENT_PORT &= 0b11111110;
    13. }
    14.  
     
  11. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Code ( (Unknown Language)):
    1. #include <avr/io.h>
    2. #include <util/delay.h>
    3.  
    4.  
    These two lines reference paths that look like you are using an atmel AVR processor.

    What compiler are you using to write your program code?

    hgmjr
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I also noticed that detail. So Harami you must tell us all details on your project. Be also aware of that you can not just take code written for say Atmel micro controllers and use it as is in a MPLAB/PIC project. Code among different C-compilers for PIC micros are either not directly compatible. By the way MPLAB is not a C-compiler. It is more a user interface for PIC/Microchip micros. So in order to write C programs in MPLAB you must also have some C-compiler installed.
     
  13. harami

    Thread Starter Member

    Jun 20, 2011
    66
    0
    Code ( (Unknown Language)):
    1.  
    2. #include <p18f452.h>
    3. #include <delays.h>
    4.  
    sorry about the error. i used this same software (MPLAB) to write a programme in assembly language and i believe it is also compatible for C language. any ways i can check if it has already installed c-compiler? if it hasnt installed, can u tell me where can i install this c-compiler from?
     
  14. harami

    Thread Starter Member

    Jun 20, 2011
    66
    0
    what do u actually mean by creating a display routine and what are the calculations? :S
     
  15. codehead

    Member

    Nov 28, 2011
    56
    11
    Sorry, didn't mean to be confusing. It looks like that's what you're doing—you have a routine that accepts a value (and a decimal flag) to update the display.
     
  16. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    The PIC18F452 would need either the MPLAB C Compiler (formerly the C18 compiler) or the HI-TECH C Compiler for PIC18.

    Both have links here. You may need to register for the free downloads.
     
  17. raychar

    Member

    Nov 8, 2011
    82
    0
    Thinking that these may help.. the program need include If-cause that if B1 is pressed, taking a variable Sum, Sum=Sum++. If B2 is pressed, Sum=Sum-. It also needs to include that if Sum is larger than some values it should go back to some predefined values or some information to be displayed on 7 segment LED. Or, if Sum is below zero, it will display negative value etc.. Of cause, hex/binary digits need to be changed to decimal digits by something like look-up table (I don't know exact name??) before sending to LED display.
     
  18. harami

    Thread Starter Member

    Jun 20, 2011
    66
    0
    can anyone check if the code is right.
    if button 1 pressed followed by button 2, increament n by 1 and save.. and if button 2 is pressed followed by button 1, decrement n by 1 and save. Also display the value of n on the led display.

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #include <p18f4520.h>
    4. #include <delays.h>
    5.  
    6.     #pragma config OSC = HS                              
    7.     #pragma config WDT = OFF
    8.     #pragma config LVP = OFF
    9.      
    10.     #define btn_on     PORTBbits.RB4
    11.     #define btn_off    PORTBbits.RB5
    12.     #define led        PORTAbits.RA0
    13.      
    14.     #endif  /* HOST */
    15.      
    16.  
    17. void SevenSegment(uint8_t n,uint8_t dp)
    18. {
    19.   static const uint8_t segBits = { 0b00000011, 0b10011111, 0b00100101, 0b00001101, 0b10011001, 0b01001001, 0b01000001, 0b00011111, 0b00000001, 0b00001001, 0b11111101 };
    20.  
    21.   if (n > 10)
    22.     n = 10;
    23.  
    24.   SEVEN_SEGMENT_PORT = segBits[n];
    25.  
    26.   if ((n<10) && dp)
    27.     SEVEN_SEGMENT_PORT &= 0b11111110;
    28. }
    29.  
    30.  
    31.     void led( int state );
    32.     int  pressed( int button, int state );
    33.      
    34.     int main ( ) {
    35.         int btn1 = 0, btn2 = 0;
    36.      
    37.     #if !defined(HOST)
    38.         TRISA = 0;  // set Port A(LED) as output
    39.         n = 0;  
    40.     #endif
    41.      
    42.         for ( ; ; ) {
    43.             btn1 = pressed( 1, btn1 );
    44.             btn2 = pressed( 2, btn2 );
    45.          if ( btn1 && !btn2 ) {
    46.                 n( ++1 );
    47.             } else
    48.             if ( btn2 && !btn1 ) {
    49.                 n( --1 );
    50.             }
    51.         }
    52.     }
    53.  
     
Loading...