problem using 16f877


Closed Thread
Results 1 to 5 of 5
  1. #1
    yrch's Avatar
    yrch Guest

    Question problem using 16f877

    Hi,

    I'm new at this forum and didn't have much time to examine it thoroughly, so sorry if this post does not really belong here.

    I have a very simple question: I'm using a 16f877 microcontroller. It needs to receive inputs from 3 switches and send output to 3 LEDs. The datasheet says that all ports are bidirectional I/O, so I thought it wouldn't matter which port I used for input or output, namely A, B, C or D. I connected the input switches to ports A1, A2 and A3; and the LEDs to B1, B2 and B3. However, it could not receive input from the A ports.

    Then I wrote a program to see if the various ports on 16f877 could light LEDs. The B ports worked fine, but with A ports, only A2 worked. When I tried the same thing for C and D ports, I was able to get very small voltages and thus very dim lights (only seen in dark). (I used three different pics and each time it was the same, so I guess it's not that there's anything wrong with the pics)

    Finally, my question: Are not all ports suitable for both input or output? How can I know if a port is suitable for either input or output?

    Any help would be appreciated.. Thanks in advance...

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    You've come to the right place...

    Rather than give you an 'answer on a plate' (I'm feeling mean), I'll throw you in at the deep end to sink or swim... figure this and you'll know what to do with any PIC you'll ever encounter from now on... so a bit of pain now makes for a real easy life later...

    You've obviously not read the DATASHEET for the 16F877.

    Step 1. Download it from the Microchip website.

    Step 2. Arm yourself with a Beer or a Glass of Warm Milk.

    Step 3. When you've woken, you'll discover the pretty pictures at the start of the datasheet (pin diagrams) tell you that PortA and PortE (and others) are multifunction Ports multiplexed with various goodies (such as Analogue functions).

    Step 4. Since PortA and PortE features are SHARED between Digital I/O and Analogue I/O... and by default on Power-Up they are ANALOG (discover that from the Special Features of the CPU Section what the Initialisation (Power-Up) conditions are for all the Registers). You need to switch them to DIGITAL before you perform any I/O on them. Go to the Section marked Analgue to Digital Converter Module... you will discover what REGISTER settings you need to switch those ports to DIGITAL in that section.

    Step 5. If you have an 16F877A, you need to repeat Step 4 whilst looking at the COMPARATOR Module Section as well.

    This excercise will tell you that if a pin has multiple functions, you need to discover how to turn on the features you want, and turn off those you don't want. You NEED to do that FIRST, before you start using that pin. The Datasheet is your bible on this. So when looking at RD5/PSP5, you need to discover "What is PSP5?" and is it going to get in my way when I want to use RD5 for Digital I/O. Look in the Datasheet for the answer to that.

    Come back and tell us how you've done, or if you need a few more pointers. Trust me, the Datasheet is your friend... everything you need to know about your chosen PIC is in there... go get...

  3. #3
    yrch's Avatar
    yrch Guest


    Did you find this post helpful? Yes | No

    Post Another problem

    Hello again!

    Thanks for your quick response to my post. I guess you were right about the datasheet issue, by examining a bit more and doing a little more research helped me solve my problem.

    However, now I have a problem which I happen to find several solutions, but cannot comprehend them due to my limited knowledge and therefore ask for your help here urgently. This time I have looked at the datasheet carefully, but like I said, it wasn't much of an help since I have trouble understanding it.

    What I'm trying to do at the moment is simply driving a DC motor with a L293D driver circuit and the same PIC, 16F877. According to my program, it needs to run in one direction for 3 seconds, then stop for 1 second and then run in the opposite direction for 3 seconds again. I don' t really need speed control, that is optional, but I do need the reverse direction, which is why I'm using the L293D as a H-bridge.

    Now I will move onto things I have learned through my research on the internet, so that you can help me out better:

    From what I have read, it seems that I need to use (or I better use?) either C1 or C2 port (or both?) for connection to the enable pins on the H-bridge and I should use the PWM module even if I'm going to control the speed of the motor.
    To use the PWM correctly, several data registers should be modified, such as CCP1CON, T2CON and the pulse frequency PR2 and the duty cycle CCPR1L.
    I'm going to be using a DC motor with a 12V input. Though I don't have much of an idea about how those registers should be modified, I added the following lines in my program according to what I have read on the internet:

    CCP1CON = 12
    CCP2CON = 12
    T2CON = %00000101
    PR2 = 249
    CCPR1L = 124
    CCPR2L = 124

    I think the duty cycle and the pulse frequency should be modified in such a way that the desired working and pause periods (3 secs & 1 sec) are achieved. Or are these values unable to determine the motor's sequence and delay and should it be specified within the actual running code (like with "Pause 3000")? If not, how are these aspects related? Apart from the C1 and C2 ports connecting to the enable pins of L293D, I think two additional output ports are required to give the proper rotation direction of the motor. Can these be any port on the 16F877? I have also read that the enable pins should be high, while the directions pins are set low and high one by one. Is there anything else I should know about the enable pins? (because last time I ran the program, no matter which stage of the program it is in, it was always high.

    This is the summary of what I have understood sand what I have not so far. Thank you in advance for your patience and help.
    Last edited by yrch; - 21st January 2006 at 08:19.

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    If you are using a single phase (single winding) DC motor, then connect it across pins 3 and 6 of the L293D (as in the DATASHEET for the L293D).

    Then all you have to do is drive pin 2 HIGH and pin 7 LOW for the motor to turn one way, and reverse it with pin 2 LOW and pin 7 HIGH for the motor to turn the other way (subject of course to you turning pin 1 ENABLE HIGH).

    For Fast Motor Stop, Drive both Inputs LOW, or Drive both Inputs HIGH or Drive ENABLE pin LOW.

    It's all in the DATASHEET.

    Done, dusted, simple. So what's all this playing with PICs Registers for?

    ...it needs to run in one direction for 3 seconds, then stop for 1 second and then run in the opposite direction for 3 seconds again.

    Enable var PortB.0 ' Enable Pin 1
    DriveA var PortB.1 ' Drive Pin 2
    DriveB var PortB.2 ' Drive pin 7

    TRISB=%11111000

    Loop:
    Gosub AllStop
    Pause 1000
    Gosub RunOneWay
    Pause 3000
    Gosub AllStop
    Pause 1000
    Gosub RunOtherWay
    Pause 3000
    Goto Loop

    AllStop:
    Low Enable
    Low DriveA
    Low DriveB
    Return

    RunOneWay:
    High DriveA
    Low DriveB
    High Enable
    Return

    RunOtherWay:
    Low DriveA
    High DriveB
    High Enable
    Return

    End

  5. #5
    mns45's Avatar
    mns45 Guest


    Did you find this post helpful? Yes | No

    Default drive dc motor with pic 16F877

    Hi, I'm a beginner in this forum.
    I want to control a 12 Volt DC motor in constant speed. To do it, i will use half of H bridge. As i have two switching element, mosfet, i will have two gate signal. I want to do it by 16F877, know i have to learn that how i could convert an analog input to a digital. I have an potentiometer , i change the resistance and i set an analog value and pic understand it.
    The second question. I have to make pwm in constant frequency. Pic take the analog value from its leg (the first analog value for the speed which we set at first ) and there is a feedback system, feedback voltage from tako, pic compares the value comes from tako with we set at first and if neccessary it will change the duty cycle in constant frequency.
    Example: we set 100 mV to drive motor in 1400 rpm

    I want to get some examples in assemble (16F877) for analog digital converter. And i wanna know to make a pulse in constant frequency.
    I look at pic 16F877 datasheet but i think it is hard to solve problem by using datasheet. Some basic example will help me too much. Thanks very much. Please help me...

Similar Threads

  1. 16f877 with LM335 problem
    By CrazyCooter in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 17th April 2015, 06:31
  2. 16F877 HSERIN problem with 3th party software
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 11th March 2009, 17:11
  3. LCD problem with 16F877
    By alexx_57 in forum General
    Replies: 10
    Last Post: - 25th July 2007, 13:47
  4. problem with 16f877 and display
    By Steve S. in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 15th January 2007, 21:53
  5. 16F877, DS18S20 and Serial Comm Problem
    By YellowTang in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 26th April 2004, 10:36

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts