⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c517.txt

📁 dsp&c51的编程,从小百合上down的
💻 TXT
📖 第 1 页 / 共 2 页
字号:
struct sensor_desc sensor_database = { 0x30, 0x50, 0x60, 

                             0xC4, 0x21 } ; 

so that here the structure is created in memory and pre-loaded with values. 

The array case follows a similar form: 

struct sensor_desc sensor_database[4] = {{0x30,0x50,0x60, 

                            0xC4, 0x21 }, 

{ 0x32,0x56,0x56,0xC5,0x28 ; } 

} ; 

6.4.4 Placing Structures At Absolute Addresses 

It is sometimes necessary to place a structure at an absolute address. This 

might occur if, for example, the registers of a memory-mapped real time cloc 

k chip are to be grouped together as a structure. The template in this insta 

nce might be 

Contents Of RTCBYTES.C Module 

    struct RTC { unsigned char seconds ; 

             unsigned char minutes ; 

             unsigned char hours   ; 

             unsigned char days    ; 

} ; 

struct RTC xdata RTC_chip ;  // Create xdata structure 

A trick using the linker is required here so the structure creation must be 

placed in a dedicated module. This module's XDATA segement, containing the R 

TC structure, is then fixed at the required address at link time. 

Using the absolute structure could be: 

/* Structure located at base of RTC Chip */ 

MAIN.C Module 

extern xdata struct RTC_chip ; 

/* Other XDATA Objects */ 

xdata unsigned char time_secs, time_mins ; 

void main(void) { 

time_secs = RTC_chip.seconds ; 

time_mins = RTC_chip.minutes; 

} 

Linker Input File To Locate RTC_chip structure over real RTC Registers is: 

l51 main.obj,rtcbytes.obj XDATA(?XD?RTCBYTES(0h)) 

See section 7.6 for further examples of this placement method. 

6.4.5 Pointers To Structures 

/* Define pointer to structure */ 

Pointers can be used to access structures, just as with simple data items. H 

ere is an example: 

struct sensor_desc *sensor_database ; 

/* Use Pointer To Access Structure Elements */ 

sensor_database->gain = 0x30 ; 

sensor_database->offset = 0x50 ; 

sensor_database->temp_coeff = 0x60 ; 

sensor_database->span = 0xC4 ; 

sensor_database->amp_gain = 0x21 ; 

Note that the '*' which normally indicates a pointer has been replaced by ap 

pending '->' to the pointer name. Thus '*name' and 'name->' are equivalent. 

6.4.6 Passing Structure Pointers To Functions 

A common use for structure pointers is to allow them to be passed to functio 

ns without huge amounts of parameter passing; a typical structure might cont 

ain 20 data bytes and to pass this to a function would require 20 parameters 

 to either be pushed onto the stack or an abnormally large parameter passing 

 area. By using a pointer to the structure, only the two or three bytes that 

 constitute the pointer need be passed. This approach is recommended for C51 

 as the overhead of passing whole structures can tie the poor old 8051 CPU i 

n knots! 

This would be achieved thus: 

struct sensor_desc *sensor_database ; 

sensor_database-> gain = 0x30 ; 

sensor_database-> offset = 0x50  ; 

sensor_database-> temp_coeff = 0x60 ; 

sensor_database-> span = 0xC4 ; 

sensor_ database- >amp_gain = 0x21 ; 

test_function(*struct_pointer) ; 

test_function(struct sensor_desc *received_struct_pointer) { 

   received_struct_pointer->gain = 0x20 ; 

   received_struct_pointer->temp_coef = 0x40 ; 

   } 

Advanced Note: Using a structure pointer will cause the called function to o 

perate directly on the structure rather than on a copy made during the param 

eter passing process. 

6.4.7 Structure Pointers To Absolute Addresses 

It is sometimes necessary to place a structure at an absolute address. This 

might occur if, for example, a memory-mapped real time clock chip is to be h 

andled as a structure. An alternative approach to that given in section 6.4. 

4. is to address the clock chip via a structure pointer. 

The important difference is that in this case no memory is reserved for the 

structure - only an "image" of it appears to be at the address. 

The template in this instance might be: 

/* Define Real Time Clock Structure */ 

struct RTC {char seconds ; 

        char mins ; 

        char hours ; 

        char days ; } ; 

/* Create A Pointer To Structure */ 

struct RTC xdata *rtc_ptr ;  // 'xdata' tells C51 that this 

                     //is a memory-mapped device. 

