Timer using PIC 16F877

Discussion in 'Embedded Systems and Microcontrollers' started by Alin, Nov 25, 2014.

  1. Alin

    Thread Starter New Member

    Nov 25, 2014
    Hi .... I need some help if someone can help me ... I made a timer using a PIC 16F877 ... electrical diagram is made by me, is attached, and must fulfill several functions, so I have 5 buttons ... START / STOP, etc different functions.
    But I need a little help in programming the PIC that I'm not good .... can someone help me to realize the code? I see your exemple but in can't understand too much. Thank you very much and i wait yours reply
  2. MaxHeadRoom


    Jul 18, 2013
    What language do you intend using?
  3. Alin

    Thread Starter New Member

    Nov 25, 2014

    Sorry maybe my English is not so good ... I'm from Romania, Eastern Europe. I try to speak as well of grammar.
  4. JohnInTX


    Jun 26, 2012
    I think what Max is asking is which programming language you want to use. C, assembler, BASIC etc. For C you can use the free XC8. MPASM is also free for assembler. I don't know of a free BASIC compiler.

    You need base resistors on Q5-Q8 and to connect the emitters to ground.
    J6 doesn't seem to do anything - if for MCLR/, put it on the other side of the diode using RK5 as a pullup.
    I think your RX/TX lines are interchanged at the PIC.

    Good idea to have jumpers on the programming lines but since your have a few pins left over, I'd move the display lines to other pins if possible. If you want to use a PICKit for debugging, sharing the lines with the LEDs will cause problems.
  5. Alin

    Thread Starter New Member

    Nov 25, 2014
    I what to use MikroC, if is possible. I know the problem about resistors on Q5-Q7 and connecting them to ground, i forgot to draw but thank you about J6 and RX/TX issue because I have not seen.I search on internet and i found some exemples but I have trouble understanding them. I am only to second year on faculty and i'm not so good in PIC programming and so I asked for help.
  6. JohnInTX


    Jun 26, 2012
    MicroC is OK. What are you going to use for a debugger?

    If you are new to PIC programming this may be a bit too ambitious but you can tackle it by breaking the whole program into small blocks and tackle each individually. For this one a starting point would be a block diagram that includes

    Switch input and debouncing
    Serial I/O using the USART with interrupts
    Display multiplexing timed with an interrupt
    System timing using interrupt-driven timers
    Top level program flow that talks to each of these blocks.

    Each block can be broken down into smaller ones until its time to code them.

    There are several threads here on AAC that cover these topics - search away. Don't expect to find a drop-in solution but you can see how others have done the same kinds of functions, code your stuff and ask for help as needed.

    Have fun.
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
  8. Alin

    Thread Starter New Member

    Nov 25, 2014
    I came back with an example found on the Internet. In this scheme is PIC 16F877A and from what I understand differences between 16F877A and 16F877 are not high. I could use A instead of the simple, vice versa I could. My only problem is that the 8 digit are connected to another port PORTD While I am connected to PORTB and transistors used for multiplexing and I do not. Could you please tell me how can I solve this problem? I change my schedule program
  9. JohnInTX


    Jun 26, 2012
    Thanks for your kind words in the PM.

    As promised, here is one AAC thread that gets into interrupt driven display multiplexing pretty thoroughly with some timer stuff thrown in as well. It uses MicroC. There are others covering that and switch inputs, PIC hardware, MicroC and many others. The point of reading it is to understand how the design/programming process goes - and if you read closely, how problems crop up due to insufficient attention to the design process and how they were resolved.

    The problem with your 'find something on the internet' approach is that, while you might be able to find a complete package doing what you want, its more likely that you'll find something close that may or not be easily changeable to suit your needs. There is also a lot of horrible programming floating around on the internet. Here at AAC, bad programming usually gets called out pretty fast with suggestions of how to make it better. I've been at this 40years and still learn from the others. Nice.

    You may or may not be able to accomplish this project at your experience level. However, everybody starts somewhere and that's usually doing lots of little projects - flashing an LED, counting on an output port etc.. All helpful AND if you break your desired project into small enough parts (lots of little blocks) then with some effort, you'll be able to do little projects that become part of your final project. You'll know WHICH little projects are useful if you have a good paper design. Its a process that you really can't short-circuit.

    So first, choose your PIC. If you have a choice pick something in the 18F family - 18F4520-I/P or 18F4525-I/P drops right into an 877 socket and is WAY better - especially for MicroC. It has everything you need.

    To start, I would complete the schematic and build / test the hardware. Make sure that you can select the digits/segments of the display etc. Grab the databook for the PIC and read the sections on the IO ports, read the MicroC manual for examples of how to declare and initialize the ports (direction and initial values) PLUS any pin conflicts with other functions (turning off the A/D converter, disabling comparators etc.) We can help you with that. After that, write a short main() funtion that inits the IO, turns on some segments and endlessly loops with a while(1) at the end. If you can get that far, you'll be well on your way.

    Good luck.
  10. CVMichael

    Senior Member

    Aug 3, 2007
    Ceao Alin,

    Sunt Roman, dar prefer sa scriu in Engleza, pentru ca este mult mai usor pentru mine :)

    I am also using MikroC for my projects. I tried to do clocks using the timer of the PIC but it is not precise. That's why it's best to use a dedicated RTC (real time clock).
    Did you know that MikroE has a forum where people post code; it's called LibStock
    Check out the examples for RTC here: http://www.libstock.com/project_categories/view/9/timers-real-time-clock

    But since you are a beginner at microcontrollers & programming, the best thing to do is test everything on a breadboard (or a prototype board), for example, put one LED as output (and nothing else), then get it to blink, get the comunication with the RS232 working, then put the display and get to do... anything... (counting for example), once you got the electronics part to work, then do the clock.
  11. MrChips


    Oct 2, 2009
    You need to define your goals and aspirations:

    1) I want to get from A to B as quickly and efficiently as possible and don't care how I get to B or what happens along the way.

    2) I want to get from A to B, want some help to show me the way and don't really care about getting to C unless someone guides me there as well, but I don't mind enjoying the ride along the way.

    3) I want to learn how to get from A to B so that I can get to C, D and E on my own.

    In other words, do you want fish to eat for today or do you want to learn how to fish?
  12. Alin

    Thread Starter New Member

    Nov 25, 2014

    Eventually I managed to complete most of the project: electrical diagram, C code and simulation in Proteus. I would like if is possible and if you have some time to look over them yourself and tell me any views and opinions. The code is in MikroC and buttons are the property: J6 - RESET, J5 - START / STOP, J4 - COUNT UP / COUNT DOWN, J3 - INCREMENT MINUTES. With J2 I want to move left or right and with J3 can I set a specific value, not just the minutes in this case.

    Thanks and waiting for your opinion. Alin
  13. vishal12345

    New Member

    Dec 16, 2014