Maker Pro
Maker Pro

control system counter up to 9

Kath

Oct 3, 2015
3
Joined
Oct 3, 2015
Messages
3
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..
 

Old Steve

Jul 23, 2015
734
Joined
Jul 23, 2015
Messages
734
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?
 

Kath

Oct 3, 2015
3
Joined
Oct 3, 2015
Messages
3
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;
}
}
}
}
 

Old Steve

Jul 23, 2015
734
Joined
Jul 23, 2015
Messages
734
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?
 

Old Steve

Jul 23, 2015
734
Joined
Jul 23, 2015
Messages
734
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:

Old Steve

Jul 23, 2015
734
Joined
Jul 23, 2015
Messages
734
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:

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
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.
 

davenn

Moderator
Sep 5, 2009
14,261
Joined
Sep 5, 2009
Messages
14,261
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
 

hevans1944

Hop - AC8NS
Jun 21, 2012
4,880
Joined
Jun 21, 2012
Messages
4,880
Can you help me create a program on a PIC16F877A microcontroller using C language ... ? ...
@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?
 

Gebus

Nov 6, 2015
4
Joined
Nov 6, 2015
Messages
4
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
 
Top