We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I am pretty new to this stuff, but I am usually able to work out problems on my own. I am trying to interface to some humidity sensor that uses it's own proprietary 2-wire serial interface (clock line and a data line). To make things easier, I declared sbits for each pin (SCK and DATA). When I try to compile and execute code like this: . . DATA = 1; SCK = 1; error = DATA; SCK = 0; . . the compiler omits the line "SCK = 1". This is just an example, and it happens sporadically throughout the code. I read on some other posts that sbits are automatically volatile, and similar rumors, but the compiler is optimizing necessary lines out anyway. My compiler version is 6.20a. Thanks
I am sure the code was not executing because I was stepping through in debug mode, and no changes were occuring on those pins. Also, if I looked at the assembly code, there was no code present between successive pin changes. I tried writing a function to change the pin values, and calling it to fool the compiler, but that didn't work. I came up with the solution of putting those functions in a separate source file. void SetClock (char value); void SetData (char value); char GetData (); It works for now, and since this is an extremely short term project, I am not going to be able to explore the cause. By the way, the compiler was set to level 8 (default) optimization. I noticed that in the Keil environment, excluded code has a lighter gray bar to the left of the line. Surprisingly, the Hold routine does work, except for when it is called multiple times. Thanks to everyone who replied, and I'm sorry I can't dig deeper into this issue. Have a good day, Sean
When I compiled your example, I got the following:
ASSEMBLY LISTING OF GENERATED OBJECT CODE ; FUNCTION L?0006 (BEGIN) 0000 C2C8 CLR SCK 0002 120000 R LCALL Hold 0005 D2C8 SETB SCK ; The above executes ; SCK = 0; ; Hold (); ; SCK = 1; ; and then falls into the Hold routine ; FUNCTION Hold (BEGIN) ; SOURCE LINE # 9 ; SOURCE LINE # 10 ; SOURCE LINE # 12 ;---- Variable 'i' assigned to Register 'R7' ---- 0007 7FFF MOV R7,#0FFH 0009 ?C0001: 0009 DFFE DJNZ R7,?C0001 ; SOURCE LINE # 13 000B ?C0004: 000B 22 RET ; FUNCTION Hold (END) ; FUNCTION StartTX (BEGIN) ; SOURCE LINE # 15 ; SOURCE LINE # 16 ; SOURCE LINE # 17 ;---- Variable 'SFRPAGE_SAVE' assigned to Register 'R6' ---- char SFRPAGE_SAVE = SFRPAGE; 0000 AE84 MOV R6,SFRPAGE ; SOURCE LINE # 18 SFRPAGE = CONFIG_PAGE; 0002 75840F MOV SFRPAGE,#0FH ; SOURCE LINE # 19 DATA = 1; 0005 D2C9 SETB DATA ; SOURCE LINE # 20 ; SOURCE LINE # 21 ; SOURCE LINE # 22 ; SOURCE LINE # 23 SCK = 0; Hold (); SCK = 1; Hold (); 0007 120000 R LCALL L?0006 ; SOURCE LINE # 24 DATA = 0; 000A C2C9 CLR DATA ; SOURCE LINE # 25 Hold (); 000C 120000 R LCALL Hold ; SOURCE LINE # 26 ; SOURCE LINE # 27 ; SOURCE LINE # 28 ; SOURCE LINE # 29 SCK = 0; Hold (); SCK = 1; Hold (); 000F 120000 R LCALL L?0006 ; SOURCE LINE # 30 DATA = 1; 0012 D2C9 SETB DATA ; SOURCE LINE # 31 Hold (); 0014 120000 R LCALL Hold ; SOURCE LINE # 32 SCK = 0; 0017 C2C8 CLR SCK ; SOURCE LINE # 33 SFRPAGE = SFRPAGE_SAVE; 0019 8E84 MOV SFRPAGE,R6 ; SOURCE LINE # 34 001B 22 RET ; FUNCTION StartTX (END)