This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Step by step ...HOW TO CREATE A LIBRARY?

Hi all!
i am working on a project where i need to control an external ADC chip with an unusual
SPI interface. in other risks (and other compilers) i did this with inline assembly succesfully. With UV2 it seems not to work so well...NOT AT ALL ACTUALLY!
I suppose it is my fault, but i decided to create a library file for this purpose, as a driver for the ADC. there is only one simple function to implement , which refreshes the ADC with new value.i prefer to use assembly language for this purpose. i have written the asm code and the ADC.A51 file is ready and assembled succesfully. now i need some help on how to turn this into a library...actually i need a detailed description cause i seem to have trouble with basics.i use the output options to tell UV2 to turn my file into a lib file , and so it does.but i guess i need some init code to include in my asm file and a header file.
so questions:
1.what else do i need to include/declare in my asm file so that the file is ready to be used for library?
2. how to create a header file?
............IN OTHER WORDS I NEED A STEP BY STEP TUTORIAL on how to create a lib (from my asm subroutine) FROM SCRATCH!
thanx in advance
Alex

Parents
  • step 1 Create a module with dummy C function ralph.c with the parametres and return you want from your assembly function.
    step 2 cut the generated assembly and save it as Ralph.a51.
    step3 add you code to ralph.a51.
    step4 assemble ralph.a51

    Erik

Reply
  • step 1 Create a module with dummy C function ralph.c with the parametres and return you want from your assembly function.
    step 2 cut the generated assembly and save it as Ralph.a51.
    step3 add you code to ralph.a51.
    step4 assemble ralph.a51

    Erik

Children
  • Hi,
    thank you for your answer.
    I've read this "recipie" before , but i suppose this wom't create a lib.
    Am i right?

  • but i suppose this wom't create a lib
    no, but this will

    if not exist as.lib goto usliex
    del as.lib
    :usliex
    c:\keil\c51\bin\lib51 c as.lib					>..\trash\trashbin
    c:\keil\c51\bin\lib51 a avmain.obj   to as.lib	>..\trash\trashbin
    

    Erik

  • Now you confused me! I am not a C or Compiler expert. I used to write code in assembly all these years.i bet you are right
    but a novice like moi....can't handle all this. what i did just now is the following:
    i started a new project with my_lib.c file as source with my_func as the fuction that implements the code to be compiled to library and using the #pragma asm i added
    assembly code in there , using R7 and R5 to pass variables to asm code.then i configured OUTPUT to create a lib instead of exe and compiled.my_lib was created. can i use this now , or do i have to create a header file?(if yes how????)
    Thanks again

  • PLEASE
    rewrite your post - separate and number the questions.

    e.g. what does #pragma asm i added have to do with a library?

    btw why on earth do you use >#pragma asm instead of just making a .a51 file?

    Erik

  • Hi
    I used #pragma asm because i wanted to add my asm routine inside the function that I wrote.
    I read some other open source libraries and saw some mixed-langauge implementations of lib-source.
    actually i thought that writting a C function with inline assembly and compiling it to Lib is the best way.here is what i did:

    #pragma SRC
    unsigned char	Dac8801	(unsigned char val,unsigned char chan)
    {
    if (chan>7) return(0);
    #pragma asm
    mov	A,R0
    push	ACC
    mov	A,#0x07
    ORL P2,#00000111B
    clr	P2.0		;clr	CS
    mov	A,R7            ;load dac_no to A
    RR	A
    RR	A
    RR	A
    mov		R0,#3
    loop1:
    RLC	A
    MOV P2.2,C
    clr	P2.1			;clr	CLK
    setb	P2.1		;set	CLK
    DJNZ	R0,loop1
    mov	A,R5            ;load dac_data to A
    mov		R0,#8
    loop2:
    RLC	A
    MOV P2.2,C
    clr	P2.1		;clr	CLK
    setb	P2.1		;set	CLK
    DJNZ	R0,loop2
    pop	ACC
    mov	R0,A
    setb	P2.0
    #pragma endasm
    return(1);
    }
    

    here are 3 questions:
    1) is this a good start?
    2) what does it take to make ports and pins be defined outside the library
    3) if all is good , how do I write a header file for this?
    Alex

  • actually i thought that writting a C function with inline assembly and compiling it to Lib is the best way
    You can not "compile to lib" you can only "compile to obj" THEN you can put the object in a library.
    1) is this a good start?
    2) what does it take to make ports and pins be defined outside the library
    3) if all is good , how do I write a header file for this?

    1) sure, you need code before you can put it in a library; however I still would recommend a pure assembler module.
    2) you can't - SFRs are absolute addresses and used at COMPILE time.
    3) just as for any other module

    Erik

  • I am afraid i am being a pain in the ...
    Anyway you assume that i know how to "write"
    a header file , but i don't!
    would it be easier if you gave me an example file for lib and header? the features i need is to pass 2 unsigned char to it and control 3 I/O pins inside my function.
    my code is in assebly as you already know. i saw a lib that expects a definition of port and pin addresses before including the lib so that the lib "knows" from definition which ports to use. for example a clock pin and a data pin for soft-SPI lib would ask for a definition (inside main source) like:

    #define SWSPI_PORT 0x20 // soft-SPI port address
    #define CLK 0           //clock pin number
    #define SDI 1           // SDI (data) pin number
    
    which would tell the compiler that
    the library function when refering to
    CLK , means P1.0,SDI means P1.1

  • would it be easier if you gave me an example file for lib and header?
    there is tons of examples right here at the Keil site and the below, actually, is an extract from a .h file.

    #define SWSPI_PORT 0x20 // soft-SPI port address
    #define CLK 0 //clock pin number
    #define SDI 1 // SDI (data) pin number

    which would tell the compiler that
    the library function when refering to
    CLK , means P1.0,SDI means P1.1

    That works, but require "presence at compile time" which you can not have for a library.

    Erik

  • 2) you can't - SFRs are absolute addresses and used at COMPILE time.

    Actually, you can. It's just a little tricky and isn't recommended for novices.

    http://www.keil.com/support/docs/2413.htm

    Jon