Help in 7-segment display ?

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

Help in 7-segment display ?

Postby albasha-8 » Tue Jul 26, 2011 2:03 pm

hi there, i have the PKT40A training kit. im trying to construct an automatic room light controller with visitor counter. but im facing a problem with the 7-segment display, i cant increment it or decrement it to count the visitors. i have developed a program in C but the numbers are not moving in order. does anyone know the problem with my program ?
CODE: SELECT_ALL_CODE
#include <htc.h>


//   configuration

__CONFIG (0x3F32);

//   define
#define   _XTAL_FREQ      20000000
#define   SW1        RB0
#define   SW2         RB1
#define SW3         RB2
#define BUZ         RC2
#define display     PORTD

//   main function

   void main()
{
   

   ADCON1 = 0x06;                     //Congifure Port A as digital I/O

   TRISA = 0b00000000;                  //Configure Port A as Input
   TRISB = 0b11111111;                  //Configure Port C as Output
        TRISD = 0b00000000;
   
   
   int visitor = 0;   
   PORTD = 0;
   
   
   while (SW3 == 0)
   {
   if (SW1 == 0)
   {
    visitor++;
    PORTD = visitor;
   }
   else if (SW2 == 0)
   {
    visitor--;
    PORTD = visitor;
   }
   }
   

   
}



the flow of the program is as follows;
-set the number of visitors to zero
-set the display zero.
-while switch 3 is pressed, if switch 1 is pressed, the number of visitors is incremented and the new number is shown on the display.
-while switch 3 is pressed, if switch 2 is pressed, the number of visitors is decremented and the new number is shown on the display.

thanx in advance :)
albasha-8
Fledgling
 
Posts: 1
Joined: Tue Jul 26, 2011 1:55 pm

Re: Help in 7-segment display ?

Postby hyng » Tue Jul 26, 2011 6:05 pm

PORTD is for 7 segment? You can't simply give value to PORTD without consider IC CD4511 latch enable pin. Bear in mind that to control a 7 segment, it is not like give '1' to its port then it will show '1' in 7segment.
If we just look into the code without refer to hardware, the code still not logic. When you press the decrement button during the visitor value is 0, it becomes negative value. But how can a PORTD to store -ve value?

AFAIK, PTK40A comes with a sample test code in a cd. PLEASE refer to that code. thanks.
User avatar
hyng
Moderator
 
Posts: 292
Joined: Thu Apr 16, 2009 11:35 am

Re: Help in 7-segment display ?

Postby Brian Griffin » Tue Jul 26, 2011 7:23 pm

You cannot convert "visitor" to the 7-segment code.

You need to break the no.of visitors into two or three digits first, and then each digit is referred to the seven-segment look-up table.

To form the table you need to use a pencil and a paper, and trace each segment and then form the pattern in PORTD. Example: One, abcdefg, only the bc is lit, so 0110000 if it's a common cathode, and the inverse if it is a common anode.

from 0 to 9, put them in an array, such as unsigned char SevenSeg[10] = { 0b0110000, ... }

Then if you want the microcontroller to show '1', simply write "PORTD = SevenSeg[1];"

If you can show the digits, then it's time for multiplexing. Make sure you did this first.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: Help in 7-segment display ?

Postby robosang » Wed Jul 27, 2011 1:45 pm

1st.... not all of us have PTK40A, so we are not sure how is the connection from PIC to the 7 segment. Is it direct from PIC pin to each segment? How many segment? Is it gone through encoder/decoder? If so, which pin is it being connected? PORTD? Which pin? So it will be good to elaborate with a schematic if you can attach or print screen the 7 segment area.

OK, assuming the 7 segment is connected through decoder/encoder and is connected at PORTD, normally 4511 (if that is the encoder) will decode from 0 to 9, and this require 4 bit interface only. Let's assume is it connected in correct sequence, from RD0 to RD3. Moving value of your visitor to PORTD will display correctly from 0 to 9 only, from 10 onwards the value is not correct because 10 is 0b00001010, and 4511 will make your 7-segment blank (if I am not mistaken). There are plenty more thing to learn about 4511. Wait a minute, have you study the function of 4511 (if it is being used on the PTK40A)?

