Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I'm having some difficulty with accessing some pic ports through inline assembly and would appriciate some advice.

I'm just trying to write to one of the ports on my pic 18f using mplab with hitech's c18 compiler, which largly consists of a switch statement.
the Idea is to send an 8-bit value over four data lines with an enable signal. I'm going to use a bit of code I wrote a few years ago, but first I need to figure out how to write to the pins.

I've rolled everything back to literally just trying to get some leds to light up. I know the switch statement triggers ok as I can access the port through standard c code, but although the code compiles, there is no action from within the inline asm...

I've tried it both ways...

case 0x3F:
        asm ( "GLOBAL _PORTB");     //DECLAIRE AS GLOBAL VARIABLE
        asm ( "movlw 0x00");
        asm ( "movwf _PORTB");       //CLEAR PINS
        asm ( "movlw 0xff" );
        asm ( "movwf _PORTB" );     //SET ALL PINS
IDENT_PORT = 0X01;          //SWITCH STATEMENT IS FINE...
         break; 


and...

if(j > 0)
      {
         j = j+1;       //dummy loop...recomended on the hitech website
      }                 //when using asm blocks in constructs

#asm volatile
GLOBAL _PORTB;
    movlw 0xff
    movwf _PORTB;
#endasm    


It must be something to do with calling the PORTB as a variable, as the underscore identifies access to a global variable, but PORTB Isn't a variable right? ...but the compiler shows an error when I try to access it as PORTB. The manual is fairly light on asm...
Posted
Updated 18-Jul-11 6:43am
v4

1 solution

well I managed to get round it...not sure if it'll be of any use, but you never know, somebody my find it in the archives in years to come...

case 0x3F:

    IDENT_PORT = 0X01;
    j = 0x3F;
    lower_nibble = (j && 0xf0);
    PORTB = lower_nibble;
    DelayMs(50);

    asm( " swapf _j " );
    asm( " andlw 0x0f0 " );
    asm( " movwf _upper_nibble ");
    
    PORTB = upper_nibble;

     break;


or completly in c...

case 0x3f:                                          //bits 4->7 are data lines. bit 3 is enable.

        j = 0x3f;                                   //load j with recieved movement data
        upper_nibble = j & 0x0f0;                   //and with b11110000 to get upper nibble
        PORTB = upper_nibble;                       //load onto data lines

        RB3 = 1;                                    //set enable pin high
        asm( " nop " );                             //skip instruction
        RB3 = 0;                                    //clear enable pin

        k = (j << 4) | (j >> 4);                    //switch nibbles of 0x3f
        lower_nibble = k & 0x0f0;                   //AND with 0x0f0
        PORTB = lower_nibble;                       //load lower nibble onto portb

        RB3 = 1;                                    //pulse enable line
        asm( " nop " );
        RB3 = 0;
        PORTB = 0x00;                               //clear and break
        break;
and heres the assembly code I was working on which (should) do the same thing...

#asm
			movlw 0x3f 								
			movwf _tempstore					
			andlw 0x0f0								
			movwf _PORTD							
			call pulse_e					
			call Delay5
			
			swapf _tempstore					
			andlw 0x0f0								
			movwf _PORTD								
			call pulse_e						
			call Delay255
			movlw 0x00
			goto DoNothing

pulse_e: 	        bsf _PORTC, ENABLE				
			call Delay100
			bcf	_PORTC, ENABLE
			retlw 0x00

Delay255:	movlw	0xff							
			goto	d0
Delay100:	movlw	0x64						
			goto	d0
Delay10:	movlw	0x0a							
			goto	d0
Delay5:		movlw	0x05							
d0:			movwf	_count1							
d1:			movlw	0xE7
			movwf	_counta							
			movlw	0x04
			movwf	_countb						
Delay_0:	decfsz	_counta, f						
			goto	$+2
			decfsz	_countb, f
			goto	Delay_0
			decfsz	_count1	,f
			goto	d1
			retlw	0x00
DoNothing:  
			nop
#endasm


Not the most attractive of solutions but are still fairly robust...any questions or critisims just holla...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900