help --PIC16f877 and programming

Thread Starter

polarbearbear

Joined Mar 12, 2009
3
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()
{
set_tris_b(0xff);
set_tris_d(0x00);
portd=0
do{
if
( input(PIN_B0)==1 && input(PIN_B1)==0)
{output_high(PIN_D0);}
Else if
(
input(PIN_B1)==1 && input(PIN_B0)==0)
{output_high(PIN_D1);}
}while(1);
}

THX…
 

thatoneguy

Joined Feb 19, 2009
6,359
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)

Rich (BB code):
while(1)
{
        if (PIN_B0) // Button should be debounced, reverses motor on each press
       {
           // MUST Be an enable line here to disable Hbridge
           Pin_D3 = 0;  // Assuming hbridge enable is on D3
           asm("nop");  // Time for disable so nothing burns, optional
           Pin_D0 = ~Pin_D0;  // Toggle D0
           Pin_D0 = ~Pin_D1;  // Toggle D1
           Pin_D3 = 1;  // Enable driver again...
       }
}
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.

Rich (BB code):
debouncing without delays, pseudocode....

int debounce;

if (button) debounce++;
other code in same function here.
if (button) debounce++;
more little tasks done wondering if user is pushing a button...
if (button) debounce++;
could be a delay....
if (button) debounce++;
one last bit of delay...
if (button) debounce++;

if (debounce == 5)
{
 reversemotor();
}else{
debounce =0;
}
 
Last edited:

lmartinez

Joined Mar 8, 2009
224
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()
{
set_tris_b(0xff);
set_tris_d(0x00);
portd=0
do{
if
( input(PIN_B0)==1 && input(PIN_B1)==0)
{output_high(PIN_D0);}
Else if
(
input(PIN_B1)==1 && input(PIN_B0)==0)
{output_high(PIN_D1);}
}while(1);
}

THX…
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:)
 
Top