void main(void) { 

   rtc_ptr = (void xdata *) 0x8000 ;  // Move structure 

                          // pointer to address 

                          //of real time clock at 

                          // 0x8000 in xdata 

    rtc_ptr->seconds = 0 ;  // Operate on elements 

    rtc_ptr->mins = 0x01 ; 

   } 

This general technique can be used in any situation where a pointer-addresse 

d structure needs to be placed over a specific IO device. However it is the 

user's responsibility to make sure that the address given is not likely to b 

e allocated by the linker as general variable RAM! 

To summarize, the procedure is: 

Define template 

Declare structure pointer as normal 

At run time, force pointer to required absolute address in the normal way. 

6.5 Unions 

A union is similar in concept to a structure except that rather than creatin 

g sequential locations to represent each of the items in the template, it pl 

aces each item at the same address. Thus a union of 4 bytes only occupies a 

single byte. A union may consist of a combination of longs, char and ints al 

l based at the same physical address. 

The the number of bytes of RAM used by a union is simply determined by the s 

ize of the largest element, so: 

union test { char x ; 

             int y  ; 

             char a[3] ; 

             long z ; 

} ; 

requires 4 bytes, this being the size of a long. The physical location of ea 

ch element is: 

addr _ 0   x byte  y high byte a[0]  z highest byte 

       +1           y low byte a[1]  z byte 

       +2                      a[2]  z byte 

       +3                      a[3]  z lowest byte 

Non-8051 programmers should see the section on byte ordering in the 8051 if 

they find the idea of the MSB being at the low address odd! 

In embedded C the commonest use of a union is to allow fast access to indivi 

dual bytes of longs or ints.  These might be 16 or 32 bit real time counters 

, as in this example: 

/* Declare Union */ 

union clock {long real_time_count ; // Reserve four byte 

    int real_time_words[2] ;      // Reserve four bytes as 

                        // int array 

    char real_time_bytes[4] ;     // Reserve four bytes as 

                          // char array 

      } ; 

/* Real Time Interrupt */ 

void timer0_int(void) interrupt 1 using 1 { 

    clock.real_time_count++ ;       // Increment clock 

    if(clock.real_time_words[1] == 0x8000) { // Check 

                    // lower word only for value 

    /* Do something! */ 

    } 

    if(clock.real_time_bytes[3] == 0x80) {  // Check most 

                   // significant byte only for value 

    /* Do something! */ 

    } 

    } 

6.6 Generic Pointers 

C51 offers two basic types of pointer, the spaced (memory-specific) and the 

generic. Up to version 3.00 only generic pointers were available. 

As has been mentioned, the 8051 has many physically separate memory spaces, 

each addressed by special assembler instructions. Such characteristics are n 

ot peculiar to the 8051 - for example, the 8086 has data instructions which 

operate on a 16 bit (within segment) and a 20 bit basis. 

For the sake of simplicity, and to hide the real structure of the 8051 from 

the programmer, C51 uses three byte pointers, rather than the single or two 

bytes that might be expected. The end result is that pointers can be used wi 

thout regard to the actual location of the data. 

For example: 

 xdata char buffer[10] ; 

 code char message[] = { "HELLO" } ; 

void main(void) { 

       char *s ; 

       char *d ; 

       s = message ; 

       d = buffer ; 

       while(*s != '\0') { 

          *d++ = *s++ ; 

          } 

   } 

Yields: 

    RSEG  ?XD?T1 

buffer:            DS  10 

    RSEG  ?CO?T1 

message: 

    DB  'H' ,'E' ,'L' ,'L' ,'O' ,000H 

; 

; 

; xdata char buffer[10] ; 

; code char message[] = { "HELLO" } ; 

; 

