PWM using controller

Status
Not open for further replies.

RenesasT

Full Member level 2
Joined
Mar 11, 2016
Messages
149
Helped
0
Reputation
0
Reaction score
0
Trophy points
16
Visit site
Activity points
1,330
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??
 

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.

What do you mean by according to the duty of input pwm ? Are you inputting pwm signal to analog pin ?

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.

How do you plan to multiplex the pin in hardware ?

If a circuit is built to switch the pin between input and output function circuits then it can be made.
 

Hello Baileychic,


Thanks for yout prompt reply.

I have option of using jumpers. But does it possible without jumper.

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?
 

A circuit like this can be used.

Edit:

There was a mistake in the circuit during drawing. Use GP2 pin.
 

Attachments

  • adc and pwm - 1 pin.png
    20.4 KB · Views: 265
Last edited:

Hello baileychic,

I would like to tell you the i/p as PWM and o/p as also PWM.

The frequency of i/p PWM user dependable.

The frequency of o/p PWM is fixed i.e 100Hz.

Please tell me how I can vary the duty o/p PWM according to the duty of i/p PWM.
 

Ok. I understood wrongly. Yes, it can be done using Timer Interrupt or Input Capture.

Pin should be initially configured as input and then duty of the input PWM has to be found and then assign this pwm duty to output pwm and then make the pin as output pin.

I quickly made the ADC and PWM function on the same pin.

Here is the code. Project file attached.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
sbit Led at GP2_bit;
sbit Led_Direction at TRISIO2_bit;
 
unsigned int pwm_duty = 0, old_pwm_duty = 256;
 
void Configure_Pin_As_Analog_Input() {
    Led_Direction = 1;
    ADCON0 = 0x80;
    ANSEL = 0x54;
    Delay_ms(2);
}
 
void Configure_Pin_As_Digital_Output() {
    Led_Direction = 0;
    ANSEL = 0x50;
}
 
void main() {
 
    CMCON0 = 0x07;
    ANSEL = 0x00;
    
    TRISIO = 0x00;
    GPIO = 0x00;
    
    Delay_ms(100);
    
    PWM1_Init(2000);
    PWM1_Set_Duty(0);
    PWM1_Start();
    
    while(1) {
    
          Configure_Pin_As_Analog_Input();
          pwm_duty = ADC_Read(2) >> 2;
          
          if(old_pwm_duty != pwm_duty) {
              PWM1_Set_Duty(255 - (unsigned short)(pwm_duty));
              old_pwm_duty = pwm_duty;
          }
          
          Configure_Pin_As_Digital_Output();
          
          Delay_ms(100);
    }
}



I will try to make a code for your requirement. According to my PIC cannot output a PWM of 100 Hz. Maybe timer interrupt has to be used to generate the pwm. Which controller are you using ?

Report bugs.


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.

If different pins for input and output then it can be easily made. I need to know what controller you are using and what is its clock frequency to make a code for you.

In the above code replace

Code:
Delay_ms(100);

with

Code:
Delay_ms(10);

- - - Updated - - -

For your requirement you can use this circuit which I made.

External Interrupt INT function is magically on the same pin as adc input, pwm input and pwm output pin.

You have to use external interrupt with Timer Interrupt.

When external pin detects low to high transistion then start timer and change edge detect bit to detect high to low transistion and when again high to low transistion is detected then stop timer interrupt and disable external interrupt. Now using the value of Timer ISR counter and TMRxx register values calculate the pulse high width. From this calculate the duty of the input pulse (pwm) and then assign it to pwm duty setting function and make the pin as output pin.

Due to 4V reverse breakdown voltage of Led I used 3.3V for the circuit and also 3.3V PIC12LF.
 

Attachments

  • adc and pwm - 1 pin - update 1.png
    19.6 KB · Views: 264
  • ADC and PWM.rar
    34.4 KB · Views: 117
  • adc + pwm to pwm.png
    20.8 KB · Views: 151
