Click here to Skip to main content
14,084,762 members
Rate this:
Please Sign up or sign in to vote.
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 ( "movlw 0x00");
        asm ( "movwf _PORTB");       //CLEAR PINS
        asm ( "movlw 0xff" );
        asm ( "movwf _PORTB" );     //SET ALL PINS


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

#asm volatile
    movlw 0xff
    movwf _PORTB;

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...
Updated 18-Jul-11 6:43am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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;

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


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
and heres the assembly code I was working on which (should) do the same thing...

			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
			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

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190518.1 | Last Updated 19 Jul 2011
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100