Continue to Site

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.

  • 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.

PwM using microcontroller

Status
Not open for further replies.

RenesasT

Member
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??
 
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)
 
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.
 
Not with the same pin how are you going to keep the adc voltage from driving the load.
 
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
 
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?
 
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.
 
The i/p is PWM and O/p is also PWM.
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.
 
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.
 
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)?
 
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)?
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.
 
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.
 
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.
 
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:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top