uC: dsPIC33FJ12GP202
IDE and Libraries: mikroC Pro for dsPIC
Simulator: Proteus ISIS
Compiler ver.: 6.2
So I have a question. I'm developing some code that communicates by UART (with library) with the computer in order to move a stepper
motor with precision, so by starting the program it asks for 1.-Direction (Left/Right). 2.-Degrees(0-360). 3.-Speed(0-120rpm max).
And by this point everything works, I managed to configure everything, dave the data and get the code structure in order, but
I have one issue..
When the UART finishes receiving the correct data, it calls a function in order to manipulate the data it got, and move the stepper motor the way
its desired. but the code stops running in the simulation just after the data is received, and the pins that correspond to the stepper motor never
change state, (said motor is conected throught a L298N H-Bridge controller).
Now, I have used that motor driver and said logic before, so first, as I didn't have experience with C fuctions, I made another
version of the program that gets rid of the function for troubleshooting and after doing that I reduced the problem to this secion of code:
//"Problematic section"------------------------------
count=(grad/degperstep); //Establecer grados->cuentas (cuantos pasos vamos a avanzar)
pos=0; //Conteo de excitación
vueltaspseg=veloc/60; //Establecer rpm->us
pasosporvuelta=stepperrev*vueltaspseg;
time=((1/pasosporvuelta)*100)+1;
//End of section---------------------------------------
Note: Certain operations have what I consider an implicit trunk, like ones "int=float/int" or such, this for
getting those variables to take part in the variable delays in the for loop and such.
Now it's not inside a function, so calling and receiving the correct data was not the problem, now, when I try to debug inside mikroC,
it stops right in the first instruction of that block of code and gets to a routine so called "long2float" that never gets out of.
So, after trying to eliminate the need for calculations, I set the variables "count", "time", and "pos" to see if I have and output
and voilá, I have an output to the motor, so, my question is:
->Are those calculations too complex to do for the microcontroller or am I running out of memory? I can't very much understand what could be
causing the issue since this microcontrollers are supossed to have pretty strong RAM and math power.
Now this are my variables and the type they are:
//Variables--------------------------------------------------
const char right[]={0b1100,0b1100,0b0100,0b0110,0b0010,0b0011,0b0001,0b1001}; //Excitacion derecha
const char left[]={0b1001,0b0001,0b0011,0b0010,0b0110,0b0100,0b1100,0b1100}; //Excitacion izquierda
int pos; //Para controlar la posición de la excitación (1~8)
char salidas; //Pines PORTB para motor
int aa; //Para recibir de la UART
int grad, sent, veloc; //Peticiones de datos en enteros
float degperstep; //->
int stepperrev, count; //Para control de ángulo
float vueltaspseg; //->
float pasosporvuelta; //->
int time; //Para control de la velocidad
int xx; //Control de flujo de cuestiones/acciones
int i,j; //For statements
//----------------------------------------------
I haven't tried changing them all to one type (all float/all int) but that would really mess the process and I have change one or two (the ones that it uses
on the first line of calculations, for example)
so I doubt that really solves anything.
The following code is the moving routine and I have succesfully implemented it in the past for moving said motor:
//Start of the routine---------------------------------
if(sent==1) //Derecha
{
for(i=0; i<count; i++)
{
if(pos==8) {pos=0;}
pos++;
salidas=right[pos]; //Establecemos la posición de la excitación
Hi(LATB)=salidas; //Movemos motor
for(j=0; j<time; j++) {delay_us(1);} //Velocidad requerida
}//Fin pasos
}//Fin Derecha
else //Izquierda
{
for(i=0; i<count; i++)
{
if(pos==8) {pos=0;}
pos++;
salidas=left[pos]; //Establecemos la posición de la excitación
Hi(LATB)=salidas; //Movemos motor
for(j=0; j<time; j++) {delay_us(1);} //Velocidad requerida
}//Fin pasos
}//Fin Izquierda
//End of move motor routine-----------------------------
I need the variable times and degrees for the cas when another motor with different specifications is used.
The compiler gives no errors and evreything works but the section I give.
How can I approach to solve this issue?, precision with variables is not a problem (I don't need 10 decimals in anything) but indeed
at least doing 45/1.8 and get a coherent result would be more than enough since it's the main goal of the program.