ceventscodewarriorgetvalue

CodeWarrior Get variable value from a event.c


to enter context I'm doing this: Drive a stepper motor through the variation of pulse frequency input to a Driver (A4988) (It is not necessary to know the functioning of this for this question). Now and got varying the frequency of the pulses (They change the engine speed). You need to know that for the motor shaft 1 full turn have to get 200 pulses (The engine is 1.8 ° degrees per step).

I got the engine and make him a full turn in 1 second. Period = 0.005s

To program this I am using the component: TimerUnit_LDD. With a frequency of 163840 Hz count

In the case of the whole turn 1 to get that frequently use this function.

---- main.c

TU1_Enable (TU1_DeviceData);  TU1_SetPeriodTicks (TU1_DeviceData, 410);   

The parameter 410 is compared to the period I want, as is sending pulses programmed by changing the value of a pin taken into account both the high and the low pulse, like this:

----- Events.c

TU1_OnCounterRestart void (* UserDataPtr LDD_TUserData) {  Step1_NegVal ();  }

The period for serious formulates 819.2, having in mind the above serious approximates 409.6 and 410 (seen in a oscilloscope frequency is 200 Hz (ok).

Already entered in context the problem is this:

---- main.c

TU1_Enable (TU1_DeviceData); // Enable the counter TU1_SetPeriodTicks (TU1_DeviceData, 410); // Setting the desired period

for (;;) {            TU1_Enable (TU1_DeviceData);            WAIT1_Waitms (1000); // Rotation time  TU1_Enable (TU1_DeviceData); // Disable the counter }

With this code is what I try to check that the frequency calculation was correct and that in one second would 1 turn. But what happens is that it gives the rotation but is offset a little more. I guess this goes through the runtime required for each line of code.

What I want to know is, how could obtain the numerical value of a variable in an event? how could I do something like this.

---- main.c

TU1_Enable (TU1_DeviceData); // Initialize the counter TU1_SetPeriodTicks (TU1_DeviceData, 410); // Setting the desired period

for (;;) { for (;;) {       if (GetValue (x) == 200) break; // GetValue (x) This function is what I want to achieve } WAIT1_Waitms (1000); }

----- Events.c

TU1_OnCounterRestart void (* UserDataPtr LDD_TUserData) {  Step1_NegVal (); x = x + 1;  }

GetValue (x) this function would obtain the value of x which is in Events.c and define a number of pulses to control espefico.

Take a variable and is affected by the counter, and that this gets to 200 (for 1 turn in 1 second).

This would have the certainty that menera be sent alone and lonely, neither more nor less, only 200 pulses.

I require this as specific as I am desarrolando the program for a CNC machine and is too importanto precision is the highest.

I hope you understand and I speak Spanish and this was translated by Chrome

Programmed in C language, Freescale KL25Z, CodeWarrior, OPEN_SDA,


Solution

  • I managed to implement something but I think it may be easier to get
    
    
    -----(main.c)
    
    extern int count;//called external variable
    
    int main(void){
    PE_low_level_init();
    
    TU1_Enable(TU1_DeviceData);                         
    TU1_SetPeriodTicks(TU1_DeviceData,410);//T=0.005 sec              
    
    for(;;){
        Term1_Cls();// Clear Console  
        WAIT1_Waitms(1000);
        Term1_MoveTo(0,0);// Set 0,0 in Console 
    
    
    
    for(;;){
      TU1_Enable(TU1_DeviceData);
      Term1_SendNum(count);
      Term1_CRLF();
          if (count>400){//amount of high and low pulse counting
              count=0;
              TU1_Disable(TU1_DeviceData);
              break;
          }
     }
     WAIT1_Waitms(1000);
     Dir1_NegVal();
     }
    
    
    ----(Events.c)
    
    
    int count;
    
    void TU1_OnCounterRestart(LDD_TUserData *UserDataPtr)
    {
         Step1_NegVal();
         count=count+1; //counter
     }