Search code examples
microcontrollermikroc

unable to display numbers counting from 0 to 9 in 7 segment display with mikroc


I am very new to micro-controller programing with MIKROC . I am trying to display numbers counting from 0 to 9 in a 7 segment display with mikroc . I have used infinite while loop . Inside the while loop I used function " delay_ms(500) " with other codes . But instead of showing numbers from 0 to 9 it shows first few numbers like 0,1,2 .

My code is below

void main()
{

 trisb=0;
 portb=0;

 while(1){
 delay_ms(500);
  portb=0x3F;
  delay_ms(500);

  portb= 0x06;
  delay_ms(500);
  portb= 0x5B;
  delay_ms(500);
  portb=0x4F;
  delay_ms(500);
  portb=0x66;
  delay_ms(500);
  portb=0x6D;
  delay_ms(500);
  portb=0x7D;
  delay_ms(500);
  portb=0x07;
  delay_ms(500);
  portb=0x7F;
  delay_ms(500);
  portb=0x6F;
  delay_ms(500);



 }
}

My circuit diagram

enter image description here


Solution

  • First of all, if this is a standard 7 segment display,it is basicly just a bunch of LED's with a common cathode or common anode. Hence you will want to protect you µChip against "shorting", as the LED will open fully and, either draw maximum power from the pins (common cathode), or force your chip to sink maximum power (common anode).

    Your circuit diagram does not reveal which is used, so I am being generic here.

    So add a resistor in series with the LCD pins, (not the common one, the other pins). For 5 Volts, you typically want somewhere between 300 and 1K ohm depending on the desired light intensity!

    Next

    something about communication with LCD's, and lcd can either be setup as: "abcdefg" or as "gfedcba"

    bit# name description
    MSb "A" is the top LED bar
    2.b "B" is the right most upper LED bar
    3.b "C" is the right most lower LED bar
    4.b "D" is the the bottom LED bar
    5.b "E" is the left most lower LED bar
    6.b "F" is the left most upper LED bar
    7.b "G" is the center LED bar
    8.b "D.P" is the dot .. also indicating down.. (or decimal point)
    

    meaning for a common cathode (inverse bit pattern for common anode):

    Digit   gfedcba abcdefg a   b   c   d   e   f   g
    0       0×3F    0×7E    on  on  on  on  on  on  off
    1       0×06    0×30    off on  on  off off off off
    2       0×5B    0×6D    on  on  off on  on  off on
    3       0×4F    0×79    on  on  on  on  off off on
    4       0×66    0×33    off on  on  off off on  on
    5       0×6D    0×5B    on  off on  on  off on  on
    6       0×7D    0×5F    on  off on  on  on  on  on
    7       0×07    0×70    on  on  on  off off on  off
    8       0×7F    0×7F    on  on  on  on  on  on  on
    9       0×6F    0×7B    on  on  on  on  off on  on
    

    so I figure (based on your hex values) you must have a type "gfedcba" as you can see, your code complies with the table. If and only if the common is connected to ground.

    (read more here: http://www.electronicsblog.org/seven-segment-display-everything-you-need-to-know-about/)

    Code I can still not see a problem with your code, if you have been able to show the numbers 0, 1 , AND 2. Since this indicates that all LED's has then been lit at least once, and that your PIC should be properly configured..

    My best guess is that this is in fact an electrical problem. you PIC should with this code, show the numbers, 0 - 9, at half a second intervals, unless it is reset after it reaches a certain number.. this could be a bad solder, or more common a problem with unstable DC supply.

    To suppress small spikes on the supply, try adding a few capacitors in parallel with the supply. for testing, you could try: 100nF and 1000µF. Dont worry it wont do anything.. except absorbing high frequency noise.. It is a special case of a low pass filter..