help --PIC16f877 and programming

Discussion in 'Homework Help' started by polarbearbear, Mar 12, 2009.

  1. polarbearbear

    Thread Starter New Member

    Mar 12, 2009
    I’m doing my project, abt the control of the dc motor direction and speed using PIC16F877. I use 2 push-on switches as my sensor.

    The PIC circuit is finished constructed, voltage regulator is used to supply the 5V voltage to PIC. I have some question to ask:

    1. I supply the the 5V voltage to the both of the sensor (push-on switch). Once the switch is pressed, 5Vvoltage will pass through the switch and act as input to the PIC. The voltage input to PIC consider as analog or digital input? Which port (port A, B, C, D, E) should act as the input for the voltage?
    2. Below is the program using CCS code I do. Is it any mistake? My question is: the program I do is definie the switch as on off switch, not push-on. Anyone can teach me how to define the condition with push-on switch?? ( when the sensor A is press, motor will change direction; when sensor B is press, motor will change direction again)

    #include <16f877.h>
    #use delay (clock=20000000)
    #fuses hs,noprotect, nowdt, nolvp
    #byte portb=6
    #byte portd=8

    void main()
    ( input(PIN_B0)==1 && input(PIN_B1)==0)
    Else if
    input(PIN_B1)==1 && input(PIN_B0)==0)

  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    Setup Input/output using the TRIS registers.

    0 is output
    1 is input

    You are setting all ports on B to input, and all ports on D to output.

    Only PortA on the 16F877 has Analog abilities, B, C and D are digital I/O. Since you are using portb as all input, and portd as all output, that is good. You can mix inputs and outputs within a port, for example setting the TRISB to 0x0F would make B0-B3 inputs, and B4-B7 Outputs.

    Remember to debounce your inputs, otherwise it would reverse many times, and end up in a random state. The code below assumes you have debouncing in hardware (rc delay, etc).

    I'm confused as to the function of two buttons for reversing. Does one have a start/stop function as well? I'm just putting out an example below that works to reverse a motor connected via HBridge, assuming there is an enable pin. If there isn't an enable pin, and the buttons have more functions, or debouncing needs to be done, it gets a bit more complicated...

    Here is an example of one pre-debounced button reversing portD (into an HBridge, I assume)

    Code ( (Unknown Language)):
    2. while(1)
    3. {
    4.         if (PIN_B0) // Button should be debounced, reverses motor on each press
    5.        {
    6.            // MUST Be an enable line here to disable Hbridge
    7.            Pin_D3 = 0;  // Assuming hbridge enable is on D3
    8.            asm("nop");  // Time for disable so nothing burns, optional
    9.            Pin_D0 = ~Pin_D0;  // Toggle D0
    10.            Pin_D0 = ~Pin_D1;  // Toggle D1
    11.            Pin_D3 = 1;  // Enable driver again...
    12.        }
    13. }
    The correct way would be to brake the motor before simply reversing it while it is going full speed. Set HBridge to 0 0 for brake, then start again in new direction.

    If there are other functions, and software debounce is needed, then one bit variable and one short are needed.

    Use the bit variable to remember the direction.

    A simple debounce is a "5 of 5", to keep from calling delays. There would be other tasks in between each increment to keep things realtime.

    Code ( (Unknown Language)):
    2. debouncing without delays, pseudocode....
    4. int debounce;
    6. if (button) debounce++;
    7. other code in same function here.
    8. if (button) debounce++;
    9. more little tasks done wondering if user is pushing a button...
    10. if (button) debounce++;
    11. could be a delay....
    12. if (button) debounce++;
    13. one last bit of delay...
    14. if (button) debounce++;
    16. if (debounce == 5)
    17. {
    18.  reversemotor();
    19. }else{
    20. debounce =0;
    21. }
    Last edited: Mar 12, 2009
  3. lmartinez

    Active Member

    Mar 8, 2009
    Out of curiosity, what software and hardware are you using to program your pic. I attempted to start using the pic chip for some of my projects but I only enjoy using c and c++ programming. Please advice and thank you:)