pic18f2321 wont run

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,304
I am trying to get this new pic to run, never used it before so i dont know what bits to set in the config to get it just to light up leds on all ports A,B,C


here is the ASM file any clues what have i missed off?

Rich (BB code):
;******************************************************************************
;   This file is a basic template for creating relocatable assembly code for  *
;   a PIC18F2220. Copy this file into your project directory and modify or    *
;   add to it as needed.                                                      *
;                                                                             *
;   The PIC18FXXXX architecture allows two interrupt configurations. This     *
;   template code is written for priority interrupt levels and the IPEN bit   *
;   in the RCON register must be set to enable priority levels. If IPEN is    *
;   left in its default zero state, only the interrupt vector at 0x008 will   *
;   be used and the WREG_TEMP, BSR_TEMP and STATUS_TEMP variables will not    *
;   be needed.                                                                *
;                                                                             *
;   Refer to the MPASM User's Guide for additional information on the         *
;   features of the assembler and linker.                                     *
;                                                                             *
;   Refer to the PIC18F2221/2321/4221/4321 Data Sheet for additional          *
;   information on the architecture and instruction set.                      *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Filename:                                                                *
;    Date:                                                                    *
;    File Version:                                                            *
;                                                                             *
;    Author:                                                                  *
;    Company:                                                                 *
;                                                                             * 
;******************************************************************************
;                                                                             *
;    Files required: P18F2321.INC                                             *
;                                                                             *
;******************************************************************************

	LIST P=18F2321, F=INHX32 ;directive to define processor and file format
	#include <P18F2321.INC>	 ;processor specific variable definitions

;******************************************************************************
;Configuration bits
;Microchip has changed the format for defining the configuration bits, please 
;see the .inc file for futher details on notation.  Below are a few examples.

	; This code will start executing when a reset occurs.

	ORG 0x000  ; RESET VECTOR

	goto Start

               
	CONFIG WDT=off , BOR= off ,OSC = INTIO2 , FCMEN =  off ,  MCLRE = off  ,PBADEN = DIG  ,	 CP0 = OFF , CP1 = OFF  
          ;internal osc i/o on ra6, ra7   ; RE3 input pin enabled; MCLR disabled   ; wdt off re1 internal set to V+ ; brown out off
	Banksel OSCCON
	movlw b'01111111'        ; 8mhz clock internal
	movwf OSCCON
 	  
	CLRF PORTA 
	CLRF PORTB
	CLRF PORTC

	CLRF LATA 
	CLRF LATB
	CLRF LATC
	MOVLW  b'00001111'  ; Configure  A/D off vref internal to set A?D on AN0 inpt E0h
	MOVWF ADCON1        ; for digital inputs
	MOVWF b'00000111'        ; Turn comparators off
	MOVWF CMCON  
	MOVLW	.0 
	MOVWF TRISA   ; outputs
	MOVWF TRISB   ;outputs
	MOVWF TRISC   ; outputs
	clrf PORTA
	clrf PORTB
	clrf PORTC
	clrf PORTE   ; port E,0 as input I/O
		errorlevel -302     ; suppress banksel warning messages during assembly
        errorlevel -311     ; suppress HIGH operator warning messages during assembly
		errorlevel -207      ; suppress labels warning





;******************************************************************************
;Variable definitions
; These variables are only needed if low priority interrupts are used. 
; More variables may be needed to store other special function registers used
; in the interrupt routines.
				
	CBLOCK 0x20          ; user file mnames
	 Temp
	HUNS
	TENS
	UNIITS
	d1
	d2 	
	d3
	d4
	
    ENDC
;******************************************************************************
;EEPROM data
; Data to be programmed into the Data EEPROM is defined here

;DATA_EEPROM	CODE	0xf00000

		;DE	"Test Data",0,1,2,3,4,5




;******************************************************************************
;High priority interrupt vector
; This code will start executing when a high priority interrupt occurs or
; when any interrupt occurs if interrupt priorities are not enabled.

;HI_INT_VECTOR	CODE
;	0x0008

	;	bra	HighInt		;go to high priority interrupt routine

;******************************************************************************
;Low priority interrupt vector
; This code will start executing when a low priority interrupt occurs.
; This code can be removed if low priority interrupts are not used.

;LOW_INT_VECTOR	CODE	0x0018

;		bra	LowInt		;go to low priority interrupt routine

