Loading...
 

Zephyr project on STM32

   Zephyr Workbench, a VSCode extension to manage Zephyr on STM32.
It enables users to easily create, develop, and debug Zephyr applications.
Main features:
  • Install host dependencies.
  • Import toolchain and SDK.
  • Create, configure, build and manage apps.
  • Debug STM32.
You can directly download it from the VSCode marketplace
For more details, visit the Zephyr Workbench

System Workbench for STM32


Analog servomotors with nucleo f334r8

Hi everybody!

I started programming ARM microcontrollers after watching Mr. Patrick Hood-Daniels’ tutorials on YouTube. I am using a Nucleo F334R8 development board to control a Puppet animatronic I am making for my niece.

You can see it here:

https://youtube.com/shorts/40tsBrCdnOg?si=NuuiD9RuRrL3VxnXQuestion

I want to share the code I used with everyone, hope you find It useful

// PUPPET head servos many positions

include “main.h”
void SystemClock_Config(void);

int main(void) {

int headServo; int neckServo; int handServo; int armServo;

int pulsecounter;

HAL_Init();

SystemClock_Config();

RCC->AHBENR |= RCC_AHBENR_GPIOCEN; RCC->AHBENR |= RCC_AHBENR_GPIODEN;

GPIOD->MODER |=GPIO_MODER_MODER2_0; GPIOD->MODER &= ~(GPIO_MODER_MODER2_1); GPIOD->OTYPER &= ~(GPIO_OTYPER_OT_2); GPIOD->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR2; GPIOD->PUPDR &= ~(GPIO_PUPDR_PUPDR2);

GPIOC->MODER |= GPIO_MODER_MODER4_0; GPIOC->MODER &= ~(GPIO_MODER_MODER4_1); GPIOC->OTYPER &= ~(GPIO_OTYPER_OT_4); GPIOC->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR4); GPIOC->PUPDR &= ~(GPIO_PUPDR_PUPDR4);

GPIOC->MODER |= GPIO_MODER_MODER1_0; GPIOC->MODER &= ~(GPIO_MODER_MODER1_1); GPIOC->OTYPER &= ~(GPIO_OTYPER_OT_1); GPIOC->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR1; GPIOC->PUPDR &= ~(GPIO_PUPDR_PUPDR1);

GPIOC->MODER |= GPIO_MODER_MODER2_0; GPIOC->MODER &= ~(GPIO_MODER_MODER2_1); GPIOC->OTYPER &= ~ (GPIO_OTYPER_OT_2); GPIOC->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR2; GPIOC->PUPDR &= ~(GPIO_PUPDR_PUPDR2);

GPIOC->MODER |= GPIO_MODER_MODER3_0; GPIOC->MODER &= ~(GPIO_MODER_MODER3_1); GPIOC->OTYPER &= ~(GPIO_OTYPER_OT_3); GPIOC->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR3; GPIOC->PUPDR &= ~(GPIO_PUPDR_PUPDR3);

RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;

TIM1->CR1 |= TIM_CR1_CEN;

TIM1->PSC= 48;

TIM1->ARR = 19999;

while (1) {

if (pulsecounter=200 && pulsecounter < 380) { headServo=1000; neckServo=2000; handServo=1500; armServo=1100; } if (pulsecounter>=380 && pulsecounter=560 && pulsecounter=740 && pulsecounterODR |= GPIO_ODR_4; } if (pulsecounter>=1100) { pulsecounter=0; GPIOC->ODR &= ~(GPIO_ODR_4); }

if ( TIM1->CNT < 800 || TIM1->CNT >2300 ) {

if (TIM1->CNT >19996) { if ((GPIOC->ODR & GPIO_ODR_1) && (GPIOC->ODR & GPIO_ODR_2) && (GPIOD->ODR & GPIO_ODR_2) && (GPIOC->ODR & GPIO_ODR_3)) {

}

else { GPIOC->ODR |= GPIO_ODR_1; GPIOC->ODR |= GPIO_ODR_2; GPIOD->ODR |= GPIO_ODR_2; GPIOC->ODR |= GPIO_ODR_3; pulsecounter++; }

} } if (TIM1->CNT >=800 && TIM1->CNT CNT >= headServo && (GPIOC->ODR & GPIO_ODR_1)) { GPIOC->ODR &= ~ (GPIO_ODR_1); }

if (TIM1->CNT >= neckServo && (GPIOC->ODR & GPIO_ODR_2)) { GPIOC->ODR &= ~ (GPIO_ODR_2); } if (TIM1->CNT >= handServo && (GPIOD->ODR & GPIO_ODR_2)) { GPIOD->ODR &= ~ (GPIO_ODR_2); } if (TIM1->CNT >=armServo && (GPIOC->ODR & GPIO_ODR_3)) { GPIOC->ODR &= ~ (GPIO_ODR_3); } }

}

}

void SystemClock_Config(void) {

RCC->CR |= RCC_CR_HSION;

while ((RCC->CR & RCC_CR_HSIRDY)==0) {}

RCC->CFGR &= ~(RCC_CFGR_SW);

while ((RCC->CFGR & RCC_CFGR_SWS) !=RCC_CFGR_SWS_HSI) {}

RCC->CR &= ~(RCC_CR_PLLON);

while ((RCC->CR & RCC_CR_PLLRDY)!=0) {}

RCC->CFGR &= ~(RCC_CFGR_PLLMUL);

RCC->CFGR |= RCC_CFGR_PLLMUL12;

RCC->CR |= RCC_CR_PLLON;

while ((RCC->CR & RCC_CR_PLLRDY)==0) {}

RCC->CFGR |= RCC_CFGR_SW_PLL;

while ((RCC->CFGR & RCC_CFGR_SWS)!= RCC_CFGR_SWS_PLL) {} }

 

Newest Forum Posts

  1. Analog servomotors with nucleo f334r8 by SkelePaw, 05:57
  2. So happy to finally meet you! by Danielrug, 2025-10-28 11:01
  3. STM32 MCU model shortlisting for Making RC remote by Palvish, 2025-07-07 15:05
  4. SPI on Nucleo_STMH533RE by royjamil, 2025-05-04 20:13
  5. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-25 07:37
  6. SPI on Nucleo_STMH533RE by royjamil, 2025-03-23 11:31
  7. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-23 09:33
  8. Configuring DMA for ADC in SW? by sam.hodgson, 2025-03-04 12:58
  9. Build a project in "release" mode by info@creosrl.it, 2025-02-20 18:12
  10. Build a project in "release" mode by info@creosrl.it, 2025-02-20 17:05

Last-Modified Blogs