Need help on programming code count 00 to 99

Discussion in 'Embedded Systems and Microcontrollers' started by munap, Mar 16, 2013.

  1. munap

    Thread Starter Member

    Nov 23, 2012
    33
    0
    Hello,

    i have problem to write programming code in MikroC Pro for PIC,since i'm quite new in micro c and programming,i do not know what my programming code mistake,btw i'm using PIC16F877A. Here i attach my code,hope someone can guide me,really appreciate it.

    int i,j,k;

    void display_s1(int j,int i){
    PORTB = 0b00100000 + j;
    PORTB = 0b00010000 + i;

    delay_ms(2);
    }

    void main() {
    TRISB = 0;
    PORTB = 0;

    while(1){
    for(k=0;k<100;k++){
    j = k%10;
    i = k/10;
    display_s1(j,i);
    delay_ms(200);
    }
    }
    }
     
  2. munap

    Thread Starter Member

    Nov 23, 2012
    33
    0
    This is my another coding,also cant get nice count 00 to 99 because the 7-segments are alternating count.
    int i,j,k;

    void display_s1(int j){
    PORTB = 0b00100000 + j;
    delay_ms(2);
    }
    void display_s2(int i){
    PORTB = 0b00010000 + i;
    delay_ms(2);
    }

    void main() {
    TRISB = 0;
    PORTB = 0;

    while(1){
    for(k=0;k<100;k++){
    j = k%10;
    i = k/10;
    display_s1(j);
    delay_ms(200);
    display_s2(i);
    delay_ms(200);
    }
    }
    }
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Please use code tags when posting code:

    http://forum.allaboutcircuits.com/showthread.php?t=37117


    What have you done to resolve your issue? Have you used your debugger?
    We will help but we are not going to go through each line of your code to see why it is not working, that is your job.

    Use your debugger, step through the code, use a logic probe, scope or voltmeter to test each pin of your display and make sure it is doing what you think it is doing.

    Post back when you have more information and still need help but can narrow your problem down to a few lines of code and you have enough information to point us in the right direction.

    Also post your schematic as a png file.
     
    munap likes this.
  4. absf

    Senior Member

    Dec 29, 2010
    1,490
    371
    Code ( (Unknown Language)):
    1. void display_s1(int j){
    2. PORTB = 0b00100000 + j;
    3. delay_ms(2);
    4. }
    5. void display_s2(int i){
    6. PORTB = 0b00010000 + i;
    7. delay_ms(2);
    8. }
    9.  
    10. void main() {
    11. TRISB = 0;
    12. PORTB = 0;
    13.  
    14. while(1){
    15. for(k=0;k<100;k++){
    16. j = k%10;
    17. i = k/10;
    18. display_s1(j);
    19. delay_ms(200);
    20. display_s2(i);
    21. delay_ms(200);
    22. }
    23. }
    24. }
    25.  
    Let me guess. You have a 4511 7-segment decoder connected between the Port B and the 7-segment LED, and RB4 and 5 are connected to the common Anode of the 7S led, right?

    If you are multiplexing the displays, ie displaying both LED in turn, you need at least 25 times per second displaying them or else the display would be flickering and hard to read. Your delay of 200mS x 2 per loop is too long. That makes about 2.5 times per second (assuming K doesn't change for at least a second).

    Show us your schematics so others here can point out your mistakes.

    Allen
     
    Last edited: Mar 17, 2013
    munap likes this.
  5. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    You really need to describe what the problem is that you are having. In the first post you say that you don't know what your mistake is but you give no indication why you believe there is a mistake at all. It's a very different thing to say that everything is dark and nothing happens than to say it displays 00 all the time than to say that it counts from 00 to 09 and the repeats. Similarly, for your second post saying that the 7-segments are alternating count is pretty lean? Does this mean that you are see something like 13..31..13..31..13..41..14..41.. or what?

    It's like calling up an automechanic and leaving a message saying, "I tuned up my car but can't figure out what I did wrong. Please call back and leave the answer on my machine."
     
    munap likes this.
  6. munap

    Thread Starter Member

    Nov 23, 2012
    33
    0
    Thanks for your reply,here is my schematic i attach.
     
  7. munap

    Thread Starter Member

    Nov 23, 2012
    33
    0
    Hello,

    I'm sorry for my inappropriate way present the thread, i have problem to maintain the both 7-segments to not alternating count(i dont what right terminology),
    only one 7-segment will be ON and the other one OFF at same time even it can count 00 to 99, its like : 0_,_0,_1,_2,_3,...,_9(count 0-9 as you can see it should indicate 00 for the 1st count but it separate;0_,_0 and the next count doesnt display 0 at front;_1,_2_3..),1_,_0,_1,..._9(count 10-19)

    So, i dont how to write the code with proper coding, as i'm newbie to programming.
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Again. What have you done to debug your code?
     
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988


    You are letting BI /RBO and LT float. Did you look at the datasheet? What does it tell you about these pins?

    Are the displays common cathode or common anode? They should be common cathode with that chip.

    Work with one at a time. Temporarily tie the cathode to ground. Step through your code and get one digit to light at a time.
     
  10. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    If you make your outputs, say for 10 to 19, go

    1_
    _0
    _1
    _2
    _3
    and so on then you will basically never see the left digit.

    You need them to ping-pong at a fast enough rate so that your eye doesn't realize that they are both blinking.

    So you need it to go something like

    1_
    _0
    1_
    _0
    1_
    _0
    1_
    _0
    1_
    _0
    1_
    _0
    1_
    _0
    1_
    _0
    1_
    _1
    1_
    _1
    1_
    _1
    1_
    _1
    1_
    _1
    1_
    _1
    1_
    _1
    1_
    _1
    1_
    _2
    1_
    _2
    1_
    _2
    1_
    _2
    1_
    _2
    1_
    _2
    1_
    _2
    1_
    _2
    1_
    _3
    1_
    _3
    1_
    _3
    1_
    _3
    1_
    _3
    1_
    _3
    1_
    _3
    1_
    _3

    and so on. You need display to flash its output about 10 to 15 times per second in order to take advantage of human POV (persistence of vision).

    In fact, if you just scroll that list down it should look somewhat like a count that is going 10 11 12 13 with both digits underlined.
     
  11. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988



    Can you post a post a screen capture video of what it is doing? Sorry but you are hard to understand.

    Is either of the displays working and just not switching between the two displays?
     
  12. munap

    Thread Starter Member

    Nov 23, 2012
    33
    0
    Helo,

    Actually i do not know much about debugger and its function. Here i attach picture of printscreen when the program run.
     
    • 1.png
      1.png
      File size:
      132.1 KB
      Views:
      49
    • 2.png
      2.png
      File size:
      132.6 KB
      Views:
      40
    • 3.png
      3.png
      File size:
      130.5 KB
      Views:
      44
    • 4.png
      4.png
      File size:
      131.3 KB
      Views:
      36
    • 5.png
      5.png
      File size:
      129.4 KB
      Views:
      31
    • 6.png
      6.png
      File size:
      132.8 KB
      Views:
      40
  13. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Why not just use 2 BCD to 7 Segment chips? It would make your job easier and you have plenty of pins on your mcu (unless you have plans for them).
     
  14. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    What numbers are you trying to display here?

    You need to learn to use the debugger. It is a good tool. You can't go to the internet to get someone to help you with every programming problem.
    I suggest before you do anything sit down and learn to use the tool. It is not hard to do at all.
     
  15. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Here is my advice.

    1. Learn to use the debugger.

    2. Create a new design to have only one display. Tie your cathode to ground.

    3. Write a new program to count only 0-9 on the one display.

    4. If that works, tie your cathode to your pic and change the program to it turns your display on and off.

    5. If that works add your second display.

    It is always best to start simple and then move to more complex.

    If you can add a second bcd to 7 segment for the second display then that would be best then you could just tie both cathodes to ground.

    But again do not worry about 2 displays now. Get one to work first.
     
    munap likes this.
  16. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    Then you need to learn. Most debuggers can be used in a very simple mode where you just step through the code line by line. Most debugging tasks can be done fairly readily even if all you learn is how to do that. Then learn a few of the other basic capabilities, such as running from the beginning until the first time you encounter a particular line of code. Then learn how to set up watch lists and then how to run until certain variables have certain values. At each stage you will be gaining a significant increase in your ability to use the tools available to you, and that with only about four of the probably dozens, if not hundreds, of things your debugger is capable of.
     
    munap likes this.
  17. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    I COMPLETELY endorse this approach. Even after you gain experience, you are well served by starting with simple steps and verifying that you really can make those simple steps work before adding in a bit more complication. I can't begin to count the number of times I have seen people write programs that were hundreds of lines long, or build circuits that had dozens of components, and then spend hours trying to figure out why it isn't working properly, only to discover that they had an error in their first few lines or had a power supply hooked up incorrectly or something else that would have reared its ugly head with the most basic first step in which case the shear simplicity of the program or circuit would have made the problem glaringly obvious.

    But people don't want to "waste" time by doing it incrementally. So instead of spending two hours building it, testing it, and correcting it incrementally, they spend one hour building it and five hours figuring out what's wrong, and another hour rebuilding it, only to discover that something else isn't working.
     
  18. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    I am an experienced programmer and I still use this approach.

    When I have a problem with a complicated program I will break it down into a much smaller program that focuses on the issue.

    When I build my projects I start off small. Get one section working then build from there.
     
Loading...