7 Segment Display

Thread Starter

harami

Joined Jun 20, 2011
66
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!!!
 

t06afre

Joined May 11, 2009
5,934
any chance you can share your code with me or help me write the programme... PLEASE
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
 

Thread Starter

harami

Joined Jun 20, 2011
66
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.
 

t06afre

Joined May 11, 2009
5,934
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.
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
 

Thread Starter

harami

Joined Jun 20, 2011
66
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
Rich (BB code):
#include <avr/io.h>
#include <util/delay.h>

#define SEVEN_SEGMENT_PORT PORTD
#define SEVEN_SEGMENT_DDR DDRD


void SevenSegment(uint8_t n,uint8_t dp)
{

   if(n<10)
   {
      switch (n)
      {
         case 0:
         SEVEN_SEGMENT_PORT=0b00000011;
         break;

         case 1:
         SEVEN_SEGMENT_PORT=0b10011111;
         break;

         case 2:
         SEVEN_SEGMENT_PORT=0b00100101;
         break;

         case 3:
         SEVEN_SEGMENT_PORT=0b00001101;
         break;

         case 4:
         SEVEN_SEGMENT_PORT=0b10011001;
         break;

         case 5:
         SEVEN_SEGMENT_PORT=0b01001001;
         break;

         case 6:
         SEVEN_SEGMENT_PORT=0b01000001;
         break;

         case 7:
         SEVEN_SEGMENT_PORT=0b00011111;
         break;

         case 8:
         SEVEN_SEGMENT_PORT=0b00000001;
         break;

         case 9:
         SEVEN_SEGMENT_PORT=0b00001001;
         break;
      }
      if(dp)
      {
         SEVEN_SEGMENT_PORT&=0b11111110;
      }
   }
   else
   {
        SEVEN_SEGMENT_PORT=0b11111101;
   }
}
 

codehead

Joined Nov 28, 2011
57
i will have two inputs, B1 and B2 (push button 1 and 2). how do i add these two inputs on the programme.
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:

Rich (BB code):
void SevenSegment(uint8_t n,uint8_t dp)
{
  static const uint8_t segBits = { 0b00000011, 0b10011111, 0b00100101, 0b00001101, 0b10011001, 0b01001001, 0b01000001, 0b00011111, 0b00000001, 0b00001001, 0b11111101 };

  if (n > 10)
    n = 10;

  SEVEN_SEGMENT_PORT = segBits[n];

  if ((n<10) && dp)
    SEVEN_SEGMENT_PORT &= 0b11111110;
}
 

hgmjr

Joined Jan 28, 2005
9,029
Rich (BB code):
#include <avr/io.h>
#include <util/delay.h>
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
 

t06afre

Joined May 11, 2009
5,934
Rich (BB code):
#include <avr/io.h>
#include <util/delay.h>
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
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.
 

Thread Starter

harami

Joined Jun 20, 2011
66
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.
Rich (BB code):
#include <p18f452.h>
#include <delays.h>
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?
 

Thread Starter

harami

Joined Jun 20, 2011
66
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.

[/CODE]
what do u actually mean by creating a display routine and what are the calculations? :S
 

raychar

Joined Nov 8, 2011
82
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.
 

Thread Starter

harami

Joined Jun 20, 2011
66
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.

Rich (BB code):
#include <p18f4520.h>
#include <delays.h>

	#pragma config OSC = HS                              
	#pragma config WDT = OFF
	#pragma config LVP = OFF
	 
	#define btn_on     PORTBbits.RB4
	#define btn_off    PORTBbits.RB5
	#define led        PORTAbits.RA0
	 
	#endif  /* HOST */
	 

void SevenSegment(uint8_t n,uint8_t dp)
{
  static const uint8_t segBits = { 0b00000011, 0b10011111, 0b00100101, 0b00001101, 0b10011001, 0b01001001, 0b01000001, 0b00011111, 0b00000001, 0b00001001, 0b11111101 };

  if (n > 10)
    n = 10;

  SEVEN_SEGMENT_PORT = segBits[n];

  if ((n<10) && dp)
    SEVEN_SEGMENT_PORT &= 0b11111110;
}


	void led( int state );
	int  pressed( int button, int state );
	 
	int main ( ) {
	    int btn1 = 0, btn2 = 0;
	 
	#if !defined(HOST)
	    TRISA = 0;  // set Port A(LED) as output
	    n = 0;   
	#endif
	 
	    for ( ; ; ) {
	        btn1 = pressed( 1, btn1 );
	        btn2 = pressed( 2, btn2 );
         if ( btn1 && !btn2 ) {
	            n( ++1 );
	        } else
	        if ( btn2 && !btn1 ) {
	            n( --1 );
	        }
	    }
	}
 
Top