cancel
Showing results for 
Search instead for 
Did you mean: 

How to covert the code according to STM32CUBEIDE

i_am_sandy
Associate II

I have Arduino program how covert it to STM32CUBEIDE suggest me any websites or channel for applying logics and change the program to it tell me any forum where i could learn the logic and apply it to real world applications

Here i have given the code below

#define HIGH_SIDE_A 9  // High-side MOSFET on side A
#define HIGH_SIDE_B 11  // High-side MOSFET on side B


#define FREQUENCY 60// 60 Hz
#define DUTY_CYCLE 100 // 100% duty cycle


int dt=8333.33; //dead-time
unsigned long period;
unsigned long highStateDuration;
unsigned long lowStateDuration;
unsigned long lastChangeTime;
bool isHighState = true;


void setup() {
  pinMode(HIGH_SIDE_A, OUTPUT);
  pinMode(HIGH_SIDE_B, OUTPUT);
 
 
  period = 1000000 / FREQUENCY; // Period in microseconds
  highStateDuration = (period * DUTY_CYCLE) / 100; // High state duration
  lowStateDuration = highStateDuration; // Low state duration is equal to high state duration
}


void loop() {
  unsigned long currentTime = micros();
 
  if (isHighState && currentTime - lastChangeTime >= highStateDuration) {
    // Switch to Low State
    digitalWrite(HIGH_SIDE_A, LOW);
    delayMicroseconds((period * dt) / 100); // 10% of period as dead time
    digitalWrite(HIGH_SIDE_B, HIGH);
    lastChangeTime = currentTime;
    isHighState = false;
  } else if (!isHighState && currentTime - lastChangeTime >= lowStateDuration) {
    // Switch to High State
    digitalWrite(HIGH_SIDE_B, LOW);
    delayMicroseconds((period * dt) / 100); // 10% of period as dead time
    digitalWrite(HIGH_SIDE_A, HIGH);
    lastChangeTime = currentTime;
    isHighState=true;
}
}
5 REPLIES 5
STTwo-32
ST Employee

Hello @i_am_sandy and welcome to the ST Community.

In fact, there is not any app that can do this. You have to do it manually if you are familiar with the Cube. We have a lot of tutorials that can help you to learn programming throw Cube (on YouTube, Wiki, ...). Also, you can keep using CubeIDE with the extension STM32DUINO that will help you programming STM32 MCUs throw Arduino.

Best Regards.

STTwo-32 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

SofLit
ST Employee

Hello @i_am_sandy  and welcome to the community,

Not sure if there is a simple solution for that. I think you need to understand what the code is doing and port it yourself.

In any case, see these threads: 

https://community.st.com/t5/stm32cubeide-mcus/convert-arduino-code-to-stm32-ide-code/td-p/382900

https://community.st.com/t5/stm32-mcus-motor-control/hi-i-would-like-to-convert-arduino-code-to-stm32-cube-ide-code/td-p/176585

https://forum.arduino.cc/t/how-to-convert-arduino-code-into-stm32-code/1238178/6

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

It's fine but I want to work in STM32CUBEIDE

 

So, the only way is to do that manually, for that you need to a be able to use the CubeIDE for programming. The YouTube Tutorials may be a good Start.  Also, Wikis can do the job.

Best Regards.

STTwo-32

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.


@i_am_sandy wrote:

 I want to work in STM32CUBEIDE


Why?

If it's working in the Arduino IDE, why not just keep it in the Arduino IDE?

If you want to start new projects in CubeIDE, then do that - but is there really any point in messing about with converting old projects?


Anyhow, the code itself is pretty simple and straightforward - it just uses these four Arduino functions:

  • pinMode
  • micros
  • digitalWrite
  • delayMicroseconds

So either create your own implementations of those, or just re-write the code to use the equivalent HAL functions.

(If you use CubeMX, you don't even need pinMode - the auto-generated code does that for you)

 

The main() function auto-generated by CubeMX looks like this:

 

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();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  // etc, with any other MX_..._Init() functions

  /* USER CODE BEGIN 2 */

  
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

 

You just need to call your setup() function in the USER CODE 2 section, and your loop() function in either the USER CODE WHILE or the USER CODE 3 section; eg,

 

  :
  :
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  // etc, with any other MX_..._Init() functions

  /* USER CODE BEGIN 2 */

    setup();                                     // <<< call your setup() here

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

    loop();                                      // <<< call your loop() here ...

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

    loop();                                      // <<< ... OR here

  }
  /* USER CODE END 3 */
}