+ Post New Thread
Results 1 to 2 of 2
  1. #1
    Junior Member level 1
    Points: 263, Level: 3

    Join Date
    Dec 2017
    Posts
    16
    Helped
    0 / 0
    Points
    263
    Level
    3

    Writing AVR inline assembly code in C++ file

    I wrote a inline assembly code in which port pins 0 to 5 represent PD2 to PD7 and 6 and 7 represent PB0 and PB1 respectively. High(pin) is used to make the respective pin logical HIGH. I referred avr-libc-user-manual.pdf to write the following code. But I went wrong some where.

    Code:
    #include 
    class GPIO{
    	private:
    	uint8_t ddrx, portx, reg0 = 0, reg1 = 1, reg2 = 0;
    	void selectIO(uint8_t);
    	void shiftBit(uint8_t);
    	void readDDRx();
    	void readPORTx();
    	void writeDDRx();
    	void writePORTx();
    	void ORing();
    	
    	public:
    	void High(uint8_t);
    	};
    
    int main(void)
    {
    	GPIO port;
    	port.High(0);
    
    	return 0;
    }
    
    void GPIO :: selectIO(uint8_t pin){
    	if((pin >= 0) && (pin <= 5)){
    		ddrx = 0x0A;
    		portx = 0x0B;
    	}
    	else if((pin >= 6) && (pin <= 7)){
    		ddrx = 0x04;
    		portx = 0x05;
    	}
    }
    
    //DDRx |= (1 << pin);
    //PORTx |= (1 << pin);
    
    void GPIO :: shiftBit(uint8_t pin){                                   //To perform (1 << pin);
    	asm volatile("mov %A0, %A1 \n\t"
    		          "rjmp down    \n\t"
    			  "up:          \n\t"
    			  "clc          \n\t"
    			  "rol %A2      \n\t"
    			  "down:        \n\t"
    			  "dec %A0      \n\t"
    			  "brpl up      \n\t"
    			  : "+r" (reg0), "+r" (pin), "+r" (reg1) : );
    }
    
    void GPIO :: readDDRx(){
    	asm volatile("in %A3, %A4" : "+r" (reg2) : "I" (ddrx));
    }
    
    void GPIO :: writeDDRx(){
    	asm volatile("out %A3, %A4" : "+r" (reg2) : "I" (ddrx) );
    }
    
    void GPIO :: readPORTx(){
    	asm volatile("in %A3, %A5" : "+r" (reg2) : "I" (portx));
    }
    
    void GPIO :: writePORTx(){
    	asm volatile("out %A3, %A5" : "+r" (reg2) : "I" (portx) );
    }
    
    void GPIO :: ORing(){
    	asm volatile("or %A3, %A1" : "+r" (reg2) : "r" (reg1) );
    }
    
    void GPIO :: High(uint8_t pin){
    	selectIO(pin);
    	shiftBit(pin);
    	readDDRx();
    	ORing();                       //reg2 = DDRx | (1 << pin)
    	writeDDRx();                //DDRx = reg2
    	readPORTx();
    	ORing();                      //reg2 = PORTx | (1 << pin)
    	writePORTx();              //PORTx = reg2
    }
    In the inline assembly code
    %A0 is reg0
    %A1 is pin
    %A2 is reg1
    %A3 is reg2
    %A4 is ddrx
    %A5 is portx

    I got these errors
    Click image for larger version. 

Name:	asm1.PNG 
Views:	2 
Size:	31.4 KB 
ID:	149965
    Click image for larger version. 

Name:	asm2.PNG 
Views:	5 
Size:	106.2 KB 
ID:	149966

    •   AltAdvertisment

        
       

  2. #2
    Super Moderator
    Points: 28,025, Level: 40
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,358
    Helped
    1057 / 1057
    Points
    28,025
    Level
    40
    Blog Entries
    6

    Re: Writing AVR inline assembly code in C++ file

    Not clear what exactly you want to achieve on using mixed language coding as such, but you should be aware that there are few things that could be made strictly in ASM language, not with C/C++; if I understood correctly you just want to make a bit shift; consider rewriting the above code in just one language.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



--[[ ]]--