int main(void) { char msg[20]; uint16_t rawValues[3]; float temp; HAL_Init(); Nucleo_BSP_Init(); /* Initialize all configured peripherals */ MX_TIM1_Init(); MX_ADC1_Init(); HAL_ADCEx_Calibration_Start(&hadc1); HAL_TIM_Base_Start(&htim1); HAL_ADC_Start_DMA(&hadc1, (uint32_t*)rawValues, 1); while(1) { while(!convCompleted); for(uint8_t i = 0; i < 1; i++) { temp = ((float)rawValues[i]) / 4095 * 3300; temp = ((temp - 1430.0) / 4.3) + 25; sprintf(msg, "rawValue %d: %hu\r\n", i, rawValues[i]); HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY); sprintf(msg, "Temperature %d: %f\r\n",i, temp); HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY); } convCompleted = 0; } }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); hdma_usart2_tx.Instance = DMA1_Channel7; hdma_usart2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_usart2_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_usart2_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_usart2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_usart2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_usart2_tx.Init.Mode = DMA_NORMAL; hdma_usart2_tx.Init.Priority = DMA_PRIORITY_LOW; HAL_DMA_Init(&hdma_usart2_tx); HAL_DMA_Start(&hdma_usart2_tx, (uint32_t)msg, (uint32_t)&huart2.Instance->DR, strlen(msg)); //Enable UART in DMA mode huart2.Instance->CR3 |= USART_CR3_DMAT; //Wait for transfer complete HAL_DMA_PollForTransfer(&hdma_usart2_tx, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY); //Disable UART DMA mode huart2.Instance->CR3 &= ~USART_CR3_DMAT; //Turn LD2 ON HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET); /* Infinite loop */ while (1); }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); hdma_usart2_tx.Instance = DMA1_Channel7; hdma_usart2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_usart2_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_usart2_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_usart2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_usart2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_usart2_tx.Init.Mode = DMA_NORMAL; hdma_usart2_tx.Init.Priority = DMA_PRIORITY_LOW; hdma_usart2_tx.XferCpltCallback = &DMATransferComplete; HAL_DMA_Init(&hdma_usart2_tx); /* DMA interrupt init */ HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn); HAL_DMA_Start_IT(&hdma_usart2_tx, (uint32_t)msg, (uint32_t)&huart2.Instance->DR, strlen(msg)); //Enable UART in DMA mode huart2.Instance->CR3 |= USART_CR3_DMAT; /* Infinite loop */ while (1); }
int main(void) { char msg[20]; HAL_Init(); Nucleo_BSP_Init(); /* Before we can access to every register of the PWR peripheral we must enable it */ __HAL_RCC_PWR_CLK_ENABLE(); while (1) { if(__HAL_PWR_GET_FLAG(PWR_FLAG_SB)) { /* If standby flag set in PWR->CSR, then the reset is generated from * the exit of the standby mode */ sprintf(msg, "RESET after STANDBY mode\r\n"); HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY); /* We have to explicitly clear the flag */ __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU|PWR_FLAG_SB); } sprintf(msg, "MCU in run mode\r\n"); HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY); while(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_SET) { HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin); HAL_Delay(100); } HAL_Delay(200); sprintf(msg, "Entering in SLEEP mode\r\n"); HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY); SleepMode(); sprintf(msg, "Exiting from SLEEP mode\r\n"); HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY); while(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_SET); HAL_Delay(200); sprintf(msg, "Entering in STOP mode\r\n"); HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY); StopMode(); sprintf(msg, "Exiting from STOP mode\r\n"); HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY); while(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_SET); HAL_Delay(200); sprintf(msg, "Entering in STANDBY mode\r\n"); HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY); StandbyMode(); while(1); //Never arrives here, since MCU is reset when exiting from STANDBY } }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); MX_TIM3_Init(); HAL_TIM_OnePulse_Start(&htim3, TIM_CHANNEL_1); while (1); }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); MX_TIM3_Init(); HAL_TIM_Base_Start_IT(&htim3); while (1); }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); hdma_memtomem_dma1_channel5.Instance = DMA1_Channel5; hdma_memtomem_dma1_channel5.Init.Direction = DMA_MEMORY_TO_MEMORY; hdma_memtomem_dma1_channel5.Init.PeriphInc = DMA_PINC_ENABLE; hdma_memtomem_dma1_channel5.Init.MemInc = DMA_MINC_ENABLE; hdma_memtomem_dma1_channel5.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_memtomem_dma1_channel5.Init.MemDataAlignment = DMA_PDATAALIGN_BYTE; hdma_memtomem_dma1_channel5.Init.Mode = DMA_NORMAL; hdma_memtomem_dma1_channel5.Init.Priority = DMA_PRIORITY_VERY_HIGH; HAL_DMA_Init(&hdma_memtomem_dma1_channel5); GPIOC->ODR = 0x100; HAL_DMA_Start(&hdma_memtomem_dma1_channel5, (uint32_t)&flashData, (uint32_t)&sramData, 1000); HAL_DMA_PollForTransfer(&hdma_memtomem_dma1_channel5, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY); GPIOC->ODR = 0x0; while(HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin)); hdma_memtomem_dma1_channel5.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; hdma_memtomem_dma1_channel5.Init.MemDataAlignment = DMA_PDATAALIGN_WORD; HAL_DMA_Init(&hdma_memtomem_dma1_channel5); GPIOC->ODR = 0x100; HAL_DMA_Start(&hdma_memtomem_dma1_channel5, (uint32_t)&flashData, (uint32_t)&sramData, 250); HAL_DMA_PollForTransfer(&hdma_memtomem_dma1_channel5, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY); GPIOC->ODR = 0x0; HAL_Delay(1000); while(HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin)); GPIOC->ODR = 0x100; memcpy(sramData, flashData, 1000); GPIOC->ODR = 0x0; HAL_Delay(1000); while(HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin)); GPIOC->ODR = 0x100; for(int i = 0; i < 1000; i++) sramData[i] = flashData[i]; GPIOC->ODR = 0x0; /* Infinite loop */ while (1); }
int main(void) { uint16_t IV[200]; float angle; HAL_Init(); Nucleo_BSP_Init(); MX_TIM3_Init(); for (uint8_t i = 0; i < 200; i++) { angle = ASR*(float)i; IV[i] = (uint16_t) rint(100 + 99*sinf(angle*(PI/180))); } HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_1, (uint32_t *)IV, 200); while (1); }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); htim6.Instance = TIM6; htim6.Init.Prescaler = 31999; //32MHz/32000 = 1000Hz htim6.Init.Period = 499; //1000HZ / 500 = 2Hz = 0.5s __TIM6_CLK_ENABLE(); HAL_NVIC_SetPriority(TIM6_IRQn, 0, 0); HAL_NVIC_EnableIRQ(TIM6_IRQn); HAL_TIM_Base_Init(&htim6); HAL_TIM_Base_Start_IT(&htim6); while (1); }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); RetargetInit(&huart2); osThreadDef(blink, blinkThread, osPriorityNormal, 0, 100); osThreadCreate(osThread(blink), NULL); osThreadDef(uart, UARTThread, osPriorityNormal, 0, 300); osThreadCreate(osThread(uart), NULL); MsgBox = osMessageCreate(osMessageQ(MsgBox), NULL); osKernelStart(); /* Infinite loop */ while (1); }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); RetargetInit(&huart2); osThreadDef(blink, blinkThread, osPriorityNormal, 0, 100); osThreadCreate(osThread(blink), NULL); osThreadDef(delay, delayThread, osPriorityNormal, 0, 100); osThreadCreate(osThread(delay), NULL); osSemaphoreDef(sem); semid = osSemaphoreCreate(osSemaphore(sem), 1); osSemaphoreWait(semid, osWaitForever); osKernelStart(); /* Infinite loop */ while (1); }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); htim1.Instance = TIM1; htim1.Init.Prescaler = 47999; //84MHz/48000 = 1750Hz htim1.Init.Period = 874; //1750HZ / 875 = 2Hz = 0.5s htim1.Init.CounterMode = TIM_COUNTERMODE_UP; htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; __TIM1_CLK_ENABLE(); HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 0, 0); HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); HAL_TIM_Base_Init(&htim1); HAL_TIM_Base_Start_IT(&htim1); while (1) { } }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); MX_TIM1_Init(); HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); /* Connect a LED to PA8 pin to see the fading effect */ uint16_t dutyCycle = HAL_TIM_ReadCapturedValue(&htim1, TIM_CHANNEL_1); while(1) { while(dutyCycle < __HAL_TIM_GET_AUTORELOAD(&htim1)) { __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, ++dutyCycle); HAL_Delay(1); } while(dutyCycle > 0) { __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, --dutyCycle); HAL_Delay(1); } } }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); MX_TIM1_Init(); MX_TIM3_Init(); HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL); HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_1); HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_2); cnt1 = __HAL_TIM_GET_COUNTER(&htim3); tick = HAL_GetTick(); while (1) { if (HAL_GetTick() - tick > 1000L) { cnt2 = __HAL_TIM_GET_COUNTER(&htim3); if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&htim3)) { if (cnt2 < cnt1) /* Check for counter underflow */ diff = cnt1 - cnt2; else diff = (65535 - cnt2) + cnt1; } else { if (cnt2 > cnt1) /* Check for counter overflow */ diff = cnt2 - cnt1; else diff = (65535 - cnt1) + cnt2; } sprintf(msg, "Difference: %d\r\n", diff); HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY); speed = ((diff / PULSES_PER_REVOLUTION) / 60); /* If the first three bits of SMCR register are set to 0x3 * then the timer is set in X4 mode (TIM_ENCODERMODE_TI12) * and we need to divide the pulses counter by two, because * they include the pulses for both the channels */ if ((TIM3->SMCR & 0x3) == 0x3) speed /= 2; sprintf(msg, "Speed: %d RPM\r\n", speed); HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY); dir = __HAL_TIM_IS_TIM_COUNTING_DOWN(&htim3); sprintf(msg, "Direction: %d\r\n", dir); HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY); tick = HAL_GetTick(); cnt1 = __HAL_TIM_GET_COUNTER(&htim3); } if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_RESET) { /* Invert rotation by swapping CH1 and CH2 CCR value */ tim1_ch1_pulse = __HAL_TIM_GET_COMPARE(&htim1, TIM_CHANNEL_1); tim1_ch2_pulse = __HAL_TIM_GET_COMPARE(&htim1, TIM_CHANNEL_2); __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, tim1_ch2_pulse); __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, tim1_ch1_pulse); } } }