;******************************************************************************
;High priority interrupt routine
; The high priority interrupt code is placed here.



;******************************************************************************
;Low priority interrupt routine
; The low priority interrupt code is placed here.
; This code can be removed if low priority interrupts are not used.



;******************************************************************************
;Start of main program

Start


	
	movlw .255
	movwf PORTB
	movwf PORTC
	movwf PORTA
	goto Start





	

		END
 
Last edited by a moderator:

MaxHeadRoom

Joined Jul 18, 2013
28,698
Personally with PIC assy lang I prefer to compile in absolute, I kept getting annoying errors in Re-locatable.
Especially for a simple program such as that.
My personal preference now anyway.
Max.
 
Run it through MPLAB's simulator. Seems all it does it jump to start and put FF into Ports ABC and loops back to start. Doesn't use any of the other code.

Might want to consider C or Swordfish BASIC if you want to be able to relocate code.
 

upand_at_them

Joined May 15, 2010
940
Read what Bill pointed out. Your initialization code never executes, because the "goto start" jumps right over it. It should be like this:

Rich (BB code):
;******************************************************************************
;   This file is a basic template for creating relocatable assembly code for  *
;   a PIC18F2220. Copy this file into your project directory and modify or    *
;   add to it as needed.                                                      *
;                                                                             *
;   The PIC18FXXXX architecture allows two interrupt configurations. This     *
;   template code is written for priority interrupt levels and the IPEN bit   *
;   in the RCON register must be set to enable priority levels. If IPEN is    *
;   left in its default zero state, only the interrupt vector at 0x008 will   *
;   be used and the WREG_TEMP, BSR_TEMP and STATUS_TEMP variables will not    *
;   be needed.                                                                *
;                                                                             *
;   Refer to the MPASM User's Guide for additional information on the         *
;   features of the assembler and linker.                                     *
;                                                                             *
;   Refer to the PIC18F2221/2321/4221/4321 Data Sheet for additional          *
;   information on the architecture and instruction set.                      *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Filename:                                                                *
;    Date:                                                                    *
;    File Version:                                                            *
;                                                                             *
;    Author:                                                                  *
;    Company:                                                                 *
;                                                                             * 
;******************************************************************************
;                                                                             *
;    Files required: P18F2321.INC                                             *
;                                                                             *
;******************************************************************************

    LIST P=18F2321, F=INHX32 ;directive to define processor and file format
    #include <P18F2321.INC>     ;processor specific variable definitions

    errorlevel -302     ; suppress banksel warning messages during assembly
    errorlevel -311     ; suppress HIGH operator warning messages during assembly
    errorlevel -207      ; suppress labels warning
        
;******************************************************************************
;Configuration bits
;Microchip has changed the format for defining the configuration bits, please 
;see the .inc file for futher details on notation.  Below are a few examples.

    CONFIG WDT=off , BOR= off ,OSC = INTIO2 , FCMEN =  off ,  MCLRE = off  ,PBADEN = DIG  ,     CP0 = OFF , CP1 = OFF  
          ;internal osc i/o on ra6, ra7   ; RE3 input pin enabled; MCLR disabled   ; wdt off re1 internal set to V+ ; brown out off
          
          
          
    ; This code will start executing when a reset occurs.

    ORG 0x000  ; RESET VECTOR

    goto Start




;******************************************************************************
;Variable definitions
; These variables are only needed if low priority interrupts are used. 
; More variables may be needed to store other special function registers used
; in the interrupt routines.
                
    CBLOCK 0x20          ; user file mnames
     Temp
    HUNS
    TENS
    UNIITS
    d1
    d2     
    d3
    d4
    
    ENDC
;******************************************************************************
;EEPROM data
; Data to be programmed into the Data EEPROM is defined here

;DATA_EEPROM    CODE    0xf00000

        ;DE    "Test Data",0,1,2,3,4,5




;******************************************************************************
;High priority interrupt vector
; This code will start executing when a high priority interrupt occurs or
; when any interrupt occurs if interrupt priorities are not enabled.

;HI_INT_VECTOR    CODE
;    0x0008

    ;    bra    HighInt        ;go to high priority interrupt routine

;******************************************************************************
;Low priority interrupt vector
; This code will start executing when a low priority interrupt occurs.
; This code can be removed if low priority interrupts are not used.

;LOW_INT_VECTOR    CODE    0x0018

;        bra    LowInt        ;go to low priority interrupt routine

