Abstract: Introduces PIC the series monolithic integrated circuit C language the development; Take HI-TECH Software Corporation’s HI-TECH PICC as the example, introduced that the PICC compiler’s characteristic and develops some questions which with it the PIC series monolithic machine-hour should pay attention.
Key word: PIC PICC compiler C language/assembly language Hi-Tech
Introduction
At present, applies widely in the market should belong to 8 monolithic integrated circuits, 8 PIC series monolithic integrated circuit which Microchip Technoloogy Corporation promotes, at present the depth is welcome in the domestic market the user, already gradually became the monolithic integrated circuit application the new wave; But it is a pity, at present domestic introduced that its C language development kit’s books and the article are quite actually few, moreover uses the human are not many, the general programmers in use its development in the process slowly to try to find out, possibly will take some tortuous paths. The author when used PIC recently the C language has met many questions, wants with recently to use PIC in here C language some experiences and the general first floor software programmer makes the exchange and introduction hope this article to uses PICC to develop the PIC series monolithic integrated circuit’s person to have the help.
At present, what in domestic uses to be many is quite Hi-Tech Hi-Tech the PICC compiler, moreover in the present market some domestic PIC monolithic integrated circuit simulator also starts to support Hi-Tech the PICC translation form; Therefore, this article mainly take Hi-Tech PICC as a foundation, introduces PIC the C language essential feature.
1 Hi-Tech PICC C language development kit’s language feature
The PICC C language defines according to ANSI C, and has carried on the C language expansion. PICC and ANSI C have a basic difference is, PICC does not support the function the recursion transfer. This is because the PIC monolithic integrated circuit’s storehouse size is by the hardware decision, the resources are limited, therefore does not support the recursion transfer. Its data also complies with standard C the construction of data, the PICC construction of data is appears by the data type form. The PICC compiler supports the data type has a type (bit), the non-sign character (unsigned char), to have the sign character (signed char), the non-mark trueing (unsigned int), to have the mark reshaping (signed int), the non-symbol bugler corporal trueing (unsigned long), to have the symbol bugler corporal trueing (signed long), the floating point (float) and the indicator type and so on. What needs to pay attention, the PICC support’s multi-byte data uses the low byte before, high byte after principle. Namely more than byte counts, for instance int, saves the order in the memory unit to save for the low byte in the address low memory cell. The upper byte saves in the address high memory cell, the programmer when uses the union defined variable certainly must pay attention to this characteristic.
The PIC C language variable divides into the local variable and the global variable, all variables must define first after the use uses. The global variable is explains outside any function, may by the random module use, in the entire program execution period maintains the effective variable. Local variable in function interior explanation. The local variable has two kinds: Automatic variable and static variable. The default type is the automatic variable, only if is clear its statement for the static variable. Moreover, all automatic variables are assigned in the register page 0, therefore bank limits the decisive remark not to be able to use in the automatic variable, then may use in static the local variable. When procedure withdrawal, the automatic variable takes the spatial release, the automatic variable also dwindles. The static variable is one kind of local variable, was only stating that its function interior is effective; But it takes the fixed memory cell, but this memory cell not by other function use, therefore other functions may through the indicator visit or the revision static variable value. The static variable starts only the initialization in the procedure one time, if therefore only uses a variable in some function interior, and hoped its value in 2 function call period maintains invariable, to realize the procedure modulation, then may its statement for the static variable. For example in following statement, somewhat for legitimate, somewhat for illegal:
void max(void)
unsigned char var1; // stated legitimately
unsigned char bank1 var2; // illegal stated
static unsigned char bank1 ver3; // stated legitimately
unsigned char var4=0×02; // stated legitimately that each time transfers initializes
static unsigned char bank1 var5=0×02; // stated legitimately, but only initialization one time
…………
}
The PICC compiler uses RAM for the local variable and the transmission parameter to cover the technology. When translation, the coupling impossible the function automatic variable area which simultaneously will transfer to overlap automatically some in the same place, will achieve the memory the highly effective use, therefore its internal RAM use efficiency will be high.
when 2 function calls parameter transmission
The PICC function parameter’s transmission is according to is passed on the parameter the length, with W, moves the function the automatic variable region or is moved the function the parameter region transmission, the transmission code is quite highly effective. Transmits may pass one for the function parameter by the question mark “?”, under draws a line “_” and the letter proper name adds a displacement quantity constitution the marking gain. Below is a transfer summation subroutine fountainhead code:
Unsigned char add_function (unsigned char augend, unsigned char addend);
Void main(void)
{
unsigned char temp1, temp2, temp3;
tem3=add_function(temp1, temp2);
}
unsigned char add_function (unsigned char augend, unsigned char addend)
{
return (augend addend);
}
After the translation, produces the assembly program is:
_main
; _temp2 assigned to? a_main 0
; _temp3 assigned to? a_main 1
; _temp1 assigned to? a_main 2
bcf status,5
bcf status,6
movf (((? a_main 0))), w
movwf (((? _add_function)))
movf (((? a_main 2))), w
fcall (_add_function)
movwf (((? a_main 1)))
_add_function
; _augend assigned to? a_add_function 0
; _augend stored from w
bcf status,5
bcf status,6
movwf (((? a_add_function 0)))
movf (((? a_add_function 0))), w
addwf (((? _add_function 0))), w
return
3 PICC languages and assembly language mix programming
In the ordinary circumstances, the master routine is compiles with the C language. The C language and the assembly language biggest difference lies, the assembly program carries out the efficiency to be high, because, the C language first needs to use the C compiler production assembly code, in many situations, the assembly code which the C compiler produces was inferior that with the assembly code efficiency which produces manually is high. In PICC, may use two methods to transfer the assembly program in the C procedure. One method is uses #asm,#endasm and asm() in the C language the direct inserting assembly code, #asm and the #endasm instruction uses in indicating the inserting assembly block the opening and the knot is separately; asm () uses in the single scroll assembly directive inserting in the code which produces to the compiler, as follows shows:
void func1(void) {
asm (”NOP”);
#asm
nop
rlf_var, f
#endasm
asm (”rlf_var, f”);
}
What needs to pay attention, the inserting assembly is not in the complete significance assembly, is one kind of false assembly directive, when the use must pay attention to them and between the compiler generating code mutual influence.
Another method is assembles takes an independent module, with assembles the compiler (ASPIC) to produce the object file, then with the link and the C language generation’s other module’s object file links in together. If the variable wants public time, in another module explained that for the exterior type, and permits the use formal parameter and returns to the value.
For example, if uses in the assembly module in the C module the function, then may know in C stated:
extern char rotate_left(char);
This statement showed must transfer this external function has a char formal parameter, and returns to a char value. But the rotate_left() function’s true function body in exterior may by the ASPIC translation assembly module (filename suffix .as), the specific code be possible to compile as follows:
processor16C84
PSECT text0, class=CODE, local, delta=2
GLOBAL _rotate_left
SIGNAT _rotate_4201
_rotate_left
movwf? a_rotate_left
rlf? a_rotate_left, w
return
FNSIZE _rotate_left,1,0
GLOBAL? a_rotate_left
END
What needs to pay attention, states the function name by the C module, is below draws a line the opening in the assembly module. GLOBAL has defined a global variable, also equates in C in the module the extern, SIGNAL compulsion link when links each object file module carries on the type match inspection, FNSIZE defines the local variable and the formal parameter memory allocation.
This method is quite troublesome, when to some module carries out the efficiency request to be high, may adopt this means; But, to guarantee that the assembly program can the normal operation, probably observe the function parameter transmission and the returns rule strictly. Certainly, to avoid the trouble which these rules bring, in the ordinary circumstances, may use the C language to compile a similar function approximately the first function, defines each kind of variable in advance, uses the PICC-S option to carry on the translation to the procedure, after then optimizes the assembly code which, manually the compiler produces took it as the independent module to be possible.
4 matters needing attention
When uses PICC, to use the resources effectively, should pay attention to the following several points:
①The use does not have the sign digit and the byte variable as far as possible.
②In the register resources permission’s situation, to certain carries out the efficiency request high even level Yuan to transfer the internal variable which mutually in the function uses, may defines it as the overall situation temporary variable, when the programming cover the use, like this may reduce many translation codes. But uses regarding the interrupt function interior variable, available global variable.
③Regarding has certain assembly experience’s person when starts uses PICC, after should pay attention to the onlooking translation, produces the assembly source code, and watches frequently after the correct translation link produces reflection document (.MAP document). In this document, listed in detail has assigned for the variable and the code address and generating code information and so on size. The user may understand whether the code does optimize, the variable assigns whether reasonably, storehouse whether to overflow and so on, thus writes the highly effective succinct C source code.
④In many situations, PICC does not support the type compulsion transformation. Namely after the type does not match must inspect the translation the assembly code, looked whether correctly, to operate particularly to the indicator time certainly must pay attention.
⑤When to some shift quantity from operation, for instance asks instead, may not write in simple form directly, for example:! flag; After the translation, cannot have the code correctly, but must write: “flag=! flag; “
⑥Chooses the global optimization translation option as far as possible. In order to guarantee register page (including procedure memory time page and RAM register page) the correct transformation, in the PICC translation code has the massive transformation register page code, will choose global optimization PICC to optimize the transformation code which massive related RP0, RP1, PCLAPH will increase, will thus speed up the program execution speed, and will save the massive procedure space.
⑦If has some code very short function to transfer frequently by many functions, should better define it as great. If because function code very short-time, because moves the function and the transfer function the additional code which produces in the identical code page institute possibly cannot surpass the function code’s length.
5 conclusions
The PICC compiler produces code in some time, although is quite tedious, but the structure and the logic are very strong, the development efficiency enhances greatly, the debugging and the maintenance are very convenient. No matter from the procedure development speed, the software quality says from the procedure maintainability and the probability, the PICC merit not assembly language can compare certainly.