Introduction:
at the beginning of 2002, the author was writing by hand a IC card prepay to consume electricity the table working routine, this electric instrument used Philips Corporation’s 8 51 expansion monolithic integrated circuit 87LPC764, the request realizes many functions, including extinguished the demonstration, the load to calculate and to control, the instruction twinkle as well as the electric instrument each kind of parameter inquiry and so on, in brief, must period of revolution the unit to be many. The author at that time used ASM51 to complete this procedure compilation, after completing the procedure quantity is 2KB many spots. Afterward, this procedure true use, only has not made for various reasons some modification to use afterward on an aged equipment carries on the time and the load computation. Approximately one year later, the author rewrote these codes.
1 system’s improvement
It can be said that the code which realizes with ASM51 does not have what organization sense to be possible saying that wants any function to join any function, made that the procedure the structure is loose, actually this is also causes the author to decide finally rewrites these codes the reason.
As everybody knows that 87LPC764 has 4KB Flash ROM, but author’s procedure quantity only then 2KB multi-spots, thus the first idea is changes to the C language to take the main development language, should as for not cause the code space not to be insufficient. Next, considered needs fixed time the function module (or calls duty, the following is called duty) many, it is necessary to carry on the order to these duties the management. The author considered that the period of revolution piece polling mode, namely for each request time management’s duty by a time-gap, the time-gap one, namely moves its code, achieves the reasonable use system timer resources the goal. Speaking of 51 systems, generally a timer may use for at least to carry on the time piece the polling. Based on the above idea, has constructed the following data type.
typedef unsigned char uInt8
typedef struct {
void (*proc) (void); // disposal procedure
uInt8 ms_count; // time piece size
} _op_;
After the construction of data defines, then realizes the code, including three parts, namely initialization data, time piece refurbishing and time to execution.
Initialization data.
#define proc_cnt 0×08 // definition process or duty quantity
// duty stack initialization
code _op_ Op[proc_cnt]={{ic_check,10},{disp_loop,100}, \
{calc_power,150},{set_led,2},…\
};
// establishment time piece starting value
data uInt8 time_val[proc_cnt]={10,100,150,2,…};
Time piece refurbishing.
void time_int1(void) interrupt 3
{uInt8 cnt;
Time_Counter:=Time_Unit;
for (cnt=0; cnt<proc_cnt; cnt )
{time_val[cnt]–;
}
}
Duty execution.
void main(void) {
uInt8 cnt;
init(); // procedure initialization
interrupt_on(); // opens the interrupt
do {
for (cnt=0; cnt<proc_cnt; cnt )
{if (! time_val[cnt])
{time_val[cnt]=Op[cnt].ms_count;
Op[cnt].proc();
}
}
} while(1);
}
In the above structure definition, proc cannot have the parameter, between various duties’ correspondence may define a parameter memory block, carries on the data message through one kind of mechanism to be interactive, like defines a global variable. Speaking of the low-power monolithic integrated circuit system, needs to do the duty are not many, the general task quantity too will not be many, thus this kind coordinated not too difficult to process.
Perhaps everybody has such understanding, namely in a real time system, almost all specific targets have the time attribute, even if does not need the process or the duty fixed time, must carry on the inquiry and refurbishing at times not necessarily. If the IC card media check, guaranteed that each second one time was enough. Therefore, these duties may also include to this structure.
In the above procedure code, considered the monolithic integrated circuit system’s RAM limit, cannot look like some real-time OS such the duty stack establishment in RAM. Author duty stack establishment in code space, thus cannot in the program run time dynamic joins the duty, therefore requests in the program compiling time, the duty stack already determined. At the same time, defines group of counting value flag time_val, time the recording program movement time quantity, and carries on refurbishing in a timer interrupt to it. Change time piece refurbishing interrupt procedure sentence Time_Counter:=Time_Unit; Time_Unit, may change the system time piece the refurbishing granularity, generally this value measures the value determination by system’s smallest time.
At the same time, carries out the flow by the duty to be possible to know, before this kind of system structure has not changed its,/background system’s nature, was only has carried on the effective management to the backstage logical operation sequence. At the same time, if carries on the task execution flow some changes, and guarantees the time piece small duty pretage, like following procedure.
do {
for (cnt=0; cnt<proc_cnt; cnt ) {
if (! time_val[cnt]) {
time_val[cnt]=Op[cnt].ms_count;
Op[cnt].proc();
break; after // carries out completes, carries on the priority scheduling
}
}
} while(1);
Then the system becomes one take to carry out the frequency as the priority task scheduling system. Certainly, establishes this way very carefully, and must pay attention to the time piece the assignment, if the time piece is too small, then possibly causes to carry out the frequency low task to carry out with difficulty; But if has two similar time pieces, is more dangerous, possibly causes second to have the same time piece duty not to carry out, thus, the time piece’s assignment must be reasonable, and guarantees its uniqueness.
2 performance analyses and duty resolution
The above two task management way, the preceding kind carries on the dispatch in turn according to the duty stack’s order and the time piece’s size, calls it for the time being the streamlined production dispatch; Then one kind, and calls it the frequency priority scheduling. Two ways have the good and bad points respectively. As soon as streamlined production dispatch’s various duties will have equate the priority, the time piece arrive namely will be transferred according to sequence, the time piece size’s order and uniqueness will not make the request; The shortcoming is possibly causes the time piece to be small, namely the request carries out quick task to wait for the long time the. Frequency priority scheduling’s various duties according to its time piece’s size, namely carry out the frequency division priority, the time piece small duty, it carries out the frequency to be high, always has the high priority, but time piece assignment coordinated, will otherwise possibly cause to carry out the frequency low duty long time waiting.
Those who want the special attention is, two ways both have the possibility to cause some duty long time to wait for, the time piece institute establishes the time also therefore cannot take the precise time the basis, according to system’s request or need, even must carry on certain protection work in the task execution process, like interrupt mask and so on, thus in carries on time the duty plan must pay attention. If a duty is tedious or possibly needs to wait for the very long time, then must consider that the duty the resolution, a big duty refinement for the small duty, divides a time-consuming long duty into many time-consuming small duties, the coordination completes its function. If in the standby period long situation, may attach a fixed time duty, fixed time duty to then transmits a news flag, the main process has not examined the news flag immediately to return, otherwise continues to carry out. Below is the demonstration code, will suppose this duty to wait for that the very long time, presently is two duty proc1 and the proc2 coordination completes its resolution the original work, the proc1 every 100 Unit of time carries out one time, but the proc2 every 200 Unit of time will carry out one time.
// defines two duties, and joins it to the duty stack.
code _op_ Op[proc_cnt]={…,{proc1,100},{proc2,200}};
data int time1_Seg; // defines an overall situation flag
// duty realizes
void proc1(void) {
if (time1_Seg)
exit;
else
time1_Seg=const_Time1; if // the time arrived, then restores the starting value and
// then carries out the following code.
… // duty actual execution code
}
void proc2(void) {
if(time1_Seg)
time1_Seg–;
}
May see by on example, after duty resolution, does not account for the excessively many CPU time nearly, causes the duty the standby period to reduce greatly, lets CPU have the enough time to carry on the task management and the dispatch. Simultaneously also lets the procedure the constitutive property and the readability to strengthen greatly.
Conclusion
Carries on based on the above mentality and the structure after the IC card electric instrument working routine rewrites completely, system’s structure performance had the very big improvement. After the complete compilation completes, the procedure code quantity approximately is 3KB many spots, obviously this kind of structure’s procedure structure will not create the very big system expenses (majority of expenses will be because will use C the result), actually enabled the development to obtain the simplification. So long as the system segmentation is a series of duties, then joins to the duty stack carries on the translation then, the very suitable low-power monolithic integrated circuit system’s development, but the author successfully has also applied this kind of structure in many systems.