;******************************************************************************
;High priority interrupt routine
; The high priority interrupt code is placed here.



;******************************************************************************
;Low priority interrupt routine
; The low priority interrupt code is placed here.
; This code can be removed if low priority interrupts are not used.



;******************************************************************************
;Start of main program

Start

    Banksel OSCCON
    movlw b'01111111'        ; 8mhz clock internal
    movwf OSCCON
       
    CLRF PORTA 
    CLRF PORTB
    CLRF PORTC

    CLRF LATA 
    CLRF LATB
    CLRF LATC
    MOVLW  b'00001111'  ; Configure  A/D off vref internal to set A?D on AN0 inpt E0h
    MOVWF ADCON1        ; for digital inputs
    MOVWF b'00000111'        ; Turn comparators off
    MOVWF CMCON  
    MOVLW    .0 
    MOVWF TRISA   ; outputs
    MOVWF TRISB   ;outputs
    MOVWF TRISC   ; outputs
    clrf PORTA
    clrf PORTB
    clrf PORTC
    clrf PORTE   ; port E,0 as input I/O

    movlw .255
    movwf LATB
    movwf LATC
    movwf LATA
    
MainLoop
    ;
    goto MainLoop

    END
 

MaxHeadRoom

Joined Jul 18, 2013
28,698
I am using the 18F2221 right now so it should be close, I use this config as default.

config OSC = INTIO2, FCMEN = OFF, IESO = OFF ;1H
config PWRT = ON, BOR = ON, BORV = 2 ;2L
config WDTPS = 1, WDT = OFF ;2H
config STVREN = OFF, LVP = OFF, DEBUG = OFF ;4L
config MCLRE = OFF

Internal 4mhz clk.

movwf PORTx should work OK as long as you have cleared them as output previously with movwf TRISx with 0x00 in WREG
Also if you have a value in WREG or a register you can use the file to file.
e.g. movff WREG, PORTB etc.
I can try and run it through MPLAB and see if I spot something.
BTW you don't need the banksel macro in the 18f series.
Max.
 
Last edited:

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,304
Read what Bill pointed out. Your initialization code never executes, because the "goto start" jumps right over it. It should be like this:

Rich (BB code):
;******************************************************************************
;   This file is a basic template for creating relocatable assembly code for  *
;   a PIC18F2220. Copy this file into your project directory and modify or    *
;   add to it as needed.                                                      *
;                                                                             *
;   The PIC18FXXXX architecture allows two interrupt configurations. This     *
;   template code is written for priority interrupt levels and the IPEN bit   *
;   in the RCON register must be set to enable priority levels. If IPEN is    *
;   left in its default zero state, only the interrupt vector at 0x008 will   *
;   be used and the WREG_TEMP, BSR_TEMP and STATUS_TEMP variables will not    *
;   be needed.                                                                *
;                                                                             *
;   Refer to the MPASM User's Guide for additional information on the         *
;   features of the assembler and linker.                                     *
;                                                                             *
;   Refer to the PIC18F2221/2321/4221/4321 Data Sheet for additional          *
;   information on the architecture and instruction set.                      *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Filename:                                                                *
;    Date:                                                                    *
;    File Version:                                                            *
;                                                                             *
;    Author:                                                                  *
;    Company:                                                                 *
;                                                                             * 
;******************************************************************************
;                                                                             *
;    Files required: P18F2321.INC                                             *
;                                                                             *
;******************************************************************************

    LIST P=18F2321, F=INHX32 ;directive to define processor and file format
    #include <P18F2321.INC>     ;processor specific variable definitions

    errorlevel -302     ; suppress banksel warning messages during assembly
    errorlevel -311     ; suppress HIGH operator warning messages during assembly
    errorlevel -207      ; suppress labels warning
        
;******************************************************************************
;Configuration bits
;Microchip has changed the format for defining the configuration bits, please 
;see the .inc file for futher details on notation.  Below are a few examples.

    CONFIG WDT=off , BOR= off ,OSC = INTIO2 , FCMEN =  off ,  MCLRE = off  ,PBADEN = DIG  ,     CP0 = OFF , CP1 = OFF  
          ;internal osc i/o on ra6, ra7   ; RE3 input pin enabled; MCLR disabled   ; wdt off re1 internal set to V+ ; brown out off
          
          
          
    ; This code will start executing when a reset occurs.

    ORG 0x000  ; RESET VECTOR

    goto Start




