Connect with us

control system counter up to 9

Discussion in 'Microcontrollers, Programming and IoT' started by Kath, Oct 3, 2015.

Scroll to continue with content
  1. Kath

    Kath

    3
    0
    Oct 3, 2015
    Can you help me create a program on a PIC16F877A microcontroller using C language with this kind of problem?

    Design a control system that will display a counter up to 9.
    When add button is pressed, it will add 1 and will display to its seven segment display.
    When start button is pressed, the sequence will be flash the led for 500ms depends on the number of count display.
    When interrupt button is pressed, it will paused the program and when it pressed again, it will continue the program.

    I really need your help..
     
  2. Old Steve

    Old Steve

    734
    169
    Jul 23, 2015
    I don't think anyone is going to write your program for you, Kath. You really need to make a start, get as far as you can, then ask for assistance if you have problems.
    Do you have any C programming experience?
     
  3. Kath

    Kath

    3
    0
    Oct 3, 2015
    Actually, i already able to create a program that when the add button is pressed, it will add 1 and will display to its 7segment..but my problem is the start button part. I already did some program but still doesn't work.

    So far this is only what i got..

    #include <16F877A.h>
    #fuses hs,noput,nowdt,noprotect,nobrownout
    #use delay(clock=4000000)

    int LED[10]={0b00111111,0b00000110,0b01011011,0b01001111,0b01100110,0b01101101,0b01111101,0b00000111,0b01111111,0b01100111};
    int count=0;
    int count_1;

    void main()
    {
    while(1)
    {
    if(input(PIN_E0)) //add button
    {
    output_C(LED[count]);
    delay_ms(500);
    count=count+1;
    }
    else
    {
    if(input(PIN_E1)) //start button
    {
    output_C(LED[count]);
    output_high(PIN_D0);
    delay_ms(500);
    output_low(PIN_D0);
    count=count-1;
    }
    }
    }
    }
     
  4. Old Steve

    Old Steve

    734
    169
    Jul 23, 2015
    Kath, when you post code, it's best to use code tags. At the top of the edit window, the fourht item from the right will highlight "Insert..." when you hover the mouse over it. Click on that button and select "Code", then paste your (formatted) code into that window. It makes it much easier to read.
    Like this:-
    Code:
    #include <16F877A.h>
    #fuses hs,noput,nowdt,noprotect,nobrownout
    #use delay(clock=4000000)
    
    int LED[10]=
    {
        0b00111111,
        0b00000110,
        0b01011011,
        0b01001111,
        0b01100110,
        0b01101101,
        0b01111101,
        0b00000111,
        0b01111111,
        0b01100111
    };
    
    int count=0;
    int count_1;
    
    void main()
    {
        while(1)
        {
            if(input(PIN_E0))     //add button
            {
                output_C(LED[count]);
                delay_ms(500);
                count=count+1;
            }
            else
            {
                if(input(PIN_E1))     //start button
                {
                    output_C(LED[count]);
                    output_high(PIN_D0);
                    delay_ms(500);
                    output_low(PIN_D0);
                    count=count-1;
                }
           }
        }
    }
    Which compiler and IDE are you using?
     
  5. Old Steve

    Old Steve

    734
    169
    Jul 23, 2015
    You don't appear to have set the TRISx registers for the ports that you're using.
    I didn't actually realise there was a PORTE on the F877, but just checked the datasheet and there is, multiplexed with AN5, AN6 and AN7.

    Can I assume that this "output_C(LED[count]);" addresses PORTC?
    You'd need to use "TRISC=0b00000000;" to set those pins as outputs.

    I usually use a different language for PICs, but that rule still applies.
    (I use C/C++ for AVR micros, and PICBasic Pro for PICs.)

    And this is not an error, but where you have "count=count+1;" and "count=count-1", it's more usual in C to write it as "count+=1;" and "count-=1;" (Easier, too.)

    Code:
    #include <16F877A.h>
    #fuses hs,noput,nowdt,noprotect,nobrownout
    #use delay(clock=4000000)
    
    TRISC=0b00000000;
    TRISD=0b11111110;
    
    int LED[10]=
    {
        0b00111111,
        0b00000110,
        0b01011011,
        0b01001111,
        0b01100110,
        0b01101101,
        0b01111101,
        0b00000111,
        0b01111111,
        0b01100111
    };
    
    int count=0;
    int count_1;
    
    void main()
    {
        while(1)
        {
            if(input(PIN_E0))     //add button
            {
                output_C(LED[count]);
                delay_ms(500);
                count+=1;
            }
            else
            {
                if(input(PIN_E1))     //start button
                {
                    output_C(LED[count]);
                    output_high(PIN_D0);
                    delay_ms(500);
                    output_low(PIN_D0);
                    count-=1;
                }
           }
        }
    }
     
    Last edited: Oct 3, 2015
  6. Old Steve

    Old Steve

    734
    169
    Jul 23, 2015
    Depending on your compiler, if the TRISC line I wrote above doesn't work, although it should, try this:-
    set_tris_c(0b00000000);

    TRISC=0b00000000; works in my C compiler.

    output_C(value); doesn't work in my C compiler, so I can't help much.

    In my compiler, this works:- "PORTC=0b11110000;" or "PORTC=0xF0;" works, or even "PORTC |= 0xF0".
     
    Last edited: Oct 3, 2015
  7. chopnhack

    chopnhack

    1,573
    352
    Apr 28, 2014
    Steve, should she be defining her pin's before calling them to ensure their status at start? The if / else statement is predicated on the values of the those pins.
     
  8. davenn

    davenn Moderator

    13,668
    1,891
    Sep 5, 2009
    Guys
    it was just brought to my attn. that the OP, Kath, has also posted this in homework
    I have closed that thread and will let it continue here since help has been forthcoming

    Those of you that know pic programming, please continue with good guidance and encourage @Kath to
    work on the problems and learn to figure things out :)
    do questions and answers to make sure things are being understood :)

    cheers
    Dave
     
  9. hevans1944

    hevans1944 Hop - AC8NS

    4,541
    2,116
    Jun 21, 2012
    @Kath it would probably help us to better help you if you could describe your development environment. The PIC16F877A is a very sophisticated microprocessor with many more functions and capabilities than your problem requires. These "extra" features usually have to be disabled to use it in a less complicated fashion. For example, you mentioned "when the interrupt button is pressed" without specifically stating what that means. Hardware interrupts are created externally by an interrupt signal applied to port RB0 and internally by several other means. Do you want to generate a hardware interrupt by connecting a switch to port RB0? Does this have to actually assert an interrupt to the running program or can you simply poll the switch state in the main program loop and then act accordingly? Writing a polling routine is much easier than writing an interrupt handler. Also, since the "interrupt button" has two functions: (1) stop the running program and (2) re-start the running program, you have to worry about switch contact bounce so the two states are entered and exited just once for each button press. Have you given any thought on how you will do this?
     
  10. Gebus

    Gebus

    4
    0
    Nov 6, 2015
    Looks like you have a few things to learn about here. Let me try and break it into more manageable chunks...

    Part 1 - Setup the PIC
    You will need to set the configuration bits. These control things like the clock source for the PIC, the watchdog settings etc. There are usually quite a few, but you will only really need to pay attention to a few I think. See the device datasheet/compiler user guide to see how to set them.
    After the configuration bits are set you will need to setup the pins you want to use. Your button for example will be wired to a pin, this pin should be capable of detecting an external interrupt. Your LCD driver pins should be outputs (how do you communicate with the LCD?).

    Part 2 - making a button do something.
    Look into PIC interrupts. After you understand what an interrupt is, check your compiler user guide or google some examples (the microchip PIC forums are helpful). You will need to set up a pin to interrupt on an edge (i.e. button press - make sure you have debounce!). When the interrupt happens you will need an Interrupt Service Routine (special bit of code to manage it).

    Part 2 - making a 500ms delay
    Easy and bad way to do this is to wast time by doing nothing for a while. Simply loop on "no ops" (no operations) until you have wasted an amount of clock cycles equivalent to 500ms.
    Better way to do this is interrupts again, this time setting up a timer (see device datasheet for the registers to do this). Every time your timer expires (i.e. hits 500ms) it will go to your ISR and execute the code there (toggle an LED in this case).

    Part 3 - control your LCD
    Have a function that can take a number and display it on your LCD. I need more info on how you talk to the LCD to give advice on this.

    Part 4 - Bring it all together
    It sounds to me like you may want to look into a "switch case" control for this, but there are many ways. If you get the individual pieces working you shouldn't have too much difficulty putting it all together.

    Remember and start small and debug every step. Start making sure you can switch an LED on, then see if you can toggle it with a button then control it with a timer...

    Good luck!
    G
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-