1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

PwM using microcontroller

Discussion in 'Microcontrollers' started by RenesasT, Aug 14, 2017.

  1. RenesasT

    RenesasT Member

    Joined:
    Jul 10, 2017
    Messages:
    30
    Likes:
    0
    Hello All,

    I am working on one project which requires following condition.

    Analog voltage and PWM on same pin of microcontroller.

    Does it possible??

    Currently I am using one pin of microcontroller. If analog voltage at that pin is 0-5V . I am converting 0-5V from internal ADC and getting values between 0-1023 as my adc is of 10 bit. Using those values, I am generating PWM and varying duty according to analog value.

    Now I wanted to use the same pin which should accept pwm and according to the duty cycle of i/p pwm it should vary the duty cycle of o/p Pwm.

    In case I am using analog then it should work as analog control and if I am using PWM then it should work as PWM control.

    Does it possible using one pin.

    Please let me know How??
     
  2. ronsimpson

    ronsimpson Well-Known Member Most Helpful Member

    Joined:
    Apr 17, 2007
    Messages:
    7,254
    Likes:
    961
    Location:
    Loveland, CO USA
    ONLINE
    Assume: Pin(X) connects to the PWM in the microcontroller and it also connects to the ADC.
    Only one can be used at a time. (pin needs to be set as input or output)

    Turn on PWM and set to 50%. Set pin (x) to output. Now the pin will be 0V or 50% and 5V or 50%.
    Normally a resistor and capacitor is connected to the pin to filter the output and make a DC voltage. In this example 2.5V.
    After some time:
    Set pin(x) to input, set to analog mode.
    Point ADC to Pin(x)
    Read ADC. Value should be about 512 or 1/2 of max value. The ADC is reading, through the resistor, to the capacitor that stores a voltage. The capacitor will hold the voltage for a short time. (maybe even seconds is there is no load)
     
  3. RenesasT

    RenesasT Member

    Joined:
    Jul 10, 2017
    Messages:
    30
    Likes:
    0
    Hello ,

    There are two methods of controlling smaller LED.

    1) Using ADC
    2) Using PWM

    In first method, suppose on P1.0 the i/p is 0-5V and I will convert that into analog value from 0-1023 as my internal ADC is of 10 bit. According to the ADC value, I am generating PWM of 100Hz on P1.1 which is connected to LED. So the ADC value on P1.0 will decide the dutycycle of PWM of P1.1.

    In second method, the i/p is square wave PWM at P1.0. The frequency of i/p PWM is user dependable. I wanted to capture the duty cycle of i/p PWM at P1.0 and according to generate PWM of 100Hz on P1.1 which is connected to LED. So the duty cycle of PWM on P1.0 will decide the dutycycle of PWM of P1.1.

    I wanted to use only one pin as i/p and other pin as o/p.

    The o/p operation is same to generate PWM of 100Hz whose duty is depends upon the i/p.

    But for i/p , How I can do?

    Hope you understood my question.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,807
    Likes:
    135
    Location:
    morristown,tn

    Not with the same pin how are you going to keep the adc voltage from driving the load.
     
  6. ronsimpson

    ronsimpson Well-Known Member Most Helpful Member

    Joined:
    Apr 17, 2007
    Messages:
    7,254
    Likes:
    961
    Location:
    Loveland, CO USA
    ONLINE
    It sounds like you ran out of pins and want to do two things on one pin.
    uP is on the left side. +5V, IO pin, ground
    When the pin is set to output; the pin over drives the pot (1k & 10k) or could (10k & 10K). Then the pin is high the LED is on, when the pin is low the LED is off.
    When the pin is set to input; the voltage will be set by the POT. The MOSFET will be on or off depending on the POT setting.
    The pin will be in input mode much less than 1% of the time. Last time I did this I checked the POT voltage only once / second.
    upload_2017-8-14_19-36-17.png
     
  7. RenesasT

    RenesasT Member

    Joined:
    Jul 10, 2017
    Messages:
    30
    Likes:
    0
    For this moment, forget about analog control.

    Suppose I am using i/p PWM signal at microcontrollers pin and now I wanted to o/p PWM at other microcontroller pin. If duty of i/p pwm is changed then duty of o/p pwm should changed.

    How to achieve this condition?
     
  8. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,228
    Likes:
    641
    Location:
    Derbyshire, UK
    I'm rather bemused you're asking a question that has such an obvious answer?.

    You read the ten bit input value of the in-coming PWM, and transfer that value to the out-going PWM registers. Assuming you want to alter the value in some way?, then perform the required maths on the value before transfer.
     
  9. RenesasT

    RenesasT Member

    Joined:
    Jul 10, 2017
    Messages:
    30
    Likes:
    0
    Hello,

    How to read 10 bit value from incoming PWM.
     
  10. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,228
    Likes:
    641
    Location:
    Derbyshire, UK
    Consult the datasheet.
     
  11. RenesasT

    RenesasT Member

    Joined:
    Jul 10, 2017
    Messages:
    30
    Likes:
    0
    hello Nigel,

    The i/p is PWM and O/p is also PWM.

    The i/P is not analog voltage.
     
  12. ronsimpson

    ronsimpson Well-Known Member Most Helpful Member

    Joined:
    Apr 17, 2007
    Messages:
    7,254
    Likes:
    961
    Location:
    Loveland, CO USA
    ONLINE
    You made the output PWM signal. So you should know what the value is. Why do you need to read it back in?

    If you really need to read a PWM in. (digital not analog) Use a counter timer.
    I think you never told us what computer so we can not help on the details.
     
  13. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,228
    Likes:
    641
    Location:
    Derbyshire, UK
    PIC's (and many others) have input pins with hardware for reading pulse width inputs.
     
  14. tomizett

    tomizett Active Member

    Joined:
    Aug 11, 2013
    Messages:
    419
    Likes:
    45
    Location:
    UK
    I think we're at cross-purposes here; I believe what the OP wants to do is use a single pin as an INPUT to EITHER:
    Read an analogue voltage as an ADC from an external source
    OR
    Read to duty cycle of an incoming PWM from an external source

    The output is on a different pin (P1.1 from #3 above) and is not relevent to the problem.
    RenesasT, does that aagree with that you are trying to do?

    If that's the case, then simply using an RC lowpass network to smooth the incoming PWM would alow the pin to be used in ADC mode all the time, by filtering the incoming PWM to an analogue voltage.
    If getting the ripple low enough means that the response time is too slow (bearing in mind that the lowpass filter would still be present even when the input was an analogue voltage), then you will need to look at measuring the PWM period directly.

    This would ideally involve using a pin that could be both an ADC and a Timer input. The exact details would depend upon the micro you're using (and there are others here better qualified than I to explain how to do it).

    If you don't have a pin that can be both ADC and Timer Input (or you cant route the same signal to two sepperate pins for these functions) then you /might/ be able to try reading the PWM period in software.

    I hope my reading of the problem is correct and, if so, that it's added some clarity.
     
  15. tomizett

    tomizett Active Member

    Joined:
    Aug 11, 2013
    Messages:
    419
    Likes:
    45
    Location:
    UK
    Re-reading #12, I think Nigel has read the same understanding of the the problem.
    Presumably, then, the micro doesn't have a PWM input, or the OP is not aware of it (I wasn't aware of such things until I read that post)?
     
  16. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,897
    Likes:
    502
    Location:
    Cleveland, OH, USA
    ONLINE
    Agreed.

    All the OP needs to do is look at the appropriate table(s) for whichever MCU it is using and find a pin that does ADC and CCP. For the PIC series that have that ability, that is usually Table 1, as in the PIC16F1825/1829 series of chips. RA2 might be a good choice, but there are others:

    upload_2017-8-16_17-16-41.png

    AN2 = ADC, CCP3 allows capture of the PWM, and TMR1 is 16 bit.
     
  17. RenesasT

    RenesasT Member

    Joined:
    Jul 10, 2017
    Messages:
    30
    Likes:
    0
    Hello tomizett and Jpanhalt,

    I got your point of low pass filter. I have done those and I am finding fluctuations in o/p of low pass filter.

    I will use separate pin for analog i/p and separate pin for PWM i/p.

    For analog i/p I will do it. But for PWM i/p , how to measure the duty of i/p PWM.

    Also one more point, I am using ATtiny 10 Atmel IC now it is under PIC.

    I read the datasheet, It has one 16 bit timer. I have to capture i/p PWM also i have to generate o/p PWM.

    Dose my operation is possible.
     
  18. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,897
    Likes:
    502
    Location:
    Cleveland, OH, USA
    ONLINE
    If you look at a typical PWM signal:
    upload_2017-8-17_5-48-52.png

    You see that duty cycle (i.e., % "on" time) is the time for 1-->2 divided by the time from 1-->3. Now, you could start and stop a timer to get those values. There are discussions on the internet about how to minimize and/or correct for lost counts that occur with those on/off changes.

    The CCP module avoids that by capturing the counter time (usually TMR1 ) that the signal goes low to high and high to low. That counter runs continuously, and those values are captured in special registers without interrupting the clock. You then move those register contents to registers you use in your calculations. Then a couple of subtractions can give you the high time, low time, and total time (i.e., high + low) from which you can calculate the duty cycle percentage, if necessary. Note that except for the very first cycle, time at #3 becomes the time at #1 for the next cycle, so you only need to capture sequential times for each low-to-high and high-to-low transition. The rest is math. If the frequency is fixed, it becomes even easier, as you need only to calculate one interval.

    The counter can rollover once in a timing interval and the math should handle that. You can set the pre- and post-scaler values to avoid more frequent rollovers.
     
  19. RenesasT

    RenesasT Member

    Joined:
    Jul 10, 2017
    Messages:
    30
    Likes:
    0
    Hello Jpanhalt,

    Can you please explain me in more details.
    How to do it by programmatic way .
     
  20. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,897
    Likes:
    502
    Location:
    Cleveland, OH, USA
    ONLINE
    Which controller, which pins, what frequency of PWM, what frequency will the controller be running, and what language? I only do MPASM, so my code, particularly for the math, probably won't help you. But the set up of CCP and TMR1 for C and Assembly is should be the same.
     
    Last edited: Aug 17, 2017

Share This Page