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, Any idea what does following do and how ? ((void (code *) (void)) 0) (); Any help would be great
Apart from the Keil-specific 'code' keyword, it's standard 'C' - see your 'C' textbook. For the Keil-specific 'code' keyword, see the Keil C51 Manual.
techven, It's still early, so spare me the tirade if I screw this up, but here goes: What this snippet of code is probably meant to do (in short form) is reset the processor (or at least jump code execution to address 0 so it can jump to the start of the application). Andy's right that this is all standard C, but just start from the 0 (the only value in the statement) and look at the casts. The value is cast to a pointer (in code memory, in this case) to a function returning void and taking no parameters (although the author put in an extra "void" for clarity). After this cast, the function at that location is "called" with the (); which will do what I described above.
"What this snippet of code is probably meant to do (in short form) is reset the processor (or at least jump code execution to address 0 so it can jump to the start of the application)." As Jay so carefully says, that may be what it's meant to do; but note that jumping to code address 0x0000 is not the same thing as resetting the processor - by a long way. A true reset will reset the chip hardware and then restart the software from code address 0x0000. Simply jumping to code address 0x0000 will leave the chip hardware in an indeterminate state when the software restarts. If you need to do a reset, the best way is to use a watchdog and just let it time-out. There's been plenty of discussion of this - try a 'Search'
I have to agree with Andy. Now that you know what this author was trying to do, the best thing for YOU to do is something entirely different. For instance, if this is done in an interrupt service routine, you're almost certainly going to have a screwed up system after this quasi-reset. And no matter where it's called from, you'll have to take care of the stack pointer, etc. to get things back to the correct state. The best way to reset will always be to turn on the watchdog and then just throw a while(1); in the code and wait for a proper reset.
"... what this author was trying to do ..." Now this author needs to figure out what processor and toolchain he/she is dealing with. http://www.htsoft.com/forum/all/showflat.php/Cat/0/Number/15018/an/0/page/0#15018
Hehe... good find Dan. It must be a bit galling to see a bunch of posts to the effect of "don't do what this hack is doing" when you wrote the code yourself. So it goes...
I think there's a more interesting point in all of this. There are no replies on the HiTech forum except Dan's. There are six replies on the Keil forum. What meaning can we read into this?
Do you really celebrate Xmas in October?
"What meaning can we read into this?" Corroborates the viewpoint favoring the 8051 in the 8051-vs-PIC market share debates?
Even at Christmas time search at http://www.keil.com/support works. Entering "void code" gives several useful answers. Item 7 is: http://www.keil.com/support/docs/307.htm This perfectly answers the initial question.