🎉Community Raffle - Win $25

An exclusive raffle opportunity for active members like you! Complete your profile, answer questions and get your first accepted badge to enter the raffle.
Join and Win

Using while loop in c block

User: "reza hakimi_20486"
Altair Community Member
Updated by reza hakimi_20486

Hello
In a program, I want the C block to continuously read the input and change the output according to the input
Now I have a few questions
1. Should I put the whole program in a while loop?
2. If I put it in the while loop, the speed of the simulation will decrease drastically, what is the solution to this problem?
3. Is there any other tool in psim other than Block C that has the ability to generate code and do this for me
I will put the image of the program in the continuation of the question

Find more posts tagged with

Sort by:
1 - 1 of 11
    User: "Albert_Dunford"
    Altair Employee
    Accepted Answer
    Updated by Albert_Dunford

    there is no break to your c code. That while loop will keep on running, and never stop. the while loop is not necessary keep the if else statements the c block executes at each time step, you are defeating this action by putting in the while loop. Please consider how does the code ever exit that while loop you have created? it is infinite.

    We need to consider how c code works on an MCU. Typically it is interrupt driven, at some rate the inputs will be sampled, then you will do calculations on these inputs and make decisions, then do it all again.

    Your code does not follow this structure you are trying to enforce the polling from within the code when really you should be letting PSIM do this for you. This is the point of the code generation and you are trying to work around this.  Please review this tutorial video ,

    I answered a support question on the other day. Trying to do edge detection on the encoder outputs with GPIOs is only going to work with very slow motor speeds. The peripheral encoder inputs is what you need to be using, they are properly design for this.

    If you truly want to do this, you need to put a ZOH in front of the c block and remove the loop structure and just have the if else statements. please look in the examples at the BLDC setups, they do something similar to what you want. examples\Code Generation\F2806x Target\TI High-Voltage BLDC Drive (sensored)\level5

    to get any decent performance you are likely going to overload the CPU.