Jump to content
OMRON Forums

Change register in background C program


Recommended Posts

I'm running a toy model to try and switch Gate3[1].Chan[0].AtanEna in a background C program. (the AtanEna is bit 18 of inCtrl). I can read the bit without any problems and it recognizes when I flip the bit using the terminal, however, when I try and set the bit in a background C program it does not work. Am I able to do this? Code below


int main(void) 
volatile GateArray3 *tga;
int inCtrl;

tga = GetGate3MemPtr(1);

inCtrl = tga->Chan[0].InCtrl;
printf("before %i\n",inCtrl); //AtanEna = 1
tga->Chan[0].InCtrl &= ~(1<<18); //AtanEna should = 0
inCtrl = tga->Chan[0].InCtrl;
printf("flipped %i\n",inCtrl); //Still shows 1
return 0;


There is no change in the value of inCtrl upon setting the bit to zero.

Link to comment
Share on other sites

  • Replies 9
  • Created
  • Last Reply

Top Posters In This Topic

I could be wrong, but I think you may need to change the line where you are actually writing the bit low to the following:


tga->Chan[0].InCtrl &= (tga->Chan[0].InCtrl)^(1<<18); //AtanEna should = 0


Does that work?


Does Gate3.WpKey have to be $AAAAAAAA for this?

Link to comment
Share on other sites

I tested this and you're right, it doesn't set the 18th bit low. Also, setting Sys.WpKey=$AAAAAAAA has no effect.


My PPMAC is malfunctioning right now; otherwise, I would test further. For now, can you just use SetPmacVar()?


For example, SetPmacVar("Gate3[1].Chan[0].AtanEna",0.0)


This will set AtanEna to 0. Using SetPmacVar is slower than writing the bit directly but it will work in a pinch.

Link to comment
Share on other sites

Yes the SetPmacVar does work so long as you make sure that WpKey is set. It's strange though that I can't set it by simply addressing the pointer in C. It might be a write protect issue but I though that there was no write protect when accessing variables from C?
Link to comment
Share on other sites

Are you sure that the printf isn't misleading you? The reason I ask is that I ran into a similar problem where I would set a register, and then immediately printf the register to read its value.


The problem is that whenever you modify a register in C you are probably doing a read-modify-write operation. If your program hasn't completed the "write" operation before your printf, you'll get the wrong value printed to the terminal.


Try inserting a usleep between the write and the printf, see if that changes anything.


This would also explain why SetPmacVar works, but the direct write appears not to work.

Link to comment
Share on other sites

I put a sleep in (1 second) and I still see the same effect. I also check using the terminal to see if the bit has been set to zero.


Another think I tried was just writing the value to the register i.e.


tga->Chan[0].InCtrl = 4194375;


And that did not work either.

Link to comment
Share on other sites

tga->WpKey=0xAAAAAAAA Did the trick! Thanks everyone and as always it was in the software reference manual although I was being distracted by:


"Furthermore, in the Script environment, many elements are write-protected so user code cannot change their values, but these elements can be read at any time. In the C environment, any element for which you are provided access (that is, whose name is provided in the RtGpShm.h header file) can be written to. "


Which I wrongly assumed meant that the registers were not write-protected.

Link to comment
Share on other sites

This topic is now closed to further replies.

  • Create New...