Would appreciate if someone can tell/show me how to combine C and assembly code in the Uvision 2?
Well, I suppose it would do that, wouldn't it! As noted elsewhere in this thread, when you use #pragma asm ... #pragma endasm in your 'C' source you also have to use the SRC directive, and then pass the resulting assembly source to the Assembler. Thus it is not the Compiler which generates the object code, but the Assembler. Therefore it is the Assembler which inserts the debug info into the object file, and not the Compiler. Therefore the debug info (including line numbers) will refer to the Assembly source, and not to the 'C' source!
thank for ur help. pls check if what i,am doing is right. example: mov DPTR,#IO_PORT0 mov A,#55h movx @dptr,A can it be done as in C: extern xdata unsigned char IO_PORT=0x9800; void main() {unsigned charx; x&=0x55; } can it be write in this way?? if not how should i write?hope that u can show me. pls also should i use extern? xdata or idata? What is the main different?? pls may i know when should i use XBYTE.
Problem: You want to move 0x55 to XDATA address IO_PORT at 0x9800. Solution:
// Do not forget volatile. volatile unsigned char IO_PORT _at_ 0x9800; void main(void) { unsigned char charVal; // Write. IO_PORT = 0x55; // Read. charVal = IO_PORT; // Read again. if (IO_PORT & 0x80) { // High bit set. } }
Never use XBYTE May I ask why?
Just because it's a macro and I don't like macros when the implementation provides a clean way to do the same thing. Every thing I say is of course an opinion, take it with a grain of salt. Regards. - Mark
when should the word extern. when xdata should or should not be use. is xdata does the same as xbyte. pls, how can i increase DPTR when i want to write it in C? Also how can i just increase DPL? And how can i write the push and pop of assembly to. pls show me with some example. Is there any side where i can find out more about using c to to control 8051. Thank you.
extern is only need on variables when you need to define them in one C file but use them in at least one other C file. The variables are extern'd in an include (.h) file typically which is included by all C files that require that variable. You can access DPTR via the sfr keyword extension. Read you C51 manual. I don't advise touching the DPTR however since you may clobber the compiler's use of it. Likewise you can access ports on the 8051 like this:
sfr port0 = 0x80; sbit portBit0_0 = port0 ^ 0; sbit portBit0_1 = port0 ^ 1; ... sbit portBit0_7 = port0 ^ 7; port0 = 0xFF; // Set all pins high portBit0_1 = 0; // Set Port 0.1 low
char xdata myVariable;
char idata someVar; int data otherVar;
char xdata aVar _at_ 0x8000; int code bVar _at_ 0xFFFE;
Hi Mark/Andrew, Read through your advice to others. Splendid! Maybe either of you can assist me too. I came across this forum when I am seeking for some quick guide of sharing code of different files of C and .a51. Calling each other from C to get the function written in .a51 or vice-versa is completed. Went through c2asm2c.zip and it worked perfectly. However, I encounter some trouble when I need to pass on some variables that need to be manipulated from .a51 to .c My case:- Main code is in .a51. Requires to call a function that adds 20 slightly different 8bit data bytes and then take its average (i.e. by division). Writing the function above in .a51 is a pain due to unavailable 16bit-addition in 8051 done under Keil uV2. My solution thought:- is to write the addition function in C so addition can be done much more easily. Problem:- how do I pass the variables/registers defined in .a51 to the C function to be called from .a51 environment? The C function failed to recognize the similar declared register under .a51 before. Do I need to declare them under a new name OR use the similar name of data registers OR can use some global declaration of data registers. But how? Appreciate some really cool advice if available. Cheers, James