ADC-PWM

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

ADC-PWM

1,167 次查看
vishaka_maithil
Contributor I

Hi all,

Anyone who can help me out with an example C source code for 12-bit ADC count to (source from potentiometer) PWM generation for pulses??

 

Thankyou in advance.

标签 (1)
0 项奖励
回复
1 回复

850 次查看
bigmac
Specialist III

Hello Visu,

 

For compatibility with 12-bit ADC conversion, your TPM modulo setting will need to be 1023.  This will give a PWM period of 1024 TPM clock cycles.

 

The following code snippet represents the ISR function associated with TPM1 overflow interrupt.  The PWM duty is updated with the result of the previous ADC conversion, and then a new (single) conversion is then started.  Since the PWM period will exceed the ADC conversion period, there is no need to test that the conversion is complete.  The ADC interrupt is not used.

 

// Global variable:volatile byte chan = 0;     // Conversion for ADC ch 0// TPM1 overflow ISR:interrupt void TPM1_ovf( void){   TPM1SC_TOF = 0;          // Clear overflow flag   TPM1C0V = ADCR & 0x0FFF; // Update PWM duty (TPM ch 0)   ADCSC1 = chan & 0x1F;    // Start ADC conversion}

 

Of course, prior to globally enabling interrupts you will need to initialise both ADC and TPM modules.

 

Note that, for this simple example, the PWM duty is the result of a single ADC conversion.  With 12-bit conversion, this will likely be subject to fluctuations due to noise.  With more complex code, it would be possible to reduce the noise fluctuation by setting the PWM duty value to the average of multiple ADC conversions, either with a simple arithmetic average, or by digital filtering of the ADC results (equivalent to single order low-pass filter).  There have been earlier threads on this forum that describe the latter approach.

 

Regards,

Mac

 

0 项奖励
回复