cancel
Showing results for 
Search instead for 
Did you mean: 

Strange code behavior

megahercas6
Senior
Posted on November 07, 2014 at 23:09

I am working on project where i need to drive pjezo motors ( special triangle wave )

for generating forward or backward step i use timer controlled DMA , that copies triangle wave data to DAC output. This part is working, but i can't found any glitch. glitch is, code only going to step, if difference is larger than 1, that means, if i try to step, it will not work, if i say, step 10 times, it will step 9 times.

int main(void)
{
Delay(0xFFFFFF);
ADC_Configuration();
fill_array();
GPIO_Int();
Enable_Power();
TIM_Config();
DMA_Trigger_Config();
DAC1_Config();
DAC2_Config();
EXTI_Int();
while(1)
{ 
update_variables();
if(pico_x<
pico_x_current
)
{
step1
=
0
; // Go backwards
pico_x_current--;
}
if(pico_x>pico_x_current)
{
step1=1; // Go_forward
pico_x_current++;
}
if(pico_x==pico_x_current)
{
step1=3; // go nowhere, but use DAC time
}
if(pico_y<
pico_y_current
)
{
step2
=
0
;// Go backwards
pico_y_current--;
}
if(pico_y>pico_y_current)
{
step2=1;// Go_forward
pico_y_current++;
}
if(pico_y==pico_y_current)
{
step2=3;// go nowhere, but use DAC time
}
DAC_CH1_Step(step1);
DAC_CH2_Step(step2);
DMA_Cmd(DMA1_Stream5, ENABLE);
DMA_Cmd(DMA1_Stream6, ENABLE);
NTC_Temp(); // monitor Flyback switch temperature
DAC_Voltage(); // monitor voltage levels on HV bus
while((DMA_GetITStatus(DMA1_Stream5, DMA_IT_TCIF5))==RESET);
while((DMA_GetITStatus(DMA1_Stream6, DMA_IT_TCIF6))==RESET);
DMA_ClearITPendingBit(DMA1_Stream5,DMA_IT_TCIF5);
DMA_ClearITPendingBit(DMA1_Stream6,DMA_IT_TCIF6);
}
}
void update_variables(void)
{
pico_x = SPI_DATA_Rx[0]; // get data from computer
pico_y = SPI_DATA_Rx[1]; // get data from computer
SPI_DATA_Tx[0] = pico_x_current;//send data to computer
SPI_DATA_Tx[1] = pico_y_current;//send data to computer
SPI_DATA_Tx[2] = pico_voltage;/send data to computer
SPI_DATA_Tx[3] = pico_temp;//send data to computer
SPI_DATA_Tx[4] = pico_error;//send data to computer
}
void DAC_CH1_Step(int dirrection)
{
if(dirrection==0)
{
DMA_InitStructure1.DMA_Memory0BaseAddr = (uint32_t)&backward_1;
}
else
{
DMA_InitStructure1.DMA_Memory0BaseAddr = (uint32_t)&forward_1;
}
if(dirrection==3)
{
DMA_InitStructure1.DMA_Memory0BaseAddr = (uint32_t)&none;
}
DMA_Init(DMA1_Stream5, &DMA_InitStructure1);
}
void DAC_CH2_Step(int dirrection)
{
if(dirrection==0)
{
DMA_InitStructure2.DMA_Memory0BaseAddr = (uint32_t)&backward_2;
}
else
{
DMA_InitStructure2.DMA_Memory0BaseAddr = (uint32_t)&forward_2;
}
if(dirrection==3)
{
DMA_InitStructure2.DMA_Memory0BaseAddr = (uint32_t)&none;
}
DMA_Init(DMA1_Stream6, &DMA_InitStructure2);
}

Somehow i don't understand why i can step with size smaller than 2. if i make single step, current position tracker will change as it should I am using spi to copy data from computer over DMA, so no additional code is needed, this part works fine ( Circular buffer)
3 REPLIES 3
Posted on November 08, 2014 at 02:11

Sorry, I'm not seeing how the step size is expressed in this code.

How does the DMA and TIM get setup, and triggered. Do you expand the DMA pattern buffer to have multiple waves? Do you use the TIM in single-shot with a repetition count?
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
megahercas6
Senior
Posted on November 08, 2014 at 09:02

Hi, thanks for taking time to answer. This was very simple coding problem, that why i should not code 8h straight. I needed just swap if condition,

if(pico_y==pico_y_current)
{
step2=3;// go nowhere, but use DAC time
} 
if(pico_y<
pico_y_current
)
{
step2
=
0
;// Go backwards
pico_y_current--;
}
if(pico_y>pico_y_current)
{
step2=1;// Go_forward
pico_y_current++;
}

now it's working as it should, problem solved
Posted on November 08, 2014 at 13:42

I see, changing one of the variables that's part of the comparison.

You could do it a bit more efficiently by not having fall thru behaviour, ie use ''else if''

if (pico_y<
pico_y_current
)
{
step2
=
0
;// Go backwards
pico_y_current--;
}
else if (pico_y>pico_y_current)
{
step2=1;// Go_forward
pico_y_current++;
}
else // (pico_y==pico_y_current) implied as only other choice
{
step2=3;// go nowhere, but use DAC time
}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..