Last edited:

Hello,

I am using ATtiny10 which is ATmel controller.

Do you have a code for i/p as a PWM and O/p is also a PWM.
 

I am not much familiar with AVR controllers. I mainly use PIC controllers. Tonight I will try to make ATTiny10 code for your requirement. Right now to implement the requirement I am using PIC12LF1840.

I want to know what is the min and max input pwm frequencies ?

In PIC we cannot generate output pwm of < 500 Hz. So, I can't implement 300 Hz pwm output in PIC micrcontrollers. For testing the logic of the code I will generate 1 KHz PWM output.

This is the latest circuit. Leds have reverse breakdown voltages of 4V. If you use Led for testing then you have to consider that with my circuit. So, it is better to use 3.3V supply whether it is PIC or AVR for the circuit of your requirement.

There is a bug in the circuit. I have to find it. In PWM input mode I am not seeing any PWM signal at NODE.

I want to know few more things before writing the code. You say POT for analog input and PWM input are both connected to the same pin. I want to know whether there is a switch to switch between analog input and pwm input or are they always connected together ?

Do you want both analog input and pwm input ? If yes, do you want both inputs on the same pin ?

Do you want output pin to be same as input pin or can I use different input and output pins ?

- - - Updated - - -

Finally got some success with the circuit. Why if D4 is connected in reverse w.r.t that shown in circuit (update 3) then I don't get input pwm at node ?

What additional multilayer ceramic capacitor can I use in series with PIN to condition the input and output PWMs ?

If SW1 is removed then additional capacitor is needed at PIN. Also if SW1 is removed and Analog input and PWM input are directly connected then when pot output is connected to ground then PWM output (3.3V) shorts to ground. To avoid this I have to add a small value resistor between PWM source and D4 zener. Is this correct ? or will the zener itself will protect.

This is a crude circuit which I designed.
 

Attachments

  • adc + pwm to pwm - update 2.png
    29.9 KB · Views: 218
  • adc + pwm to pwm - update 3.png
    103.2 KB · Views: 159
Last edited:

Hello baileychic ,

Thanks for continuously being with me.

Let me tell you all ans,

ATtiny 10 is able to generate frequency of 100Hz.

Please forget the operation of Analog signal(forget about switch).I have done analog part.

Please concentrate on PWM i/p and PWM o/p.

The max i/p pwm freqency is 1Hz-1kHz. O/p pin and I/p pin are different.

Please see below example which will clear your all doubts ,

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

The i/p PWM frequency can be anything but the o/p PWM frequency should be of 100Hz. The duty of o/p PWM should be exactly same as the duty of i/p PWM.

Please give me the code based on ATtiny10.
 

Ok. Now things are clear. I will provide C code.

For your understanding then coding should be like this.

Your max input frequency is 1 KHz. Take it as 1.1 KHz

fi = 1.1 KHz

Ti = 1/Fi = 0.909 ms

You need to create a Timer Interrupt of 9.09 ms.

Don't yet start the timer.

Use INT pin or External Interrupt function pin for inputting the PWM.

Configure to detect low to high transistion which causes an external interrupt.

When this occurs toggle the edge detect bit and start timer. The timer ISR should have a counter variable of unsigned long type which is incremented in the Timer ISR.

Now when another external interrupt occurs save the timer register and counter values and then toggle the edge detect bit again.

Now when 3rd external interrupt occurs stop the timer and save the timer register and counter values.

Now using the two counter values and the two saved timer register values and doing maths find out the pulse high time and low time and from this calculate the pwm duty. Duty value will be 0 to 100. convert this to 0 to 255 value (as I am using mikroC PRO AVR Compiler, The PWM library function uses 0 to 255 for pwm duty argument) and assign it to output PWm.

So, the duty of the input PWM will be applied to output PWM.

I am not familiar with AVR. I mainly use PIC. There may be other better and simpler method of implementing the requirement in AVR.

