Inline asembler

General discussion on mikroPascal for dsPIC30/33 and PIC24.
Post Reply
Author
Message
SamY Fareast
Posts: 46
Joined: 05 Aug 2007 07:15
Location: Shizuoka JAPAN

Inline asembler

#1 Post by SamY Fareast » 17 Jan 2009 09:56

Hi all
I'm just working with dspic30f3012 on FRC x8 clock.

Code: Select all

asm
  BTG LATC,  13
end
This code not work.

Code: Select all

asm
  MOV #$2000 , W0
  XOR LATC
Also this.
But

Code: Select all

asm
  MOV #$2000 , W0
  XOR LATC, WREG
This code work fine. But I feel something funny.
MicroChips saying on DS70157B PAGE3-5 that
When the optional {,WREG} operand is specified, the destination of the instruction is
WREG. When {,WREG} is not specified, the destination of the instruction is the file
register f.
Could anyone helps me?

User avatar
milan
mikroElektronika team
Posts: 1013
Joined: 04 May 2006 16:36
Contact:

#2 Post by milan » 19 Jan 2009 11:34

Hi,

you should check several things to locate the problem:
1) are asm instructions used in proper format
2) is compiler excepting asm instructions properly and
are they transformed to correct opcode (this is our job, there should be
no errors, we tested lot of code)
3) is there another problem (MCU, datasheet ...)

I tried this code on RC13 and it's not working:

Code: Select all

program test;
begin
  ADPCFG := 0xFFFF;
  
  TRISB := $0000;
  TRISC := $0000;

  PORTC := 0xFFFF;
  asm
    BTG LATC,  13
  end;
end.
But the same code is working on RB3 pin:

Code: Select all

program test;
begin
  ADPCFG := 0xFFFF;
  
  TRISB := $0000;
  TRISC := $0000;

  PORTB := 0xFFFF;
  asm
    BTG LATB,  3
  end;
end.

So, it is probably something with RC13 pin.
See the other functions of RC13 in the Datasheet and configure RC13 as I/O pin (if possible).
SmartADAPT2 rules !

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#3 Post by yo2lio » 19 Jan 2009 12:11

Hello Milan,

No, it's compiler problem :

Code: Select all

;test_DSPIC.dpas,10 :: 		BTG W11, #13
$0116	$A2D00B			BTG	W11, #13
;test_DSPIC.dpas,11 :: 		BTG LATB, 13
$0118	$AABFFF			BTG	LATB, 13
This ASM code not exist :

Code: Select all

BTG	LATB, 13
must be :

Code: Select all

BTG	LATB, #13
and compiler give error : 0:11 E-420 Invalid ASM instruction (51): LATB (0x000118) #13

From datasheet :

Code: Select all

BTG f,#bit4 Bit Toggle f 
BTG Ws,#bit4 Bit Toggle Ws
I'm sure that this problems will be resolved in new PRO Compiler for DSPIC.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

SamY Fareast
Posts: 46
Joined: 05 Aug 2007 07:15
Location: Shizuoka JAPAN

#4 Post by SamY Fareast » 19 Jan 2009 15:31

milan:
But the same code is working on RB3 pin:

Code: Select all

program test;
begin
  ADPCFG := 0xFFFF;
 
  TRISB := $0000;
  TRISC := $0000;

  PORTB := 0xFFFF;
  asm
    BTG LATB,  3
  end;
end.
Now I tested the code, on software debugger, but it works still funny.
After set PORTB tp $FFFF, LATB still $0000, and after BTG instruction so on.

YES, I know kind of this try will mix up compiler issue and debugger issue,
So i will try it on real chip later.
And I have a question, if you set PORTB to $0000( instead $FFFF), did your code still work fine?

yo1lio:
must be :

Code: Select all

BTG   LATB, #13
and compiler give error : 0:11 E-420 Invalid ASM instruction (51): LATB (0x000118) #13
I don't know why, but on
instruction f, #13
style code, MP's inline asembler gives this error.
ie.

