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.
Hi, I have a problem with the way your preprocessor works. This problem occurs in both assembly and in C. The reason we started trying to do this in C was that the assembler had a problem and while it accepted the code, did not produce the correct constant. It truncated the long constant to a 16 bit value and then divided. This produced a zero and messed up the program. When we originally wrote CLOCK_PERIOD_32K_100 equ (3052) DELAY_20_MS equ 2000000 / CLOCK_PERIOD_32K_100 We expected a value of about 655 from the preprocessor. This would fit into a 16 bit value and work well. We got zero or some other unexpected value. So we moved it to C. #define CLOCK_PERIOD_32K_100 ((long)(3052L)) #define DELAY_1_MS ((USHORT)(100000L / CLOCK_PERIOD_32K_100)) #define DELAY_2_MS 200000 / CLOCK_PERIOD_32K_100 #define DELAY_20_MS ((USHORT)((long)(2000000L) / CLOCK_PERIOD_32K_100)) ;this is about 655 counts When I used the above in an asm file, the assembler did not like constants with L on the end, and complained that a paren was missing. Not really up to the usual C standard for constants. It did not divide, just complained. And Casts will not work well. So what should we do? If nothing but go through the math my self and post that constant (say 655 for the above) to the #define, we will do what we must, and this post will be a warning for anyone trying to use the preprocessor in the same way. John "Sean" Cleary Sean
You may have run into the same problem we did. We found that you had to give both constants the proper cast when dividing. The original code: #define DELAY_20_MS ((USHORT)((long)(2000000L) / CLOCK_PERIOD_32K_100)) would need to be changed to: #define DELAY_20_MS ((USHORT)((long)(2000000L) / (long)CLOCK_PERIOD_32K_100)) I assume the USHORT is an unsigned int and not an unsigned char. Once we added the cast for the second parameter, the value was calculated correctly, otherwise it was set to 0.