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 all it may seem very easy but i can't initialize my code constants with binary value in c51 ver 3.20 franklin DOS version i want to: code char bin_code[3]={ 0b1100110011,0b1111110011,0b1100111011}; but compiler doasn't accept. any one can help me?
I'm afraid you just can't do this directly in 'C'. I think this has been discussed before - why don't you search the forum to see if someone came up with some sort of macro solution? Stefan
"but compiler doesn't accept." Does the Manual say that it should? Have you ever seen it mentioned in K&R, or any other 'C' text book? Of course not - it's just not part of the 'C' language. Sorry, but you'll just have to stick to Hex, Octal, or decimal! :-( It certainly has been discussed here before, and various macro solutions have been suggested - hit that 'Search' button!
hi all my first text has a typing fault! this is the correct text: it may seem very easy but i can't initialize my code constants with binary value in c51 ver 3.20 franklin DOS version i want to: code char bin_code[3]={ 0b11001100,0b11111100,0b11001110}; but compiler doasn't accept. any one can help me?
"my first text has a typing fault" Did you actually read the previous replies? This has nothing to do with the number of bits, or any other typos: it's a fundamental language issue: The 'C' programming language does NOT provide a notation for binary numbers!!!
"Did you actually read the previous replies?" If he did, they weren't to his liking. Clearly neither "you can't do this directly in C" and "search this forum for a macro solution" are good enough. Either someone had better search for the solution for him and post it in this thread or contact ANSI to have the 'C' standard revised by close of business today. Well, I'm not going to do it. Stefan
Looks like hitech c compiler accepts binary as well as decimal, octal, hex etc... I haven't tried it in Keil C but here is a code snippet from a working program using PIC microcontroller:
/****************************************************************************** * Name : initialize * * Description : This function initializes the PIC to the following : * * 1. PORTA as output * * 2. PORTB as Input * * 3. Clears interrupt flags and sets timer0 to int every 1msec * * 4. Stops the motor. * * 5. sets the RB0 Interrupt to falling edge. * * 6. sets the timeOut variable to set speed required. * * 7. Clears the 1 msec flag * * 8. Enables both RB0 and Timer interrupts. * * Input : None * * Outout : None * ******************************************************************************/ void initialize() { TRISA = 0; // SET PORTA AS OUTPUT PORTA = 0; // STOP MOTOR flag1Msec = 0; // initialise 1 msec flag engOkFlag = 1; // initialise engage ok flag overSpdReg = 0; // set speed trigger to 30km/hr OPTION = 0b00010111; // Prescaler /256 inc timer every 128usec TMR0 = 248; // 128 usec x 8 = 1.024 msec INTCON = 0B00110000; // ENABLE INTERRUPTS AND CLR FLAGS clockA = 0; clockB = 0; debouncedState = 1; oldSwitchState = 0; startTiming = 0; GIE = 1; overSpdCntr = 0; // initialise speed counter underSpdCntr = 0; systemState = 0; // initialize state to state0 }
Just write the constants in hex. ANSI C does not have any syntax for specifying binary integer literals. Decimal, octal, hex; no binary. If Hitech C added an extension to do so -- good for them. The Keil compiler does not happen to have this particular extension. As has been noted, the forum already contains a discussion on macro implementations to allow you to write binary constants and transform them into something compilable with standard C. ObTrivia: You can't write a literal decimal value for zero in C.
"ObTrivia: You can't write a literal decimal value for zero in C." don't you mean, "You can't write a literal decimal integer value for zero in C?" ;-)
Yep, that's what I meant. (Floats? What's a float?) Non-decimal floating point literals could get nightmarish. Maybe 0x12345678.0xdeadbeef isn't so bad, but when you start switching bases on either side of the "radix point", you're asking for headaches. Maybe we should break down and just use Ada syntax for numeric literals. Base 11? Go right ahead: 11#48a19. Although even they wimp out and limit the base to be from 2 to 16.