;******************************************************************************
;Variable definitions
; These variables are only needed if low priority interrupts are used. 
; More variables may be needed to store other special function registers used
; in the interrupt routines.
                
    CBLOCK 0x20          ; user file mnames
     Temp
    HUNS
    TENS
    UNIITS
    d1
    d2     
    d3
    d4
    
    ENDC
;******************************************************************************
;EEPROM data
; Data to be programmed into the Data EEPROM is defined here

;DATA_EEPROM    CODE    0xf00000

        ;DE    "Test Data",0,1,2,3,4,5




;******************************************************************************
;High priority interrupt vector
; This code will start executing when a high priority interrupt occurs or
; when any interrupt occurs if interrupt priorities are not enabled.

;HI_INT_VECTOR    CODE
;    0x0008

    ;    bra    HighInt        ;go to high priority interrupt routine

;******************************************************************************
;Low priority interrupt vector
; This code will start executing when a low priority interrupt occurs.
; This code can be removed if low priority interrupts are not used.

;LOW_INT_VECTOR    CODE    0x0018

;        bra    LowInt        ;go to low priority interrupt routine

;******************************************************************************
;High priority interrupt routine
; The high priority interrupt code is placed here.



;******************************************************************************
;Low priority interrupt routine
; The low priority interrupt code is placed here.
; This code can be removed if low priority interrupts are not used.



;******************************************************************************
;Start of main program

Start

    Banksel OSCCON
    movlw b'01111111'        ; 8mhz clock internal
    movwf OSCCON
       
    CLRF PORTA 
    CLRF PORTB
    CLRF PORTC

    CLRF LATA 
    CLRF LATB
    CLRF LATC
    MOVLW  b'00001111'  ; Configure  A/D off vref internal to set A?D on AN0 inpt E0h
    MOVWF ADCON1        ; for digital inputs
    MOVWF b'00000111'        ; Turn comparators off
    MOVWF CMCON  
    MOVLW    .0 
    MOVWF TRISA   ; outputs
    MOVWF TRISB   ;outputs
    MOVWF TRISC   ; outputs
    clrf PORTA
    clrf PORTB
    clrf PORTC
    clrf PORTE   ; port E,0 as input I/O

    movlw .255
    movwf LATB
    movwf LATC
    movwf LATA
    
MainLoop
    ;
    goto MainLoop

    END


Done it this way and the leds come on then go off, but wont stay on??
 

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,304
I am using the 18F2221 right now so it should be close, I use this config as default.

config OSC = INTIO2, FCMEN = OFF, IESO = OFF ;1H
config PWRT = ON, BOR = ON, BORV = 2 ;2L
config WDTPS = 1, WDT = OFF ;2H
config STVREN = OFF, LVP = OFF, DEBUG = OFF ;4L
config MCLRE = OFF

Internal 4mhz clk.

movwf PORTx should work OK as long as you have cleared them as output previously with movwf TRISx with 0x00 in WREG
Also if you have a value in WREG or a register you can use the file to file.
e.g. movff WREG, PORTB etc.
I can try and run it through MPLAB and see if I spot something.
BTW you don't need the banksel macro in the 18f series.
Max.
So how do you set a bit on the ports then i normally use " bsf PORTB,4" but i have to use this LATB how?
 

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,304
i can get the leds to come on when its programmed , but if i power it down then power up again the led dont come on, there must be some setting thats missing , i have no idea whats wrong?
 

MaxHeadRoom

Joined Jul 18, 2013
28,698
If you have the master clear enabled, then you need a 10k resistor from MCLR to +5v pull up.
It is a good idea anyway.
First thing to try.
Max.
 

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,304
If you have the master clear enabled, then you need a 10k resistor from MCLR to +5v pull up.
It is a good idea anyway.
First thing to try.
Max.
its disabled, internal, also you said i dont need to use Banksel so what do you do to write to say the OSCCON ?
 

upand_at_them

Joined May 15, 2010
940
MaxHeadRoom said:
Use a movwf PORTB instead of movwf LATB etc.
Max.
No. Read the datasheet for why you should use LAT registers.

Dodgydave said:
any clue why my programme wont run, leds come on at first but on re-power go off?
Which pins are you connecting the LEDs to? Are the LEDs installed with the anode to the PIC pin and the cathode to ground, with a current limiting resistor?
 
Top