Advice for programming the PIC16f877A Microcontroller

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
sharelone
Posts: 2
Joined: Sat Jan 19, 2019 6:43 am
Has thanked: 1 time
Contact:

Advice for programming the PIC16f877A Microcontroller

Post by sharelone »

I'm working on a project on the PIC16F877A microcontroller. The PIC will be programmed using Flowcode V4 and i've already finished making the flowcode file. The project that i've chosen is a chess clock. Nothing too fancy, just for bullet chess. The .fcf is attached on this thread.

I've designed the project so that it uses a single switch for the players turn, two 7-segment displays shared by the two players, two LED's for the player turn indicator, and a reset button. I've already asked advice from my upperclassmen but their advice are quite limited. They said that the difficult part of getting the whole project to work properly is the timing mechanism. Most likely, we'd be using a crystal oscillator, but the timing would be way off unless we add a capacitor. They mentioned that we would still need to calculate for the right crystal oscillator and capacitor to use. They also mentioned that one of them used a car battery to power their project because using a normal battery wouldn't give enough current. I have a variable power supply but it only has variable voltage.

It's obvious that programming the project is only the easy part. If things get too complicated, i might drop the reset and LED indicator features. Are there any advice from any of you who've had experiences with similar situations? Any comments and advice are warmly welcome. I'd also appreciate it if you guys could suggest some components that would be perfect for the project.

Cheers.
Attachments
prototype with reset.fcf
(18.04 KiB) Downloaded 238 times
Last edited by sharelone on Sun Jan 20, 2019 11:33 am, edited 1 time in total.

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Advice for programming the PIC16f877A Microcontroller

Post by chipfryer27 »

Hi

Trying to understand your functionality. I don't have v4 installed on this machine but your Flowchart opens in v5 and I have fond memories of such.

From what I gather you want a clock that will decrement each second and this will be displayed on your 7-Segment LED displays (note your Flowchart only has two displays but you say you are using four, two for each player).

When started or reset the counter will count down from 60 until the switch is toggled from player 1 to player 2 (or vice-versa). This will halt the countdown for that player and commence the countdown for the other. When toggled again the countdown for that player will recommence from where it left off. e.g. Player one starts and takes ten seconds to make a move. His counter stops at 50 and player two's counter starts to decrement. When he finishes his move Player one's counter continues from 50 etc. Is that roughly what you are after?

I don't see anything in your program regarding timing delays (other than 500mS for flashing LEDs) or timer interrupts so how are you doing your timing to decrement each second? How are you measuring a second before decrement? It looks like you are just incrementing a counter and in simulation this "may" work depending on your PC. A PIC however running at almost 20MHz will run your program in the blink of an eye and you probably will just see a blur on your displays (if anything).

I don't have a datasheet for your PIC to hand, but most provide suggested capacitor values for given crystals. As long as you follow that you won't be too far wrong so don't worry much about it. Offhand from memory 15pf - 22pf sounds about right but do check with the crystal manufacturer. You could most likely get away with RC components and you probably have these components lying around. Again this will meet your requirements. Datasheet will give example values

I'd suggest you first make a simple flasher program. An LED connected to a port and then evaluate the different ways to make it flash at one second intervals. A quick search on the forum / wiki for One-Second Flasher will give examples of using a simple Delay instruction and also utilising Interrupts via Timers. Both of these options will provide you with more than enough accuracy for your needs and you can incorporate whichever you feel comfortable with.

For your switches / buttons, remember to incorporate "debounce". Again a quick search will give examples. Don't forget Pull-Up or Pull-Down resistors as needed.

I don't know why your colleagues suggested a car battery for this unless you plan on playing in the dark and the battery is also powering the lighting..:) It is certainly doable with normal batteries but you could consider a "wall wart" PSU that provides a regulated DC voltage from the A/C Mains. Of course, you will need a few extra components if using such (7805 regulator or the like and some discrete components).

