pic18f2321 wont run

Discussion in 'Embedded Systems and Microcontrollers' started by Dodgydave, Jul 20, 2013.

  1. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,986
    745
    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?

    Code ( (Unknown Language)):
    1.  
    2. ;******************************************************************************
    3. ;   This file is a basic template for creating relocatable assembly code for  *
    4. ;   a PIC18F2220. Copy this file into your project directory and modify or    *
    5. ;   add to it as needed.                                                      *
    6. ;                                                                             *
    7. ;   The PIC18FXXXX architecture allows two interrupt configurations. This     *
    8. ;   template code is written for priority interrupt levels and the IPEN bit   *
    9. ;   in the RCON register must be set to enable priority levels. If IPEN is    *
    10. ;   left in its default zero state, only the interrupt vector at 0x008 will   *
    11. ;   be used and the WREG_TEMP, BSR_TEMP and STATUS_TEMP variables will not    *
    12. ;   be needed.                                                                *
    13. ;                                                                             *
    14. ;   Refer to the MPASM User's Guide for additional information on the         *
    15. ;   features of the assembler and linker.                                     *
    16. ;                                                                             *
    17. ;   Refer to the PIC18F2221/2321/4221/4321 Data Sheet for additional          *
    18. ;   information on the architecture and instruction set.                      *
    19. ;                                                                             *
    20. ;******************************************************************************
    21. ;                                                                             *
    22. ;    Filename:                                                                *
    23. ;    Date:                                                                    *
    24. ;    File Version:                                                            *
    25. ;                                                                             *
    26. ;    Author:                                                                  *
    27. ;    Company:                                                                 *
    28. ;                                                                             *
    29. ;******************************************************************************
    30. ;                                                                             *
    31. ;    Files required: P18F2321.INC                                             *
    32. ;                                                                             *
    33. ;******************************************************************************
    34.  
    35.     LIST P=18F2321, F=INHX32 ;directive to define processor and file format
    36.     #include <P18F2321.INC>  ;processor specific variable definitions
    37.  
    38. ;******************************************************************************
    39. ;Configuration bits
    40. ;Microchip has changed the format for defining the configuration bits, please
    41. ;see the .inc file for futher details on notation.  Below are a few examples.
    42.  
    43.     ; This code will start executing when a reset occurs.
    44.  
    45.     ORG 0x000  ; RESET VECTOR
    46.  
    47.     goto Start
    48.  
    49.                
    50.     CONFIG WDT=off , BOR= off ,OSC = INTIO2 , FCMEN =  off ,  MCLRE = off  ,PBADEN = DIG  ,  CP0 = OFF , CP1 = OFF  
    51.           ;internal osc i/o on ra6, ra7   ; RE3 input pin enabled; MCLR disabled   ; wdt off re1 internal set to V+ ; brown out off
    52.     Banksel OSCCON
    53.     movlw b'01111111'        ; 8mhz clock internal
    54.     movwf OSCCON
    55.      
    56.     CLRF PORTA
    57.     CLRF PORTB
    58.     CLRF PORTC
    59.  
    60.     CLRF LATA
    61.     CLRF LATB
    62.     CLRF LATC
    63.     MOVLW  b'00001111'  ; Configure  A/D off vref internal to set A?D on AN0 inpt E0h
    64.     MOVWF ADCON1        ; for digital inputs
    65.     MOVWF b'00000111'        ; Turn comparators off
    66.     MOVWF CMCON  
    67.     MOVLW   .0
    68.     MOVWF TRISA   ; outputs
    69.     MOVWF TRISB   ;outputs
    70.     MOVWF TRISC   ; outputs
    71.     clrf PORTA
    72.     clrf PORTB
    73.     clrf PORTC
    74.     clrf PORTE   ; port E,0 as input I/O
    75.         errorlevel -302     ; suppress banksel warning messages during assembly
    76.         errorlevel -311     ; suppress HIGH operator warning messages during assembly
    77.         errorlevel -207      ; suppress labels warning
    78.  
    79.  
    80.  
    81.  
    82.  
    83. ;******************************************************************************
    84. ;Variable definitions
    85. ; These variables are only needed if low priority interrupts are used.
    86. ; More variables may be needed to store other special function registers used
    87. ; in the interrupt routines.
    88.                
    89.     CBLOCK 0x20          ; user file mnames
    90.      Temp
    91.     HUNS
    92.     TENS
    93.     UNIITS
    94.     d1
    95.     d2  
    96.     d3
    97.     d4
    98.    
    99.     ENDC
    100. ;******************************************************************************
    101. ;EEPROM data
    102. ; Data to be programmed into the Data EEPROM is defined here
    103.  
    104. ;DATA_EEPROM    CODE    0xf00000
    105.  
    106.         ;DE "Test Data",0,1,2,3,4,5
    107.  
    108.  
    109.  
    110.  
    111. ;******************************************************************************
    112. ;High priority interrupt vector
    113. ; This code will start executing when a high priority interrupt occurs or
    114. ; when any interrupt occurs if interrupt priorities are not enabled.
    115.  
    116. ;HI_INT_VECTOR  CODE
    117. ;   0x0008
    118.  
    119.     ;   bra HighInt     ;go to high priority interrupt routine
    120.  
    121. ;******************************************************************************
    122. ;Low priority interrupt vector
    123. ; This code will start executing when a low priority interrupt occurs.
    124. ; This code can be removed if low priority interrupts are not used.
    125.  
    126. ;LOW_INT_VECTOR CODE    0x0018
    127.  
    128. ;       bra LowInt      ;go to low priority interrupt routine
    129.  
    130. ;******************************************************************************
    131. ;High priority interrupt routine
    132. ; The high priority interrupt code is placed here.
    133.  
    134.  
    135.  
    136. ;******************************************************************************
    137. ;Low priority interrupt routine
    138. ; The low priority interrupt code is placed here.
    139. ; This code can be removed if low priority interrupts are not used.
    140.  
    141.  
    142.  
    143. ;******************************************************************************
    144. ;Start of main program
    145.  
    146. Start
    147.  
    148.  
    149.    
    150.     movlw .255
    151.     movwf PORTB
    152.     movwf PORTC
    153.     movwf PORTA
    154.     goto Start
    155.  
    156.  
    157.  
    158.  
    159.  
    160.    
    161.  
    162.         END
    163.  
     
    Last edited by a moderator: Jul 20, 2013
  2. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    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.
     
  3. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    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.
     
  4. upand_at_them

    Active Member

    May 15, 2010
    246
    29
    Aren't you supposed to write to the latches only, instead of the ports?
     
  5. upand_at_them

    Active Member

    May 15, 2010
    246
    29
    Good catch. The pins are never configured to be outputs.
     
  6. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,986
    745
    I don't know i have never used this chip, normally i use a 16f690, so how do i get it working?
     
  7. upand_at_them

    Active Member

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

    Code ( (Unknown Language)):
    1. ;******************************************************************************
    2. ;   This file is a basic template for creating relocatable assembly code for  *
    3. ;   a PIC18F2220. Copy this file into your project directory and modify or    *
    4. ;   add to it as needed.                                                      *
    5. ;                                                                             *
    6. ;   The PIC18FXXXX architecture allows two interrupt configurations. This     *
    7. ;   template code is written for priority interrupt levels and the IPEN bit   *
    8. ;   in the RCON register must be set to enable priority levels. If IPEN is    *
    9. ;   left in its default zero state, only the interrupt vector at 0x008 will   *
    10. ;   be used and the WREG_TEMP, BSR_TEMP and STATUS_TEMP variables will not    *
    11. ;   be needed.                                                                *
    12. ;                                                                             *
    13. ;   Refer to the MPASM User's Guide for additional information on the         *
    14. ;   features of the assembler and linker.                                     *
    15. ;                                                                             *
    16. ;   Refer to the PIC18F2221/2321/4221/4321 Data Sheet for additional          *
    17. ;   information on the architecture and instruction set.                      *
    18. ;                                                                             *
    19. ;******************************************************************************
    20. ;                                                                             *
    21. ;    Filename:                                                                *
    22. ;    Date:                                                                    *
    23. ;    File Version:                                                            *
    24. ;                                                                             *
    25. ;    Author:                                                                  *
    26. ;    Company:                                                                 *
    27. ;                                                                             *
    28. ;******************************************************************************
    29. ;                                                                             *
    30. ;    Files required: P18F2321.INC                                             *
    31. ;                                                                             *
    32. ;******************************************************************************
    33.  
    34.     LIST P=18F2321, F=INHX32 ;directive to define processor and file format
    35.     #include <P18F2321.INC>     ;processor specific variable definitions
    36.  
    37.     errorlevel -302     ; suppress banksel warning messages during assembly
    38.     errorlevel -311     ; suppress HIGH operator warning messages during assembly
    39.     errorlevel -207      ; suppress labels warning
    40.        
    41. ;******************************************************************************
    42. ;Configuration bits
    43. ;Microchip has changed the format for defining the configuration bits, please
    44. ;see the .inc file for futher details on notation.  Below are a few examples.
    45.  
    46.     CONFIG WDT=off , BOR= off ,OSC = INTIO2 , FCMEN =  off ,  MCLRE = off  ,PBADEN = DIG  ,     CP0 = OFF , CP1 = OFF  
    47.           ;internal osc i/o on ra6, ra7   ; RE3 input pin enabled; MCLR disabled   ; wdt off re1 internal set to V+ ; brown out off
    48.          
    49.          
    50.          
    51.     ; This code will start executing when a reset occurs.
    52.  
    53.     ORG 0x000  ; RESET VECTOR
    54.  
    55.     goto Start
    56.  
    57.  
    58.  
    59.  
    60. ;******************************************************************************
    61. ;Variable definitions
    62. ; These variables are only needed if low priority interrupts are used.
    63. ; More variables may be needed to store other special function registers used
    64. ; in the interrupt routines.
    65.                
    66.     CBLOCK 0x20          ; user file mnames
    67.      Temp
    68.     HUNS
    69.     TENS
    70.     UNIITS
    71.     d1
    72.     d2    
    73.     d3
    74.     d4
    75.    
    76.     ENDC
    77. ;******************************************************************************
    78. ;EEPROM data
    79. ; Data to be programmed into the Data EEPROM is defined here
    80.  
    81. ;DATA_EEPROM    CODE    0xf00000
    82.  
    83.         ;DE    "Test Data",0,1,2,3,4,5
    84.  
    85.  
    86.  
    87.  
    88. ;******************************************************************************
    89. ;High priority interrupt vector
    90. ; This code will start executing when a high priority interrupt occurs or
    91. ; when any interrupt occurs if interrupt priorities are not enabled.
    92.  
    93. ;HI_INT_VECTOR    CODE
    94. ;    0x0008
    95.  
    96.     ;    bra    HighInt        ;go to high priority interrupt routine
    97.  
    98. ;******************************************************************************
    99. ;Low priority interrupt vector
    100. ; This code will start executing when a low priority interrupt occurs.
    101. ; This code can be removed if low priority interrupts are not used.
    102.  
    103. ;LOW_INT_VECTOR    CODE    0x0018
    104.  
    105. ;        bra    LowInt        ;go to low priority interrupt routine
    106.  
    107. ;******************************************************************************
    108. ;High priority interrupt routine
    109. ; The high priority interrupt code is placed here.
    110.  
    111.  
    112.  
    113. ;******************************************************************************
    114. ;Low priority interrupt routine
    115. ; The low priority interrupt code is placed here.
    116. ; This code can be removed if low priority interrupts are not used.
    117.  
    118.  
    119.  
    120. ;******************************************************************************
    121. ;Start of main program
    122.  
    123. Start
    124.  
    125.     Banksel OSCCON
    126.     movlw b'01111111'        ; 8mhz clock internal
    127.     movwf OSCCON
    128.        
    129.     CLRF PORTA
    130.     CLRF PORTB
    131.     CLRF PORTC
    132.  
    133.     CLRF LATA
    134.     CLRF LATB
    135.     CLRF LATC
    136.     MOVLW  b'00001111'  ; Configure  A/D off vref internal to set A?D on AN0 inpt E0h
    137.     MOVWF ADCON1        ; for digital inputs
    138.     MOVWF b'00000111'        ; Turn comparators off
    139.     MOVWF CMCON  
    140.     MOVLW    .0
    141.     MOVWF TRISA   ; outputs
    142.     MOVWF TRISB   ;outputs
    143.     MOVWF TRISC   ; outputs
    144.     clrf PORTA
    145.     clrf PORTB
    146.     clrf PORTC
    147.     clrf PORTE   ; port E,0 as input I/O
    148.  
    149.     movlw .255
    150.     movwf LATB
    151.     movwf LATC
    152.     movwf LATA
    153.    
    154. MainLoop
    155.     ;
    156.     goto MainLoop
    157.  
    158.     END
     
    Dodgydave likes this.
  8. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    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: Jul 20, 2013
    Dodgydave likes this.
  9. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,986
    745


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

    Expert

    Jul 18, 2013
    10,542
    2,369
    Use a movwf PORTB instead of movwf LATB etc.
    Max.
     
  11. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,986
    745
    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?
     
  12. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    I rarely if ever use LATx, to set an individual output bit it would be bsf PORTB, 2 etc as you have done.
    Max.
     
  13. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,986
    745
    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?
     
  14. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    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.
     
  15. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,986
    745
    its disabled, internal, also you said i dont need to use Banksel so what do you do to write to say the OSCCON ?
     
  16. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    763
    Read about "read modify write" and you could understand why you should.
     
  17. upand_at_them

    Active Member

    May 15, 2010
    246
    29
    No. Read the datasheet for why you should use LAT registers.

    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?
     
  18. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    763
    Because that micro was "binned and swapped" (your words)? :D :D :D
     
    Dodgydave likes this.
Loading...