Hi,
I'm using S32K144, trying to get average value of sensor signal.
Input sensor signal is varying pulse signal, similar to PWM signal.
I used ADC with Hardware average enabled to get average value of this signal, but I couldn't get constant value, just unstable value which can't be predicted. I need exact average value of signal to determine the system's mode. Do I have to match (sample time)*(samples needed to average) to specific value like pulse signal's period?
For example, if the signal's one cycle has 1ms On & 5ms Off, Do I have to match (sample time)*(32 samples) to 6ms and apply the 32 sample Hardware averaging?
Or Is there any other way using ADC to get constant average value of PWM like signal?
This is how I initialized ADC configuration
void ADC1_init(void)
{
/*!
* ADC1 Clocking:
* ===================================================
*/
PCC->PCCn[PCC_ADC1_INDEX] &=~ PCC_PCCn_CGC_MASK; /* Disable clock to change PCS */
PCC->PCCn[PCC_ADC1_INDEX] |= PCC_PCCn_PCS(1); /* PCS=1: Select SOSCDIV2 */
PCC->PCCn[PCC_ADC1_INDEX] |= PCC_PCCn_CGC_MASK; /* Enable bus clock in ADC */
/*!
* ADC0 Initialization:
* ===================================================
*/
ADC1->SC1[0] |= ADC_SC1_ADCH_MASK; /* ADCH=1F: Module is disabled for conversions */
/* AIEN=0: Interrupts are disabled */
ADC1->CFG1 |= ADC_CFG1_ADIV_MASK
|ADC_CFG1_MODE(1); /* ADICLK=0: Input clk=ALTCLK1=SOSCDIV2 */
/* ADIV=0: Prescaler=1 */
/* MODE=1: 12-bit conversion */
ADC1->CFG2 = ADC_CFG2_SMPLTS(255);
ADC1->SC2 = 0x00000000;
ADC1->SC3 = 0x00000111;
}
And these are functions i used for ADCs.
void convertAdcChan1(uint16_t adcChan)
{
ADC1->SC1[0]&=~ADC_SC1_ADCH_MASK; /* Clear prior ADCH bits */
ADC1->SC1[0] = ADC_SC1_ADCH(adcChan); /* Initiate Conversion */
}
uint8_t adc_complete1(void)
{
return ((ADC1->SC1[0] & ADC_SC1_COCO_MASK)>>ADC_SC1_COCO_SHIFT); /* Wait for completion */
}
uint32_t read_adc_chx1(void)
{
uint16_t adc_result=0;
adc_result=ADC1->R[0]; /* For SW trigger mode, R[0] is used */
return (uint32_t) ((5000*adc_result)/0xFFF); /* Convert result to mv for 0-5V range */
}