Nothing in your project requires anything special so go for whatever is in your component box or cheapest to buy. Remember to decouple the PIC with something like 100nF capacitors as close as possible to the chip's +ve to ground.

I don't think you have much to worry about at all, other than correctly determining each second that passes (see One-Second Flasher for ideas). Hope this helps in some small way.

Regards

sharelone
Posts: 2
Joined: Sat Jan 19, 2019 6:43 am
Has thanked: 1 time
Contact:

Re: Advice for programming the PIC16f877A Microcontroller

Post by sharelone »

chipfryer27 wrote: From what I gather you want a clock that will decrement each second and this will be displayed on your 7-Segment LED displays (note your Flowchart only has two displays but you say you are using four, two for each player).
Oh sorry i meant to say two 7-segment displays shared by the two players. Let me edit my post.

And yes, you have explained my program perfectly. I'm using sort of a manual timing mechanism by incrementing a variable until it reaches a certain value. And i just guessed that value so it would simulate a one second delay.

Also i've thought about using timer interrupts while making the flowcode program but it seems to be working just fine with my current approach.

Since it's my first time programming a PIC, there are a few things you mentioned that i do not quite understand. First is why the reason for debounce? I've worked with pull-up and pull-down resistors before on a quiz show buzzer and it has been very useful. But i don't see the reason to incorporate debounce on a chess clock?

Second is the decoupling of the PIC. I honestly have no idea what that does to the PIC.

You have been very helpful to me and i have learned so much. I will try to make a flasher program right away when i get the PIC working.

Thank you!

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Advice for programming the PIC16f877A Microcontroller

Post by chipfryer27 »

Hi

When a mechanical switch closes it does not "cleanly" make a connection. It will make/break/make a few times before setling into the desired switch state. This is called switch bounce and typically last for up to a few tens of mS. For a light switch on your wall (as an example) you don't see this or even care. However your microcontroller sees a train of make / break pulses etc and it does care. With many millions of instructions per second a microcontroller could read and act on a these chains of break/make.

One software method to eliminate is to incorporate a delay of say 50mS from detecting a switch change to actually reading the port and techniques like this are called debounce as it gives the switch time to settle into the desired state before acting upon it.

When you switch, what is physically happening in your circuit (I don't have flowcode on this machine)? If you are just making one pin go from high to low to indicate player one / player two then you should employ a Pull Up / Pull Down resistor. Pull Up conects between VDD and your chosen input pin and keeps the pin high until you close your button, which would short the pin to VSS. Without Pull Ups (or down if you prefer) the input pin is floating when not directly connected to either VDD or VSS. If floating you cannot guarantee it's state. It could stay low or could go high or even toggle between by itself, but point being you cannot guarantee it will change state to what you want.

If you ever get the chance, put a scope on an electronic circuit between +ve and -ve. It is very doubtful it will be a perfectly straight trace and you will see noise, especially during any switching. It is good practise to try and eliminate this "noise" at each integrated circuit (or module for that matter), as noise entering such can have an undesireable effect. Putting a capacitor between +ve and -ve as close to the chip's +ve connection as possible helps to "decouple" the supply reducing the noise. A typical example would be 100nF.

Have you tried your program on hardware yet? I ask as although it did simulate quite well by counting down, on my PC it went from sixty to zero in about ten seconds (real seconds). Simulation in Flowcode is just that, but is dependent on your PC capabilities. It isn't a guarantee that the same thing will actually happen in hardware.

As I mentioned before, I don't think you have much to worry about really, except getting your timing correct and this is better done with either of the methods mentioned earlier rather than a simple count. Plenty of examples in the wiki/forum and if you get stuck or have a question I'm sure many will help.

Remember your variable types too. From memory I think you had them assigned as bytes. These go up to 255 then roll over back to zero. If you were relying on a count and it was going to be higher than 255 then you need to make your variable(s) either Int (up to 32,767), UInt (65,535) or ULong (4,294,967,295).

Regards

Post Reply