sexta-feira, 24 de março de 2023

LOM204A - LENDO ADC1 COM KEIL C - STM32 CUBE


O objetivo deste BLOG é demonstrar como é possível ler o ADC1 do WISOL LOM204A (PA1)

Foi utilizado o STM32 CUBE para gerar o código para o KEIL C e então incluído no Projeto KEIL  C padrão  da WISOL.

LOM204A (MESH) e u-BLOX NINA W106 (Gateway)


STM32 Cube (selecione a opção abaixo para o processador do LOM204A)



Segue código gerado (adicionado ao projeto KEIL da WISOL, em negrito)

//---------------------------------------------------------------------------- // Project Name : LoRa WAN //---------------------------------------------------------------------------- // File Name : Main.c //---------------------------------------------------------------------------- // Processor : STM32L071CZY WLCSP // Compiler : uVision V5.20.0.0 // RF Transceiver : SX1276 // Frequency : 917 ~ 928Mhz // Modulation : LoRa // Version : H/W(1.3), LIB(2.02) #define MAIN_C_ #include <stddef.h> #include <string.h> #include "stm32l0xx_hal.h" #include <math.h> #include "board.h" #include "Utilities.h" #include "misc.h" #include "process.h" #include "timer.h" #include "init.h" #include "timer.h" #include "main.h" #include "WISOL_API.h" //adc static void MX_ADC_Init(void); ADC_HandleTypeDef hadc; //adc /** * @brief ADC Initialization Function * @param None * @retval None */ static void MX_ADC_Init(void) { /* USER CODE BEGIN ADC_Init 0 */ /* USER CODE END ADC_Init 0 */ ADC_ChannelConfTypeDef sConfig = {0}; /* USER CODE BEGIN ADC_Init 1 */ /* USER CODE END ADC_Init 1 */ /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ hadc.Instance = ADC1; hadc.Init.OversamplingMode = DISABLE; hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1; hadc.Init.Resolution = ADC_RESOLUTION_12B; hadc.Init.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc.Init.ContinuousConvMode = DISABLE; hadc.Init.DiscontinuousConvMode = DISABLE; hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc.Init.DMAContinuousRequests = DISABLE; hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV; hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED; hadc.Init.LowPowerAutoWait = DISABLE; hadc.Init.LowPowerFrequencyMode = ENABLE; hadc.Init.LowPowerAutoPowerOff = DISABLE; if (HAL_ADC_Init(&hadc) != HAL_OK) { //Error_Handler(); } /** Configure for the selected ADC regular channel to be converted. */ sConfig.Channel = ADC_CHANNEL_1; sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) { //Error_Handler(); } /* USER CODE BEGIN ADC_Init 2 */ /* USER CODE END ADC_Init 2 */ } //Uart Para receber LTS UART_HandleTypeDef huart1; static void MX_USART1_UART_Init(void) { /* USER CODE BEGIN USART1_Init 0 */ /* USER CODE END USART1_Init 0 */ /* USER CODE BEGIN USART1_Init 1 */ /* USER CODE END USART1_Init 1 */ huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_UART_Init(&huart1) != HAL_OK) { __disable_irq(); } /* USER CODE BEGIN USART1_Init 2 */ /* USER CODE END USART1_Init 2 */ } //Para leitura de dados via UART char buff[ 132 ]; //mais null //Programa Principal int main(void) { //Init Start_Init(); //Abre UART1 para recebimento LTS MX_USART1_UART_Init(); //adc MX_ADC_Init(); while(1) { HAL_ADC_Start(&hadc); if(HAL_ADC_PollForConversion(&hadc,10)==HAL_OK) { sprintf(buff, "%d\r\n ",HAL_ADC_GetValue(&hadc)); HAL_UART_Transmit(&huart1,buff, sizeof(buff),10); } HAL_Delay(1000); HAL_ADC_Stop(&hadc); } } //Não mexer void Start_Init(void) { HW_SystemClock_Config(); StartNVInit(); BoardInitPeriph( ); BoardInitMcu( ); Var_Init(); sub_process_init(); // set receive mode for uart TimerInit( &TxNext, Txnext_fn ); // Do not modify nvm_init(); // To read the configuration saved in EEPROM. Clr_BIT2(); LDO_on(); } void Restart_Init(void) { BoardInitPeriph( ); BoardInitMcu( ); Var_Init(); sub_process_init(); // set receive mode for uart TimerInit( &TxNext, Txnext_fn ); // Do not modify nvm_init(); // To read the configuration saved in EEPROM. Clr_BIT2(); LDO_on(); }

Execução (conectando o ADC1 em 3.3V)

Sobre a SMARTCORE

A SmartCore fornece módulos para comunicação wireless, biometria, conectividade, rastreamento e automação.
Nosso portfólio inclui modem 2G/3G/4G/NB-IoT/Cat.M, satelital, módulos WiFi, Bluetooth, GNSS / GPS, Sigfox, LoRa, leitor de cartão, leitor QR code, mecanismo de impressão, mini-board PC, antena, pigtail, LCD, bateria, repetidor GPS e sensores.

Mais detalhes em www.smartcore.com.br 

Nenhum comentário:

Postar um comentário