Another thing that cause random number display, aside from the point stated above and also highlighted by hyng, microcontroller run in very fast frequency, your program will increase value of visitor when microcontroller detected the switch is press. In reality, the switch is being scanned many time even when you press it momentary, thus the value of visitor has been increase several times even it is a short press, so how? 8-)
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: Help in 7-segment display ?

Postby Brian Griffin » Wed Jul 27, 2011 2:14 pm

robosang WROTE:1st.... not all of us have PTK40A, so we are not sure how is the connection from PIC to the 7 segment. Is it direct from PIC pin to each segment? How many segment? Is it gone through encoder/decoder? If so, which pin is it being connected? PORTD? Which pin? So it will be good to elaborate with a schematic if you can attach or print screen the 7 segment area.

OK, assuming the 7 segment is connected through decoder/encoder and is connected at PORTD, normally 4511 (if that is the encoder) will decode from 0 to 9, and this require 4 bit interface only. Let's assume is it connected in correct sequence, from RD0 to RD3. Moving value of your visitor to PORTD will display correctly from 0 to 9 only, from 10 onwards the value is not correct because 10 is 0b00001010, and 4511 will make your 7-segment blank (if I am not mistaken). There are plenty more thing to learn about 4511. Wait a minute, have you study the function of 4511 (if it is being used on the PTK40A)?

Another thing that cause random number display, aside from the point stated above and also highlighted by hyng, microcontroller run in very fast frequency, your program will increase value of visitor when microcontroller detected the switch is press. In reality, the switch is being scanned many time even when you press it momentary, thus the value of visitor has been increase several times even it is a short press, so how? 8-)


Thanks for reminding me about the button debounce.

There are some methods of handling buttons:

1.) Hardware - R/C configuration. Kills most of the unwanted oscillations. But due to capacitor having +/-20% tolerances, it must be measured properly before applying.

2.) Hardware - Flip-flops. Detects single step and then latch. As simple as that.

3.) Hardware interrupts present in microcontrollers.

4.) Software debouncing - very tedious, but do-able.

5.) Seperate hardware debouncing chip. (very expensive)
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: Help in 7-segment display ?

Postby robosang » Wed Jul 27, 2011 6:38 pm

brian..... the problem is not only deboucing :mrgreen: , is the program checking switch. Even if the deboucing is added for hardware... it will still increase a lot 8-)
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: Help in 7-segment display ?

Postby Brian Griffin » Wed Jul 27, 2011 7:27 pm

robosang WROTE:brian..... the problem is not only deboucing :mrgreen: , is the program checking switch. Even if the deboucing is added for hardware... it will still increase a lot 8-)


Poll the switch every few miliseconds. Use a timer interrupt, or use a hardware interrupt if polling gives too much software overhead.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: Help in 7-segment display ?

Postby shahrul » Wed Jul 27, 2011 9:38 pm

If to do a counter, it's not counting at the low state or at high state. But counting at the time the state changing, ex when the switch goes from high to low.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: Help in 7-segment display ?

Postby robosang » Wed Jul 27, 2011 10:03 pm

shahrul..... you get the point..... is not about whether the logic is low or high is about reading high to low, or low to high.... pulses :D
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: Help in 7-segment display ?

Postby ober » Tue Aug 02, 2011 10:15 am

Yup, agreed with most of the discussion here.

Let's look the connection from PIC pin to 7-segment on PTK40A. As hyng and robosang highlighted, there are plenty of method to use 7-segment, direct IO to each segment. and for more than 1one 7-segment, you can either use multiplexing using transistor or encoder such as CD4511. PTK40A uses 4511 to encode and latch the 7-segment output.

7-segment PTK.JPG


Looking at the schematic for the 7-segment section, you will notice that RD0 to RD3 is connected to four input pin of 4511 which will decide the output on 7-segment, either blank, to display 0 to 9.

Image

while the LT and BL is being pullup to 5V, we will utilize the LE (Latch Enable) to offer multiplex two 7-segment since both 4511 is connected to same four output from PIC (RD0 to RD3)

one 7-segment can only display digit from 0 to 9 according to the true table above and the digit is related to input at D0, D1, D2 and D3 (schematic). Yes, moving 1 to PORTD will display 1 at 7-segment, but moving 10 to PORTD will not display '1' and '0' at 7-segment. We need to utilize the latch and also to single digit, if the value is 10, it should be '1' for 1st 7-segment and '0' for 2nd 7-segment.

