Structure for command of control ADC start & setting is sting with 6 byte
1 - set/reset autostart for ADC conversion ( 1 - single start ADC, other - autostart)
2 - HI byte for delay (msec) ADC start (hex mode)
3 - LO byte for delay(msec) ADC start (hex mode)
4 - HI byte for ADC sample number (hex mode)
5 - LO byte for ADC sample number (hex mode)
6 - 0xFF - end byte
Structure for ADC send result of measurement
1 - HI digit of ADC result in ASCII format (0x30...0x39)
1 - MI digit of ADC result in ASCII format (0x30...0x39)
1 - MI digit of ADC result in ASCII format (0x30...0x39)
1 - LO digit of ADC result in ASCII format (0x30...0x39)
5 - 0x0D
6 - 0x0A
Keil code for main.c
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f0xx_hal.h"
#include "adc.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"
/* USER CODE BEGIN Includes */
#define ADC_0V_VALUE 0
#define ADC_1V_VALUE 1241
#define ADC_2V_VALUE 2482
#define ADC_3V_VALUE 3723
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
uint8_t uart_tx[7]={0,0,0,0,0,0xD,0xA};
uint32_t adcResultall,adcResult = 0;
uint32_t iii,adc_samlpe=1000,autostart=0,adc_delay=1;
uint32_t i_0, i_1, i_2;
uint8_t TxData[6]={0};
uint8_t RxData[6]={0};
uint8_t aaa[5]={0};
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
void adc_string(uint32_t adc)
{
//uint8_t aaa[5]={0};
aaa[0]=adc/10000;
aaa[0]=aaa[0] &0xF;
uart_tx[0]=aaa[0]|0x30;
aaa[1]=(adc-aaa[0]*10000)/1000;
aaa[1]=aaa[1] &0xF;
uart_tx[1]=aaa[1]|0x30;
aaa[2]=(adc-aaa[0]*10000-aaa[1]*1000)/100;
aaa[2]=aaa[2] &0xF;
uart_tx[2]=aaa[2]|0x30;
aaa[3]=(adc-aaa[0]*10000-aaa[1]*1000-aaa[2]*100)/10;
aaa[3]=aaa[3] &0xF;
uart_tx[3]=aaa[3]|0x30;
aaa[4]=(adc-aaa[0]*10000-aaa[1]*1000-aaa[2]*100-aaa[3]*10);
aaa[4]=aaa[4] &0xF;
uart_tx[4]=aaa[4]|0x30;
uart_tx[5]=0x0D;
uart_tx[6]=0x0A;
}
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_UART_Receive_DMA(&huart1, RxData, 6);
if(RxData[5]==0xFF) //
{
RxData[5]=0x00;
adc_samlpe = 256*RxData[3] + RxData[4];
adc_delay = 256*RxData[1] + RxData[2];
adcResult = 0;
adcResultall = 0;
HAL_Delay(1+adc_delay);
for(i_1=0;i_1 < adc_samlpe+1; i_1++)
{
HAL_ADC_Start(&hadc);
HAL_ADC_PollForConversion(&hadc, 1000);
adcResult = adcResult + HAL_ADC_GetValue(&hadc);
HAL_ADC_Stop(&hadc);
}
adcResultall = adcResult/(adc_samlpe+1);
adc_string(adcResultall);
TxData[0] = uart_tx[1];
TxData[1] = uart_tx[2];
TxData[2] = uart_tx[3];
TxData[3] = uart_tx[4];
TxData[4] = 0x0D;
TxData[5] = 0x0A;
HAL_UART_Transmit_DMA(&huart1, TxData, 6);
for(i_2=0; i_2 <= 5; i_2++)
{
RxData[i_2] = 0;
}
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
1 - set/reset autostart for ADC conversion ( 1 - single start ADC, other - autostart)
2 - HI byte for delay (msec) ADC start (hex mode)
3 - LO byte for delay(msec) ADC start (hex mode)
4 - HI byte for ADC sample number (hex mode)
5 - LO byte for ADC sample number (hex mode)
6 - 0xFF - end byte
Structure for ADC send result of measurement
1 - HI digit of ADC result in ASCII format (0x30...0x39)
1 - MI digit of ADC result in ASCII format (0x30...0x39)
1 - MI digit of ADC result in ASCII format (0x30...0x39)
1 - LO digit of ADC result in ASCII format (0x30...0x39)
5 - 0x0D
6 - 0x0A
Keil code for main.c
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f0xx_hal.h"
#include "adc.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"
/* USER CODE BEGIN Includes */
#define ADC_0V_VALUE 0
#define ADC_1V_VALUE 1241
#define ADC_2V_VALUE 2482
#define ADC_3V_VALUE 3723
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
uint8_t uart_tx[7]={0,0,0,0,0,0xD,0xA};
uint32_t adcResultall,adcResult = 0;
uint32_t iii,adc_samlpe=1000,autostart=0,adc_delay=1;
uint32_t i_0, i_1, i_2;
uint8_t TxData[6]={0};
uint8_t RxData[6]={0};
uint8_t aaa[5]={0};
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
void adc_string(uint32_t adc)
{
//uint8_t aaa[5]={0};
aaa[0]=adc/10000;
aaa[0]=aaa[0] &0xF;
uart_tx[0]=aaa[0]|0x30;
aaa[1]=(adc-aaa[0]*10000)/1000;
aaa[1]=aaa[1] &0xF;
uart_tx[1]=aaa[1]|0x30;
aaa[2]=(adc-aaa[0]*10000-aaa[1]*1000)/100;
aaa[2]=aaa[2] &0xF;
uart_tx[2]=aaa[2]|0x30;
aaa[3]=(adc-aaa[0]*10000-aaa[1]*1000-aaa[2]*100)/10;
aaa[3]=aaa[3] &0xF;
uart_tx[3]=aaa[3]|0x30;
aaa[4]=(adc-aaa[0]*10000-aaa[1]*1000-aaa[2]*100-aaa[3]*10);
aaa[4]=aaa[4] &0xF;
uart_tx[4]=aaa[4]|0x30;
uart_tx[5]=0x0D;
uart_tx[6]=0x0A;
}
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_UART_Receive_DMA(&huart1, RxData, 6);
if(RxData[5]==0xFF) //
{
RxData[5]=0x00;
adc_samlpe = 256*RxData[3] + RxData[4];
adc_delay = 256*RxData[1] + RxData[2];
adcResult = 0;
adcResultall = 0;
HAL_Delay(1+adc_delay);
for(i_1=0;i_1 < adc_samlpe+1; i_1++)
{
HAL_ADC_Start(&hadc);
HAL_ADC_PollForConversion(&hadc, 1000);
adcResult = adcResult + HAL_ADC_GetValue(&hadc);
HAL_ADC_Stop(&hadc);
}
adcResultall = adcResult/(adc_samlpe+1);
adc_string(adcResultall);
TxData[0] = uart_tx[1];
TxData[1] = uart_tx[2];
TxData[2] = uart_tx[3];
TxData[3] = uart_tx[4];
TxData[4] = 0x0D;
TxData[5] = 0x0A;
HAL_UART_Transmit_DMA(&huart1, TxData, 6);
for(i_2=0; i_2 <= 5; i_2++)
{
RxData[i_2] = 0;
}
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Комментариев нет:
Отправить комментарий
ваше мнение...