2021-12-11 02:26 AM
uint16_t val[200];
unsigned char data[20];
uint32_t AVG_val[10];
uint16_t val_2=0;
unsigned char count=0;
double rms=0.0;
uint32_t square = 0;
double mean = 0;
double root=0.0;
int arr[]={15,78,59,85,56};
int n=100;
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC3_Init();
MX_USART1_UART_Init();
HAL_Delay(3000);
while (1)
{
while(1){
HAL_ADC_Start(&hadc3);
HAL_ADC_PollForConversion(&hadc3,1000);
val_2 = HAL_ADC_GetValue(&hadc3); //i=6;
val[count] = (2500*val_2)/65535;
HAL_ADC_Stop(&hadc3);
HAL_Delay(1);
count++;
if(count==99){count=0;break;}
}
/* while(1){
data[0]=val[count]/10000+48;
val[count] = val[count]%10000;
data[1]=val[count]/1000+48;
val[count] = val[count]%1000;
data[2]=val[count]/100+48;
val[count] = val[count]%100;
data[3]=val[count]/10+48;
data[4] = val[count]%10+48;
count++;
HAL_UART_Transmit (&huart1, data, sizeof (data), 10);
HAL_UART_Transmit (&huart1, "\r\n", 2, 10);
HAL_Delay(1);
if(count==99){count=0;break;}*/
square = 0;
for (int i = 0; i < 100; i++) {
square += pow(val[i], 2);
}
//sprintf(data,"%ld",square);
// HAL_UART_Transmit (&huart1, data, sizeof (data), 10);
// HAL_UART_Transmit (&huart1, "\r\n", 2, 10);
// Calculate Mean.
mean = (389753638 / (float)(100));
sprintf(data,"%lf",mean);
HAL_UART_Transmit (&huart1, data, sizeof (data), 10);
HAL_UART_Transmit (&huart1, "\r\n", 2, 10);
// Calculate Root.
// root = sqrt(mean);
// sprintf(data,"%lf",root);
//HAL_UART_Transmit (&huart1, data, sizeof (data), 10);
// HAL_UART_Transmit (&huart1, "\r\n", 2, 10);
}
HAL_Delay(2000);
/* USER CODE END 3 */
}
i am trying to read RMS value, i cant get the mean value .
so try to get mean value of constant, it printed only null value.
what wrong with this line i dont understand. i thought this is because of data size.
but thesame problem with double.
thanking you
Solved! Go to Solution.
2021-12-11 05:19 AM
Maybe first miss unhandled uint16 overload
val[count] = (2500*val_2)/65535;
secon overload
for (int i = 0; i < 100; i++) {
square += pow(val[i], 2);
}
and next issue is sprintf float values, try print %d.%d and calculate parts.
mean = 389753638.0 / 100.0;
2021-12-11 03:32 AM
Use a debug probe, put a breakpoint at the beginning, use step by step, run to cursor, step over, etc.. to find out what is wrong.
When doing calculations, variable type, mix and max values are to be checked out.
I would reccomend to start with float
2021-12-11 05:19 AM
Maybe first miss unhandled uint16 overload
val[count] = (2500*val_2)/65535;
secon overload
for (int i = 0; i < 100; i++) {
square += pow(val[i], 2);
}
and next issue is sprintf float values, try print %d.%d and calculate parts.
mean = 389753638.0 / 100.0;
2021-12-14 05:03 AM
the problem with sprintf function.
thanking you
2021-12-25 04:02 PM
pow(val[i], 2)
C'mon, you do know how to square a number with simple methods? pow() is a sophisticated and CPU intensive universal function for real numbers. Using it for integers is a waste of resources and even can be imprecise.