I will try to implement the above mentioned method in mikroC PRO AVR.
 

I read the datasheet of the ATTiny10 and you can use Input Capture feature to read the input pwm and get the duty_cycle of the pwm and then you have to use output compare to generate PWM of 100 Hz. I am trying to make a code. If I succeed I will post the code.

Edit:

Have you seen this ? It will be helpful for your project.

https://electronut.in/attiny10-hello/

- - - Updated - - -

This will be a good document to start with (see attached document).
 

Attachments

  • Atmel-8014-Using-Timer-Capture-to-Measure-PWM-Duty-Cycle_ApplicationNote_AVR135.pdf
    226 KB · Views: 252
Last edited:

Hello baileychic,

in datasheet of ATtiny10 it is mentioned that it has one timer.

Does my project is possible.?
 

Hello,

Sorry ,i don't know ATMEL MCU ..

...According to my PIC cannot output a PWM of 100 Hz.

i did some test on low frequency PWM with a PIC12F1840
with MikroE PWM library it is not possible ..
but if we overwrite the configuration of Timer2 ..

so don't use PWM1_Init(xxxx)
// test PWM Overwriting Timer2 config
// at 16MHz mini=4mS 250Hz
// at 8MHz mini=8ms 125Hz
// at 4MHz mini=16mS 62.75Hz


F=62,5Hz

// PWM1_Init(1000L);
// over Init Timer2
T2OUTPS0_bit=1; // postscaleer =1/16
T2OUTPS1_bit=1;
T2OUTPS2_bit=1;
T2OUTPS3_bit=1;
T2CKPS1_bit=1; //prescaler select=11 => 1/64
T2CKPS0_bit=1;
PWM1_Set_Duty(Ratio);
PWM1_Start();

duty cycle set by PWM1_Set_Duty(Ratio);
with Ratio 0 à 255 => 0 100%
 

in datasheet of ATtiny10 it is mentioned that it has one timer.

Does my project is possible.?

Yes, even I saw that earlier that it has only one 16 bit timer. I have to read the datasheet in detail and also read application notes.

I will see if there is a way to do what you want. I am not familiar with CodeVisionAVR. I thought to write code in mikroC PRO AVR but that Compiler doesn't support your device and so I have to get familiar with CodeVisionAVR and write the code.
 

Hello paulfjujo and baileychic,

I am able to generate the operation o/p PWM of 100Hz.

Suppose I am taking analog 0-5V as a i/p then I am able to generate 100Hz frequency on other port of controller i.e o/p with duty according to i/p voltage.

Now the o/p is 100Hz frequency(which I can generate). The problem is that now, i/p is PWM square wave. According to duty at i/p , I wanted to set the duty at o/p.

The ATMEL ATtiny 10 datasheet show, it has only one timer.

I dont know how to capture duty at i/p.

For coding ATtiny10, AVR Studio is used.

- - - Updated - - -

Sorry for miss spelling.

For coding ATtiny10, Atmel Studio is used.
 

Can you use additional circuit if ATTiny doesn't allow what you need ? Like using a frequency to voltage converter and scaling the voltage using OpAmp and then feeding it to ADC channel 2 and generate PWM after converting the ADC value to PWM duty ?
 

Ok. I will read the application notes and check some examples and see if it can be done because Timer is used for generating PWM.
 

Yes, I generated PWM from PB0 pin of ATTiny25 but frequency is 4KHz. I have to make changes to get 100 Hz PWM after that I can try to capture input PWM and find its duty and assign it to output PWM. I will reply in 1 or 2 days.

Can you use PIC12F ? If cost is the only issue then can you use PIC12F. There are a few PIC12F which are cheaper than ATTiny25.
 
Last edited:

ATTiny25 doesn't have ICPx pin. INPx pin is needed for input capture to measure PWM duty. ATTiny25 has 2 Timers. I guess Timer1 will be used for PWM generation. So, Timer0 has to be used with INT0 pin to find out duty of input PWM.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…