1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

MIX C and Assembly in AtmelStudio

Discussion in 'AVR' started by Majid soltani, May 15, 2015.

  1. Majid soltani

    Majid soltani New Member

    Joined:
    May 6, 2015
    Messages:
    22
    Likes:
    0
    Hi my friend...

    I declare this variable :

    const unsigned char screen_pattern[8*8] PROGMEM = {
    10,20,0,11,0,125,255,13,
    10,20,0,0,0,125,255,13,
    10,20,115,90,0,125,255,13,
    10,20,0,0,0,125,255,13,
    10,47,0,0,0,125,255,13,
    10,120,0,80,0,125,255,13,
    10,200,0,0,0,125,255,13,
    10,20,0,127,0,125,255,13
    };

    now ... i want mix C and Assembly code and send screen_pattern to PORTD with assembly code .... please help me...
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    In the documentation it will show how to use assembly within the C environment... It will be quite straight forward...

    I don't use avr studio so I'm not sure....
     
  3. Majid soltani

    Majid soltani New Member

    Joined:
    May 6, 2015
    Messages:
    22
    Likes:
    0
    you use which compiler ? ... please you say your solution maybe i could use that.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK

    I use Pic's.... I don't think my compiler would be much use to you.. I haven't a copy of the GNU C compiler to hand...

    Are you using AVR studio???
     
  6. Majid soltani

    Majid soltani New Member

    Joined:
    May 6, 2015
    Messages:
    22
    Likes:
    0
    OK thank you.... Yes , I use atmelStudio 6

    now i solved my problem

    first i set an assembly name to screen_pattern[] :

    const unsigned char screen_Pattern[8*8] asm("sc_p") PROGMEM = {
    67,20,0,11,0,125,255,13,
    10,20,0,0,0,125,255,13,
    10,20,115,90,0,125,255,13,
    10,20,0,0,0,125,255,13,
    10,47,0,0,0,125,255,13,
    10,120,0,80,0,125,255,13,
    10,200,0,0,0,125,255,13,
    10,20,0,127,0,125,255,13
    };


    then use this codes :


    asm ("PUSH R16 ");
    asm ("PUSH R30 ");
    asm ("PUSH R31 ");

    // Z Register is ( R31:R30)

    asm ("LDI r30,lo8(sc_p)"); // R30 = LOW(sc_P)

    asm ("LDI r31,hi8(sc_p)"); // R31 = HIGH(sc_P)

    asm ("LPM R16 , Z "); // LPM (Load program memory) R16 = [Z] Z = first cell of screen_Pattern[]
    asm ("OUT 0x12 , R16 "); // 0x12 is address of PORTD ; PORTD=R16

    asm ("POP R31 ");
    asm ("POP R30 ");
    asm ("POP R16 ");
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Why are you wanting to use C... If you are going to program in ASM just use an assembler??
     
  8. Majid soltani

    Majid soltani New Member

    Joined:
    May 6, 2015
    Messages:
    22
    Likes:
    0
    Because ,I in a part of program have time limits and in other parts of the program do not need to spend own time writing code in assembly language.
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Okay... Some of my functions are just ASM wrappers as well.... When the fastest code possible is needed..
     
    • Like Like x 1

Share This Page