Please refer to the 7seg.c for the function that you can use.
CODE: SELECT_ALL_CODE
/* The protocol for the CD4511
LE   D   C   B   A   a    b   c   d   e   f   g   Display
0   0   0   0   0   1   1   1   1   1   1   0   '0'   
0   0   0   0   1   0   1   1   0   0   0   0   '1'   
0   0   0   1   0   1   1   0   1   1   0   1   '2'   
0   0   0   1   1   1   1   1   1   0   0   1   '3'   
0   0   1   0   0   0   1   1   0   0   1   1   '4'   
0   0   1   0   1   1   0   1   1   0   1   1   '5'   
0   0   1   1   0   0   0   1   1   1   1   1   '6'   
0   0   1   1   1   1   1   1   0   0   0   0   '7'   
0   1   0   0   0   1   1   1   1   1   1   1   '8'   
0   1   0   0   1   1   1   1   0   0   1   1   '9'   
0   1   0   1   0   0   0   0   0   0   0   0   ' '   (blank)
0   1   0   1   1   0   0   0   0   0   0   0   ' '   (blank)
0   1   1   0   0   0   0   0   0   0   0   0   ' '   (blank)
0   1   1   0   1   0   0   0   0   0   0   0   ' '   (blank)
0   1   1   1   0   0   0   0   0   0   0   0   ' '   (blank)
0   1   1   1   1   0   0   0   0   0   0   0   ' '   (blank)
1   X   X   X   X   *   *    *    *   *   *   *   '*'   (depend on the value at ABCD during LE from 0 to 1

X = Don't care
* = special condition



/*******************************************************************************
* PUBLIC FUNCTION: seg7_initialize
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Initialize and clear the 7 segment LED
*
*******************************************************************************/
void seg7_initialize(void)
{
   S7_L1 = 0;
   S7_L2 = 0;
   S7_DATA = (S7_DATA & 0xf0) | 0x0A;
   __delay_ms(1);   //delay for a while
   S7_L1 = 1;   //holding the state before this, which is blank
   S7_L2 = 1;   //holding the state before this, which is blank
   S7_DATA = S7_DATA & 0xf0;
}




/*******************************************************************************
* PUBLIC FUNCTION: seg7_1_dis(unsigned char uc_number)
*
* PARAMETERS:
* ~ uc_number which will be display on 7 segment, 0 to 9, other value will result in blank.
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Display the value of uc_number on 7 segment 1 and hold it
*
*******************************************************************************/
void seg7_1_dis(unsigned char uc_number)
{
   S7_L1 = 0;   
   S7_DATA = (S7_DATA & 0xf0) | uc_number;
   __delay_ms(1);   //delay for a while   
   S7_L1 = 1;   //holding the state before this, which is the uc_number
   S7_DATA = S7_DATA & 0xf0;
}



/*******************************************************************************
* PUBLIC FUNCTION: seg7_2_dis(unsigned char uc_number)
*
* PARAMETERS:
* ~ uc_number which will be display on 7 segment, 0 to 9, other value will result in blank.
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Display the value of uc_number on 7 segment 2 and hold it
*
*******************************************************************************/
void seg7_2_dis(unsigned char uc_number)
{
   S7_L2 = 0;   
   S7_DATA = (S7_DATA & 0xf0) | uc_number;
   __delay_ms(1);   //delay for a while   
   S7_L2 = 1;      //holding the state before this, which is the uc_number
   S7_DATA = S7_DATA & 0xf0;

}


/*******************************************************************************
* PUBLIC FUNCTION: seg7_com_dis(unsigned char uc_number)
*
* PARAMETERS:
* ~ uc_number which will be display on both 7 segment, 0 to 99, other value will result in blank.
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Display the value of uc_number on both 7 segment and hold it
*
*******************************************************************************/
void seg7_com_dis(unsigned char uc_number)
{
   if(uc_number > 99)
   {
   seg7_1_dis(0x0A);   //blank
   seg7_2_dis(0x0A);   //blank
   }
   else
   {
   seg7_1_dis(uc_number%10);   //display the 1st 10 based digit
   seg7_2_dis(uc_number/10);   //display the 2nd 10 based digit
   }
}


Not to forget the point highlighted by Brian, robosang and shahrul, switch deboucing and checking routine.
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Next

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 5 guests

cron