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.

Advice with ADC (TMR2) working with PWM.

Status
Not open for further replies.

MrNobody

New Member
Hi, I am working on ADC/TMR2 with PWM.
Ok.. I want to sample ADC at 8Khz and at the same time, generate PWM signal (which will pass through low pass filter of Fc of 4kHz) to display out the input signal. In another words, the ADC sampled values will become the PWM's duty cycle.

There are 2 methods i have in mind to do that. BTW, I am using PIC18F4620 at 40MHz.

First method:
Prescale = 1:1, Postscale = 1:10 & PR2 = 124 which will generate 8kHz accurately
Since PWM also use the same PR2 and Prescale values, the PWM frequency is 80kHz which means that for every duty cycle, there will be 10 pulses.

Second method:
Prescale = 1:16, Postscale = 1:1, PR2 = 77 which will generate 8.013kHz (which is not 8kHz).
The same values will also generate PWM frequency of 8.013kHz which will generate 1 pulse per duty cycle.

Hmm.. my question is, which method should I use..? The first one will generate a much smoother signal i think but after passing through the low pass filter (using opamp), the output won't be defferent much from the second mathod right..?
Any suggestion ..?
 
Hello,

Are you trying to read a voltage signal at the ADC and then set the duty cycle accordingly? So if you use an 8 bit result and read 255 from the ADC you want 100% duty cycle and 0 will be 0% duty cycle with a linear relation in between?

If you choose option 1, your PWM period will be 13uS and your duty cycle in (CCPRxL:CCPxCON<5:4>) will vary from 0-500 for 0-100%.

If you choose option 2, your PWM period will be 125uS and your duty cycle will take the input of 0-312.5 for 0-100%.

If you are using a 8 bit result from the ADC, I would chose option 1 becuse it is so close to just multiply the ADC result by two to get close to the duty cycle needed...but you could just use a lookup table to make it very accurate.

-Jacob
 
Thanks Jocob for the wonderful suggestion..
I was wondering, because I use 10 bit ADC but actually only collect the 8 MSB bits, can I multiply the MSB byte by 4 so that it become 10 bits but only write the MSB 8 bits into CCPRxL register and ignore the LSB 2 bits in CCPxCON ..? For example, 255 (8 bit) --> 1020 (10 bit) but only keey 8 MSB bits (11111110). I know that the quality might be affected abit.. but is the results still satisfactory..? anybody tried that..?
 
Thanks Jocob for the wonderful suggestion..
I was wondering, because I use 10 bit ADC but actually only collect the 8 MSB bits, can I multiply the MSB byte by 4 so that it become 10 bits but only write the MSB 8 bits into CCPRxL register and ignore the LSB 2 bits in CCPxCON ..? For example, 255 (8 bit) --> 1020 (10 bit) but only keey 8 MSB bits (11111110). I know that the quality might be affected abit.. but is the results still satisfactory..? anybody tried that..?

So, you want to multiply (say) 123 by 4 to get 492 and then only write the top 8 bits which would be 1111011 which is 123. I would guess that would work. You could of course just write the 8 bits from the ADC.

Mike.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top