Code: Select all

 BCLR LATC, #13
but

Code: Select all

 BCLR LATC, 13
works fine.

FRM
Posts: 381
Joined: 20 May 2005 18:58
Location: UK

#5 Post by FRM » 19 Jan 2009 17:03

yo2lio wrote:I'm sure that this problems will be resolved in new PRO Compiler for DSPIC.
Hi Florin, sorry to be slightly off-topic, but where can I read more about this new version :?:

User avatar
milan
mikroElektronika team
Posts: 1013
Joined: 04 May 2006 16:36
Contact:

#6 Post by milan » 21 Jan 2009 17:35

Hi,
yo2lio wrote: This ASM code not exist :

Code: Select all

BTG	LATB, 13
must be :

Code: Select all

BTG	LATB, #13
Yes, you are right. We will correct this for the next compiler release.
Thank you for the other post too (DSPIC, BTSC f,#bit4 error).

@ SamY fareast
I can't check it on HW right now, I will try the code soon.
SmartADAPT2 rules !

User avatar
milan
mikroElektronika team
Posts: 1013
Joined: 04 May 2006 16:36
Contact:

#7 Post by milan » 25 Jan 2009 13:58

Hi,

I think I found a problem.
Try this code:

Code: Select all

program test;
begin
  ADPCFG := 0xFFFF;

  TRISB := $0000;

  // RB11
  LATB := 0xFFFF;  Delay_ms(1000);
  asm
    BTG LATB,  11
  end;             Delay_ms(1000);

  LATB := 0;       Delay_ms(1000);
  asm
    BTG LATB,  11
  end;             Delay_ms(1000);
  
  // RB3
  LATB := 0xFFFF;  Delay_ms(1000);
  asm
    BTG LATB,  3
  end;             Delay_ms(1000);

  LATB := 0;       Delay_ms(1000);
  asm
    BTG LATB,  3
  end;             Delay_ms(1000);
  
end.
Code generator somehow produces the same BTG opcode $AA62CA for RB3 and RB11.

We will investigate/fix this, I am sorry for the trouble we caused.

I tested the code on 30F4013 because it has RB11 pin.

Workaround: for pins 8,9,... use if then. This code work ok on 30F4013:

Code: Select all

program test;
begin
  ADPCFG := 0xFFFF;

  TRISB := $0000;

  // RB11
  LATB := 0xFFFF;  Delay_ms(1000);
  if LATB.11 > 0 then
    LATB.11 := 0
  else
    LATB.11 := 1;  Delay_ms(1000);

  LATB := 0;       Delay_ms(1000);
  if LATB.11 > 0 then
    LATB.11 := 0
  else
    LATB.11 := 1;  Delay_ms(1000);

  // RB3
  LATB := 0xFFFF;  Delay_ms(1000);
  asm
    BTG LATB,  3
  end;             Delay_ms(1000);

  LATB := 0;       Delay_ms(1000);
  asm
    BTG LATB,  3
  end;             Delay_ms(1000);

end.
SmartADAPT2 rules !

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#8 Post by yo2lio » 26 Jan 2009 11:01

I suggest to use this code :

Code: Select all

  LATB := LATB;
  asm
    MOV #0x8000, W0   // toggle bit 15
    XOR LATB

    MOV #0x2000, W0   // toggle bit 13
    XOR LATB

    MOV #0x0800, W0   // toggle bit 11
    XOR LATB

    MOV #0x0080, W0   // toggle bit 7
    XOR LATB
  end;
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

SamY Fareast
Posts: 46
Joined: 05 Aug 2007 07:15
Location: Shizuoka JAPAN

#9 Post by SamY Fareast » 26 Jan 2009 15:34

Hi milan
I think I found a problem.
Try this code:
I tried that code on 30F4012.
As you said, RB3 blinked on EasydsPIC4 board.
But on software-simulator, LATB become $0008 at first BTG instruction.
So, I think there is another bug(s?) on simulator.

yo2lio:
Thank you for your suggestion.
On first post, I also wrote about odd behaver of XOR LATC {WREG}.
but now I cant remember that happened on real chip or on simulator.
So I will try it next weekend.

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#10 Post by yo2lio » 26 Jan 2009 15:55

You must initialize the register first :

Code: Select all

LATC := LATC;
asm
  MOV #$2000 , W0
  XOR LATC 
end;
Now must work !
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

SamY Fareast
Posts: 46
Joined: 05 Aug 2007 07:15
Location: Shizuoka JAPAN

#11 Post by SamY Fareast » 27 Jan 2009 15:35

Hi, yo2lio
You must initialize the register first :
No, LATC is one of predefined globals, so there is no need to assign initial value.

I tried this code, on 10MHz XT DSPIC30F4013 on EASYdsPIC4 board.

Code: Select all

program TestAsm;
begin
  ADPCFG := 0xFFFF;
  TRISB := $0000;
  TRISC := $0000;
  LATC := $A000;
  LATB := $AAAA;
  Delay_ms(1000);
  while true do begin
    asm
      MOV #$FFFF,W0
      XOR LATB
      XOR LATC
    end;
    Delay_ms(1000);
  end;
end.
I think LEDs on boards should blink, but not.
Next I tried this code

Code: Select all

program TestAsm;
begin
  ADPCFG := 0xFFFF;
  TRISB := $0000;
  TRISC := $0000;
  LATC := $A000;
  LATB := $AAAA;
  Delay_ms(1000);
  while true do begin
    asm
      MOV #$FFFF,W0
      XOR LATB, WREG
      XOR LATC, WREG
    end;
    Delay_ms(1000);
  end;
end.
LEDs should not blink, only value of WREG should toggled, but LEDS blink.
I can't find variable 'WREG' in p30F4013.dpas, but I can compile above code.

SamY Fareast
Posts: 46
Joined: 05 Aug 2007 07:15
Location: Shizuoka JAPAN

#12 Post by SamY Fareast » 31 Jan 2009 09:04

Oh, I am sorry, I was wrong. yo2lio
I tested this code.

Code: Select all

program TestAsm;
begin
  ADPCFG := 0xFFFF;
  TRISB := $0000;
  LATB := LATB;
  while true do begin
    asm
      MOV #$FFFF,W0
      XOR LATB, WREG
    end;
  end;
end.
MP generated this. (TestAsm.asm)

Code: Select all

;TestAsm.dpas,9 :: 		XOR LATB, WREG
$0110	$B6A2CA			XOR	LATB, WREG
Then i commented out 'LATB := LATB".
Still I could compile without any errors. But MP generated this

Code: Select all

;TestAsm.dpas,9 :: 		XOR LATB, WREG
$0110	$B6BFFF			XOR	LATB, WREG
Linker may lost LATB. and write to $1FFF.

And finally I compile this.

Code: Select all

program TestAsm;
begin
  ADPCFG := 0xFFFF;
  TRISB := $0000;
  LATB := LATB;
  while true do begin
    asm
      MOV #$FFFF,W0
      XOR LATB
    end;
  end;
end.
MP. generates this

Code: Select all

$0110	$B682CA			XOR	LATB
$82 is %10000010. This means distnation is WREG.
So it reverses.

User avatar
milan
mikroElektronika team
Posts: 1013
Joined: 04 May 2006 16:36
Contact:

#13 Post by milan » 04 Dec 2009 17:05

Hi,

this problem is solved

Code: Select all

program test;
begin
  ADPCFG := 0xFFFF;
  TRISB := $0000;
  
  while TRUE do
    begin
      asm
        BTG LATB,  #11
        BTG LATB,  #3
      end;
    end;
end.
tested on hardware :)

Solution will be included in the next release (beta version is very near now).

Thank you for your help and patience.

Check announcements on forum,
test codes from this topic with beta
and report us if something is not solved.
SmartADAPT2 rules !

Post Reply

Return to “mikroPascal for dsPIC30/33 and PIC24 General”