;    void main(void) { 

    RSEG  ?PR?main?T1 

    USING    0 

main: 

            ; SOURCE LINE # 6 

; 

;       char *s ; 

;       char *d ; 

; 

;       s = message ; 

            ; SOURCE LINE # 11 

    MOV      s?02,#05H 

    MOV      s?02+01H,#HIGH message 

    MOV      s?02+02H,#LOW message 

;       d = buffer ; 

            ; SOURCE LINE # 12 

    MOV      d?02,#02H 

    MOV      d?02+01H,#HIGH buffer 

    MOV      d?02+02H,#LOW buffer 

?C0001: 

; 

;       while(*s != '\0') { 

            ; SOURCE LINE # 14 

    MOV      R3,s?02 

    MOV      R2,s?02+01H 

    MOV      R1,s?02+02H 

    LCALL    ?C_CLDPTR 

    JZ       ?C0003 

;          *d++ = *s++ ; 

            ; SOURCE LINE # 15 

    INC      s?02+02H 

    MOV      A,s?02+02H 

    JNZ      ?C0004 

    INC      s?02+01H 

?C0004: 

    DEC      A 

    MOV      R1,A 

    LCALL    ?C_CLDPTR 

    MOV      R7,A 

    MOV      R3,d?02 

    INC      d?02+02H 

    MOV      A,d?02+02H 

    MOV      R2,d?02+01H 

    JNZ      ?C0005 

    INC      d?02+01H 

?C0005: 

    DEC      A 

    MOV      R1,A 

    MOV      A,R7 

    LCALL    ?C_CSTPTR 

;          } 

            ; SOURCE LINE # 16 

    SJMP     ?C0001 

;       } 

            ; SOURCE LINE # 17 

?C0003: 

    RET 

; END OF main 

    END 

As can be seen, the pointers '*s' and '*d' are composed of three bytes, not 

two as might be expected. In making *s point at the message in the code spac 

e an '05' is loaded into s ahead of the actual address to be pointed at. In 

the case of *d '02' is loaded. These additional bytes are how C51 knows whic 

h assembler addressing mode to use. The library function C_CLDPTR checks the 

 value of the first byte and loads the data, using the addressing instructio 

ns appropriate to the memory space being used. 

This means that every access via a generic pointer requires this library fun 

ction to be called. The memory space codes used by C51 are: 

CODE  - 05 

XDATA - 02 

PDATA - 03 

DATA  - 05 

IDATA - 01 

6.7 Spaced Pointers In C51 

Considerable run time savings are possible by using spaced pointers. By rest 

ricting a pointer to only being able to point into one of the 8051's memory 

spaces, the need for the memory space "code" byte is eliminated, along with 

the library routines needed to interpret it. 

A spaced pointer is created thus: 

char xdata *ext_ptr ; 

to produce an uncommitted pointer into the XDATA space or 

char code *const_ptr ; 

which gives a pointer solely into the CODE space. Note that in both cases th 

e pointers themselves are located in the memory space given by the current m 

emory model. Thus a pointer to xdata which is to be itself located in PDATA 

would be declared thus: 

pdata char xdata *ext_ptr ; 

   |         | 

location     | 

of pointer   | 

        Memory space pointed into 

        by pointer 

In this example strings are always copied from the CODE area into an XDATA b 

uffer. By customising the library function "strcpy()" to use a CODE source p 

ointer and a XDATA destination pointer, the runtime for the string copy was 

reduced by 50%. The new strcpy has been named strcpy_x_c(). 

The function prototype is: 

extern char xdata *strcpy(char xdata*,char code *) ; Here is the code produc 

ed by the spaced pointer strcpy(): 

; char xdata *strcpy_x_c(char xdata *s1, char code *s2)  { 

_strcpy_x_c: 

    MOV      s2?10,R4 

    MOV      s2?10+01H,R5 

;__ Variable 's1?10' assigned to Register 'R6/R7' __ 

;   unsigned char i = 0; 

;__ Variable 'i?11' assigned to Register 'R1' __ 

    CLR      A 

    MOV      R1,A 

?C0004: 

; 

;   while ((s1[i++] = *s2++) != 0); 

    INC      s2?10+01H 

    MOV      A,s2?10+01H 

    MOV      R4,s2?10 

    JNZ      ?C0008 

    INC      s2?10 

?C0008: 

    DEC      A 

    MOV      DPL,A 

    MOV      DPH,R4 

    CLR      A 

    MOVC     A,@A+DPTR 

    MOV      R5,A 

    MOV      R4,AR1 

    INC      R1 

    MOV      A,R7 

    ADD      A,R4 

    MOV      DPL,A 

    CLR      A 

    ADDC     A,R6 

    MOV      DPH,A 

    MOV      A,R5 

    MOVX     @DPTR,A 

    JNZ      ?C0004 

?C0005: 

;   return (s1); 

; } 

?C0006: 

    END 

Notice that no library functions are used to determine which memory spaces a 

re intended. The function prototype tells C51 only to look in code fot the s 

tring and xdata for the RAM buffer. 

---------------------------------------------------------------------------- 

---- 

  



-- 

Ours is essentially a tragic age, so we refuse to take it tragically. 

  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -