• AVR Freaks

PIC16F877A

Page: 12 > Showing page 1 of 2
Author
kaiser444
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2013/03/25 22:23:46
  • Location: 0
  • Status: offline
2013/03/25 22:39:34 (permalink)
0

PIC16F877A

I wanted to know how to set counters and if the counter for each pin is independent?
#1

23 Replies Related Threads

    NKurzman
    A Guy on the Net
    • Total Posts : 17605
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re:PIC16F877A 2013/03/26 07:36:16 (permalink)
    0
    Could you be a little more vauge?
     
    You mean using the timers as counters?
    If so on you chip the pins are fixed.
    #2
    kaiser444
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2013/03/25 22:23:46
    • Location: 0
    • Status: offline
    Re:PIC16F877A 2013/03/26 10:51:03 (permalink)
    0
    I am using them to measure the discharge times of 8 capacitor circuits each individually connected to 8 pins of the microcontroller.
    I need to measure them individually and at the same time. For example capacitor 1 will take 2000us, capacitor 2 10000us, capacitor 3 780us and so on.
    How do i do that?
    #3
    leon_heller
    Super Member
    • Total Posts : 6411
    • Reward points : 0
    • Joined: 2004/08/17 13:19:45
    • Location: St. Leonards-on-Sea, E. Sussex, UK.
    • Status: offline
    Re:PIC16F877A 2013/03/26 11:01:17 (permalink)
    0
    You can't!

    Leon Heller
    G1HSM

    #4
    NKurzman
    A Guy on the Net
    • Total Posts : 17605
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re:PIC16F877A 2013/03/26 11:24:36 (permalink)
    0
    Can't maybe.
    Your chip has comparitors and shcmitt trigger inputs.  You can run the chip fast and poll the pins.
    The chip does not have enough timers to handle all 3 with internal timers.
    #5
    kaiser444
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2013/03/25 22:23:46
    • Location: 0
    • Status: offline
    Re:PIC16F877A 2013/03/26 12:56:45 (permalink)
    0
    I am using a QTR-8RC Reflectance Sensor Array from http://www.pololu.com/catalog/product/961 to implement a line follower robot

    Do you know a way i can program it using the PIC microcontroller to follow the line?
    #6
    NKurzman
    A Guy on the Net
    • Total Posts : 17605
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re:PIC16F877A 2013/03/26 15:11:34 (permalink)
    0
    What is it looking for? 
    It sounds like the want you to read it not Time it.
    Again you can just start a sample, then sit in a tight loop and count a variable when each line changes save the value to a variable for the Pin.
    #7
    kaiser444
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2013/03/25 22:23:46
    • Location: 0
    • Status: offline
    Re:PIC16F877A 2013/03/26 21:56:32 (permalink)
    0
    I am constructing a line follower for my last year project.
    The robot has QTR-8RC Reflectance Sensor Array as a sensor to detect the a white line on a black surface.
    It is using differential drive steering meaning that it will have independently powered  2 DC motors.
    The speed of both motor vary using PWM to perform turns and straight line driving.
    The idea is to keep the line on the centre of the robot that is at the fourth and fifth phototransistor. If it is to the left of the centre, the left motor will run faster and if to the right the right will run faster.
    But the problem is i don't know how to program it. I was trying to measure the discharge time for each capacitor. If one sensor is above a white line, the discharge time is lower and it is above a black surface, the discharge time is higher.
    Can you tell me how i can detect the line?
    #8
    WaltR
    Super Member
    • Total Posts : 3759
    • Reward points : 0
    • Joined: 2003/11/07 12:38:21
    • Status: offline
    Re:PIC16F877A 2013/03/27 06:19:31 (permalink)
    0
    Look in the Microchip App Notes and the TipsnTricks documents for ideas on measuring the Cap discharge time. Basically you poll a port pin looking for a logic high. Then once the logic high is detected you start a timer/counter (hardware or software) and poll the port pin until a logic low is detected. The timer/counter value is now your discharge time.
     
    Did you carefully read and study the examples on Pololu's web site on how to use that sensor? They provide code examples (for a different processor but in C so this will port to a PIC very easily.
    There was a discussion about doing this in the forums here within the past year or so. Try searching to find that thread.
     
    Also, google "line following robot" for lots of info about ways to do this. These line followers are a very common robot and there is tons of info on the web about them.
    #9
    kaiser444
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2013/03/25 22:23:46
    • Location: 0
    • Status: offline
    Re:PIC16F877A 2013/03/27 08:07:29 (permalink)
    0
    Thank you.
    I already know how to measure the discharge time. But i don't know if i can do it for 8 capacitors simultaneously.
     
    #10
    kaiser444
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2013/03/25 22:23:46
    • Location: 0
    • Status: offline
    Re:PIC16F877A 2013/03/27 23:17:10 (permalink)
    0
    Can we start a timer for the whole sensor array and measure the time for each 8 pins to go low?
    #11
    WaltR
    Super Member
    • Total Posts : 3759
    • Reward points : 0
    • Joined: 2003/11/07 12:38:21
    • Status: offline
    Re:PIC16F877A 2013/03/28 06:43:21 (permalink)
    0
    I would try doing a software polling of all eight sensor inputs (have all eight on the same PIC Port). Then have eight variables to count the decay times.
    The software 'loop' (or a hardware timer 'tick') would read the port, check for logic 1 and increment the corresponding counter. Keep the state of the previous port read so that a change of a port pin can be detected.
    When a port pin changes from low to high clear its counter. if still high on the next port read then increment its counter. When a port pin changes from high to low set a flag bit in a 'counter ready' register so the main processing loop knows a new counter value to available.
    I would write this in assembler so the polling run fast.
     
    Do use the MPLAB simulator to test the coding. The simulator has a Stimulus that can inject port pin levels into the simulator for testing.
    #12
    kaiser444
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2013/03/25 22:23:46
    • Location: 0
    • Status: offline
    Re:PIC16F877A 2013/03/28 07:58:28 (permalink)
    0
    I understand what you mean.
    I am trying to do this:
    Let's say there are 8 fully charged capacitors connected to all 8 pins of portD. Their discharge times will be different. Now i start an internal timer of the microcontroller and let one to go 'low'. I record the time and let others discharge. I record each time of the remaining 7. When the last one goes low, i stop the counter.
    Then i charge again until all are fully charged, reset the timer and repeats the process. Is it possible? I want a 100% sure answer that it will work please.
    #13
    WaltR
    Super Member
    • Total Posts : 3759
    • Reward points : 0
    • Joined: 2003/11/07 12:38:21
    • Status: offline
    Re:PIC16F877A 2013/03/28 09:49:48 (permalink)
    0
    This can easily be done with a PIC16F877.
    Go back and study the Pololu info again. Note the scope picture the the sensor page. There the digital output (as detected by a processor) is 2.65msec. The 16F877 running a 4MHz XTAL runs each instruction cycle at 1MHz or 1usec per instruction. So measuring the decay times of 8 sensors is very doable.
     
    You now need to write some code for the PIC and get the PIC to measure the decay time of one sensor and output to a port pin.
     
     
     
    #14
    Ian.M
    Super Member
    • Total Posts : 13225
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re:PIC16F877A 2013/03/28 11:01:15 (permalink)
    0
    Here's a skeleton algorithm for all eight sensors in C.
      
    unsigned char sensorbit[8], lastD, newD, count;
     
    //...
     
    // *** precharge all sensor caps here
    // *** clear timer here
    count=0;
    LastD=0xFF;
    TRISD=0xFF; // switch to all inputs
    // *** start timer here
    do {
     
       while(lastD==(newD=PORTD); //spin here till next change
     
    // *** store the current timer value in an array at position 'count'
       sensorbit[count]=newD^lastD;  //Store the bit that changed
     
       lastD=newD;
       count++;
    } while(newD); // until whole port is zero

    // *** stop timer here

    You need to write code to replace all comments like  // ***
     
    It should work reasonably well using a hardware timer even in XC8 free mode, but would be much better written in assembler.
    post edited by Ian.M - 2013/03/28 11:03:21
    #15
    kaiser444
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2013/03/25 22:23:46
    • Location: 0
    • Status: offline
    Re:PIC16F877A 2013/03/28 11:21:17 (permalink)
    0
    Can tell me some code on how to use the timer?
    I am doing it in microbasic language
    #16
    kaiser444
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2013/03/25 22:23:46
    • Location: 0
    • Status: offline
    Re:PIC16F877A 2013/03/28 23:04:10 (permalink)
    0
    How can i know how much time it discharges?
    How do i store the reading of the timer for the discharge time of 1 capacitor?
    #17
    mrp
    Super Member
    • Total Posts : 912
    • Reward points : 0
    • Joined: 2012/10/30 01:08:06
    • Location: 0
    • Status: offline
    Re:PIC16F877A 2013/03/28 23:24:40 (permalink)
    +2 (1)
    Do you mean mikroBasic - with a 'k' - from MikroElektronika?  For specific advice regarding mikroBasic, you may need to visit their forum:
    http://www.mikroe.com/forum/
     
    #18
    kaiser444
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2013/03/25 22:23:46
    • Location: 0
    • Status: offline
    Re:PIC16F877A 2013/03/29 08:23:17 (permalink)
    0
    Thank you mrp, Ian.M and WalTR for your help.
    I don't know why my supervisor told me i can't measure all 8 pins with a single timer.
    #19
    Ian.M
    Super Member
    • Total Posts : 13225
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re:PIC16F877A 2013/03/29 12:17:32 (permalink)
    +2 (1)
    Its difficult to measure all pins ACCURATELY with a single timer in software.   Consider what happens if one pin goes low then another one goes low while you are still processing the first pin.  The second pin's timer reading will obviously be delayed so the second time will be longer than it should be.   If you had a lot of 16 bit timers you could stop each timer when the corresponding pin goes low, but the difficult problem would then be starting them at the same time.
     
    To do this *RIGHT* you would need a CCP module for each sensor, but the only 8 bit PICs with enough input capture channels are the enhanced midrange PIC16F1526 and PIC16F1527.  However all the CCP modules would share the same timer which you would start when you change the port direction
    post edited by Ian.M - 2013/03/29 12:24:43
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5