Thread: Something missing in my code?

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    7

    Something missing in my code?

    Hi all, I appreciate you taking time to view this thread, im not sure if its in the wrong place etc, but its my first post. I'm doing a dice project, using a pic16f877 microcontroller I need to produce two seven segment displays which would emulate the outcomes of two random dice, ive got to a certain stage but I need help, im using c language by the way, anything would be appreciated, thank you.
    here's my code:
    Code:
    #include <pic.h>
    const static unsigned char segment_code[] = { 0x3F,    //0
    0x06,  //1
    0x5B,  //2
    0x4F,  //3
    0x66,  //4
    0x6D,  //5
    0x7C };
     
    int counter1 = 0, counter2 = 0;
    bit button_up = 0;
     
    void main(void)
    {
           TRISD = 0X00;
           TRISC = 0X00;
           TRISB = 0xFF;
          
           counter1 = rand() % 6 + 1;
           counter2 = rand() % 6 + 1;
           for (;;)
           {
                  if (RB0 == 1)
                         button_up = 1;
                  else
                  {
                         if (button_up == 1)
                         {
                               button_up == 0;
     
                         }
                  }
                  PORTC = segment_code[counter1];
                  PORTD = segment_code[counter2];
           }
    }
    Last edited by kgriffiths1927; 01-08-2014 at 06:06 AM. Reason: mis-spelt the word'two' in relation to two seven segment displays being required

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Check out this line of code.
    Code:
     button_up == 0;
    Should that be a comparison, or an assignment?

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    7
    I think an assignment? I'm not too sure, im new to programming as a whole, should it be [code]
    button_up=0
    [\code]
    instead?
    Last edited by kgriffiths1927; 01-08-2014 at 06:16 AM.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    you may have to rethink your code as it only gets random numbers at the start program and uses these for the rest. also may i suggest that you use interrupts?

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    7
    Quote Originally Posted by africanwizz View Post
    you may have to rethink your code as it only gets random numbers at the start program and uses these for the rest. also may i suggest that you use interrupts?
    how should I change the code for it to produce a random number further down? I see what you mean though, as they are declared at the beginning they would be used for the display, however if the programme was re-run would it produce a different number? In relation to interrupts, I am yet to learn about them, please enlighten me? thank you for your time!

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Quote Originally Posted by kgriffiths1927 View Post
    however if the programme was re-run would it produce a different number? In relation to interrupts, I am yet to learn about them, please enlighten me?
    yes, and you would need to read up on interrupts on your own .

    first off, you would have to have some delay functions. Then generate a new number at the start of your for loop.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You appear to know what an assignment operator in C, is:
    Code:
    int counter1 = 0;
    You may want to run through the C tutorial which is linked on the button at the top of this web page. It's excellent, and you will need a bit more C knowledge than you have currently.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I don't know if you're restricted in the circuit that you use, but a BCD to 7-segment driver (such as the 7447) will greatly simplify your code.

    Also, the most common 7-segment common-anode displays with multiple digits have shared lines for the segments, and require a dedicated drive source for each digit (the easiest way being to bias a transistor to supply the voltage to the anode of interest). In this scenario, you would have to use other pins to "select" the digit of interest before outputting the 8-bit segment pattern for that digit. I don't know if this is applicable to your 7-segment display, but thought I'd mention it for your consideration.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-15-2013, 10:31 AM
  2. Replies: 2
    Last Post: 04-06-2012, 02:12 PM
  3. Replies: 22
    Last Post: 07-07-2006, 04:42 AM
  4. Need the missing line of code
    By mayhem in forum C Programming
    Replies: 3
    Last Post: 06-20-2005, 04:21 PM
  5. Am i missing something?
    By Chaplin27 in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 03-08-2005, 03:27 AM

Tags for this Thread