fast access to control_reg

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
GuNo_288966
Level 4
Level 4
25 replies posted 10 replies posted 10 questions asked

Hi,

   

I am using a control_reg to control two stepper motors. The motors are controlled by two different functions.

   

Currently I do

   

Control_Reg_Motors_Write(Control_Reg_Motors_Read() | step_bit);

   

or

   

Control_Reg_Motors_Write(Control_Reg_Motors_Read() & ~step_bit);

   

to set and clear bits.

   

Isn´t this slow? Would it be better to use a copy of the control register content that is updated in both functions so that it is not needed to read the control_reg?

   

control_reg_copy |= step_bit;

   

Control_Reg_Motors_Write(control_reg_copy);

0 Likes
5 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Your line

   

control_reg_copy |= step_bit;

   

is a read-modify-write access as well. The difference in the timing is not much and lies in the range of µs. In the generated controlregister.h file you will see a declaration of the register itself, so you may use your above scheme directly on the control register and not on a shadow copy.

   

 

   

Bob

0 Likes

Thanks, Bob!

   

One more question. If I need to update the control register both in an ISR and in the normal code, I think I must

   

ISR_Disable();

   

ManipulateControlReg();

   

ISR_Enable();

   

to be safe. Is that ok? Is that fast? Is there a better solution?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

" Is there a better solution?"  Yes, there is. When not disturbing your concept a CyEnterCriticalSection() and CyExitCriticalSection() would be the most common solution. See "System Reference Guide" from Creator help menu.

   

 

   

Bob

0 Likes

Again Thanks, Bob, this is exactly what I was looking for.

   

Turtle

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You are always welcome, Turtle.

   

 

   

Bob

0 Likes