implemented DMA-based averager. Avg res is dumped to USB_VCP. But it is not compiles
This commit is contained in:
@ -59,6 +59,14 @@ static void MX_ADC1_Init(void);
|
|||||||
|
|
||||||
/* Private user code ---------------------------------------------------------*/
|
/* Private user code ---------------------------------------------------------*/
|
||||||
/* USER CODE BEGIN 0 */
|
/* USER CODE BEGIN 0 */
|
||||||
|
struct ADC_proc{
|
||||||
|
uint8_t status; // 0 - stopped, 1 - started-filling, 2 - filled
|
||||||
|
uint32_t sum;
|
||||||
|
uint32_t avg;
|
||||||
|
uint32_t N;
|
||||||
|
} adc_process;
|
||||||
|
|
||||||
|
|
||||||
#define ADC_BUFF_SIZE 100
|
#define ADC_BUFF_SIZE 100
|
||||||
uint16_t ADC1_buff_circular[ADC_BUFF_SIZE];
|
uint16_t ADC1_buff_circular[ADC_BUFF_SIZE];
|
||||||
/* USER CODE END 0 */
|
/* USER CODE END 0 */
|
||||||
@ -98,6 +106,11 @@ int main(void)
|
|||||||
/* USER CODE BEGIN 2 */
|
/* USER CODE BEGIN 2 */
|
||||||
HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_SET);
|
HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_SET);
|
||||||
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)ADC1_buff_circular, ADC_BUFF_SIZE);
|
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)ADC1_buff_circular, ADC_BUFF_SIZE);
|
||||||
|
adc_process.status = 0; // ADC started
|
||||||
|
adc_process.N = 0;
|
||||||
|
adc_process.sum = 0;
|
||||||
|
adc_process.avg = 0;
|
||||||
|
|
||||||
/* USER CODE END 2 */
|
/* USER CODE END 2 */
|
||||||
|
|
||||||
/* Infinite loop */
|
/* Infinite loop */
|
||||||
@ -106,7 +119,31 @@ int main(void)
|
|||||||
{
|
{
|
||||||
HAL_GPIO_TogglePin(LED_RED_GPIO_Port, LED_RED_Pin);
|
HAL_GPIO_TogglePin(LED_RED_GPIO_Port, LED_RED_Pin);
|
||||||
HAL_Delay(100);
|
HAL_Delay(100);
|
||||||
|
|
||||||
|
if (adc_process.status == 2) {
|
||||||
|
adc_process.avg = adc_process.sum / adc_process.N;
|
||||||
|
adc_process.status = 1; // reset for next accumulation
|
||||||
|
adc_process.sum = 0;
|
||||||
|
adc_process.N = 0;
|
||||||
|
char digits[10] = {0};
|
||||||
|
digits[0] = (adc_process.avg / 1000000000) % 10 + '0';
|
||||||
|
digits[1] = (adc_process.avg / 100000000) % 10 + '0';
|
||||||
|
digits[2] = (adc_process.avg / 10000000) % 10 + '0';
|
||||||
|
digits[3] = (adc_process.avg / 1000000) % 10 + '0';
|
||||||
|
digits[4] = (adc_process.avg / 100000) % 10 + '0';
|
||||||
|
digits[5] = (adc_process.avg / 10000) % 10 + '0';
|
||||||
|
digits[6] = (adc_process.avg / 1000) % 10 + '0';
|
||||||
|
digits[7] = (adc_process.avg / 100) % 10 + '0';
|
||||||
|
digits[8] = (adc_process.avg / 10) % 10 + '0';
|
||||||
|
digits[9] = (adc_process.avg / 1) % 10 + '0';
|
||||||
|
|
||||||
|
CDC_Transmit_FS((uint8_t *)"ADC Average calculated: ", 24);
|
||||||
|
CDC_Transmit_FS((uint8_t *)digits, 10);
|
||||||
|
CDC_Transmit_FS((uint8_t *)"\r\n", 2);
|
||||||
|
|
||||||
|
}
|
||||||
CDC_Transmit_FS((uint8_t *)"Hello from STM32!\r\n", 19);
|
CDC_Transmit_FS((uint8_t *)"Hello from STM32!\r\n", 19);
|
||||||
|
|
||||||
/* USER CODE END WHILE */
|
/* USER CODE END WHILE */
|
||||||
|
|
||||||
/* USER CODE BEGIN 3 */
|
/* USER CODE BEGIN 3 */
|
||||||
|
|||||||
@ -58,7 +58,7 @@
|
|||||||
extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
|
extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
|
||||||
extern DMA_HandleTypeDef hdma_adc1;
|
extern DMA_HandleTypeDef hdma_adc1;
|
||||||
/* USER CODE BEGIN EV */
|
/* USER CODE BEGIN EV */
|
||||||
|
extern struct ADC_proc adc_process;
|
||||||
/* USER CODE END EV */
|
/* USER CODE END EV */
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
@ -232,6 +232,11 @@ void OTG_FS_IRQHandler(void)
|
|||||||
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
|
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
|
||||||
{
|
{
|
||||||
HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_SET);
|
HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_SET);
|
||||||
|
for (uint32_t i = ADC_BUFF_SIZE/2; i < ADC_BUFF_SIZE; i++) {
|
||||||
|
adc_process.sum += ADC1_buff_circular[i];
|
||||||
|
}
|
||||||
|
adc_process.N += ADC_BUFF_SIZE - ADC_BUFF_SIZE/2;
|
||||||
|
adc_process.status = 2; // buffer filled
|
||||||
// This function is called when the first half of the ADC buffer is filled
|
// This function is called when the first half of the ADC buffer is filled
|
||||||
// You can process the first half of ADC1_buff_circular here
|
// You can process the first half of ADC1_buff_circular here
|
||||||
}
|
}
|
||||||
@ -243,6 +248,11 @@ void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
|
|||||||
//HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_RESET);
|
//HAL_GPIO_WritePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin, GPIO_PIN_RESET);
|
||||||
|
|
||||||
HAL_GPIO_TogglePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin);
|
HAL_GPIO_TogglePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin);
|
||||||
|
for (uint32_t i = 0; i < ADC_BUFF_SIZE/2; i++) {
|
||||||
|
adc_process.sum += ADC1_buff_circular[i];
|
||||||
|
}
|
||||||
|
adc_process.N += ADC_BUFF_SIZE/2;
|
||||||
|
|
||||||
// This function is called when the first half of the ADC buffer is filled
|
// This function is called when the first half of the ADC buffer is filled
|
||||||
// You can process the first half of ADC1_buff_circular here
|
// You can process the first half of ADC1_buff_circular here
|
||||||
}
|
}
|
||||||
|
|||||||
1877
build/main.lst
1877
build/main.lst
File diff suppressed because it is too large
Load Diff
BIN
build/main.o
BIN
build/main.o
Binary file not shown.
Reference in New Issue
Block a user