17.01.2015 Views

AN109 1Introduction of C Compiler

AN109 1Introduction of C Compiler

AN109 1Introduction of C Compiler

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Introduction <strong>of</strong> C <strong>Compiler</strong><br />

How to Use Struct And Write Struct Data to<br />

ROM Area<br />

Below example 7 discribes how to store struct data in the<br />

ROM and shows how to read struct data in the<br />

function_read(). But the function_write(), which writes<br />

data in the ROM area, is a bad example because<br />

cook_data pointer indicates the ROM area. Example 8<br />

describes struct copy function which copys struct data in<br />

the ROM to struct variable in the RAM. It will be need in<br />

the embedded system which has small RAM size. And<br />

example 9 describes use <strong>of</strong> struct in the inline-asm.<br />

When you use array <strong>of</strong> struct and a variable which indicate<br />

a location <strong>of</strong> array, it will increase the ROM size<br />

remarkablely. So, if you reduce a ROM size, you would<br />

better change C to ASM like example 10. It will reduce a<br />

ROM size dramatically. But if the locational variable <strong>of</strong><br />

array is a constant, ROM size <strong>of</strong> C is as same as one <strong>of</strong><br />

ASM.<br />

typedef struct<br />

{<br />

unsigned char model;<br />

unsigned char power_level[2];<br />

unsigned char disp_data[2];<br />

}cook;<br />

cook RICE[] __attribute__ ((section(".text"))) = {<br />

{1,100,100,4,5},<br />

{2,100,100,3,4},<br />

{3,100,100,2,3}<br />

};<br />

cook *cook_data;<br />

cook data_ram;<br />

unsigned char model, power, disp;<br />

void function_read(void)<br />

{<br />

cook_data = &RICE[1];<br />

model = cook_data->model;<br />

power = cook_data->power_level[0];<br />

disp = cook_data->disp_data[0];<br />

}<br />

// bad example<br />

void function_write(void)<br />

{<br />

cook_data = &RICE[1];<br />

cook_data->model = model;<br />

cook_data->power_level[0] = power;<br />

cook_data->disp_data = disp;<br />

}<br />

Example 7. Use <strong>of</strong> Struct<br />

// struct size : 5bytes<br />

void struct_copy(cook data_ram, cook cook_data)<br />

{<br />

unsigned char i;<br />

for ( i=0 ; i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!