assemblypicmplabpic18

I can't even turn on LED on PIC18F4550 using MPASM v5.87. What am I doing wrong?


I can't find any tutorials specific to PIC18F4550 using MPASM.

; PIC18F4550 Configuration Bit Settings

; Assembly source line config statements

#include "p18f4550.inc"

; CONFIG1L
  CONFIG  PLLDIV = 1            ; PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
  CONFIG  CPUDIV = OSC1_PLL2    ; System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
  CONFIG  USBDIV = 1            ; USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)

; CONFIG1H
  CONFIG  FOSC = INTOSC_XT      ; Oscillator Selection bits (Internal oscillator, XT used by USB (INTXT))
  CONFIG  FCMEN = OFF           ; Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
  CONFIG  IESO = OFF            ; Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

; CONFIG2L
  CONFIG  PWRT = OFF            ; Power-up Timer Enable bit (PWRT disabled)
  CONFIG  BOR = ON              ; Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
  CONFIG  BORV = 3              ; Brown-out Reset Voltage bits (Minimum setting 2.05V)
  CONFIG  VREGEN = OFF          ; USB Voltage Regulator Enable bit (USB voltage regulator disabled)

; CONFIG2H
  CONFIG  WDT = ON              ; Watchdog Timer Enable bit (WDT enabled)
  CONFIG  WDTPS = 32768         ; Watchdog Timer Postscale Select bits (1:32768)

; CONFIG3H
  CONFIG  CCP2MX = ON           ; CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
  CONFIG  PBADEN = ON           ; PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
  CONFIG  LPT1OSC = OFF         ; Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
  CONFIG  MCLRE = ON            ; MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

; CONFIG4L
  CONFIG  STVREN = ON           ; Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
  CONFIG  LVP = ON              ; Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
  CONFIG  ICPRT = OFF           ; Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
  CONFIG  XINST = OFF           ; Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

; CONFIG5L
  CONFIG  CP0 = OFF             ; Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
  CONFIG  CP1 = OFF             ; Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
  CONFIG  CP2 = OFF             ; Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
  CONFIG  CP3 = OFF             ; Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)

; CONFIG5H
  CONFIG  CPB = OFF             ; Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
  CONFIG  CPD = OFF             ; Data EEPROM Code Protection bit (Data EEPROM is not code-protected)

; CONFIG6L
  CONFIG  WRT0 = OFF            ; Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
  CONFIG  WRT1 = OFF            ; Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
  CONFIG  WRT2 = OFF            ; Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
  CONFIG  WRT3 = OFF            ; Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)

; CONFIG6H
  CONFIG  WRTC = OFF            ; Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
  CONFIG  WRTB = OFF            ; Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
  CONFIG  WRTD = OFF            ; Data EEPROM Write Protection bit (Data EEPROM is not write-protected)

; CONFIG7L
  CONFIG  EBTR0 = OFF           ; Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
  CONFIG  EBTR1 = OFF           ; Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
  CONFIG  EBTR2 = OFF           ; Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
  CONFIG  EBTR3 = OFF           ; Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)

; CONFIG7H
  CONFIG  EBTRB = OFF           ; Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)

;    __CONFIG _WDT_OFF & _CP_OFF & _MCLRE_OFF
    ORG 0x0000
INIT ; We are initializing the microcontroller over the next three lines.
    MOVLW ~(1 << TRISA0)  ;these two lines set GP1 as an output
    MOVWF TRISA
    BSF TRISA, TRISA0 ;this line is where we set GP1 output high to light the LED
LOOP 
    GOTO LOOP                          ; loop forever
END ; Needed to end the program.

I've copied the main code from this tutorial, https://www.circuitbread.com/tutorials/the-first-assembly-program-part-5-simple-microcontroller-pic10f200. It builds fine but I get The following:

memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x7f
configuration memory
program memory
Address: 0 Expected Value: fe Received Value: 0
Failed to program device

I suspect my syntax is wrong somehow, or am I doing something wrong completely. The data sheet is too technical for me to understand. Logic should be, move literal into working register, move working register to file register trisa, set bit to high so it outputs?


Solution

  • After looking at code I know works and rereading the data sheet I think I understand now. I believe I've been using BSF wrong, it's BSF TRISA, 1 I think rather than BSF TRISA, TRISA1. And I should be using Data Latch Register

    BSF Bit Set f
    Syntax: BSF f, b {,a}
    Operands: 0 ≤ f ≤ 255
    0 ≤ b ≤ 7
    a ∈ [0,1]
    Operation: 1 → f<b>
    Status Affected: None
    Encoding: 1000 bbba ffff ffff
    Description: Bit ‘b’ in register ‘f’ is set. 
    If ‘a’ is ‘0’, the Access Bank is selected. 
    If ‘a’ is ‘1’, the BSR is used to select the 
    GPR bank (default). 
    If ‘a’ is ‘0’ and the extended instruction 
    set is enabled, this instruction operates 
    in Indexed Literal Offset Addressing 
    mode whenever f ≤ 95 (5Fh). See 
    Section 26.2.3 “Byte-Oriented and 
    Bit-Oriented Instructions in Indexed 
    Literal Offset Mode” for details.
    Words: 1
    Cycles: 1
    Q Cycle Activity:
    Q1 Q2 Q3 Q4
    Decode Read
    register ‘f’
    Process 
    Data
    Write
    register ‘f’
    Example: BSF FLAG_REG, 7, 1
    Before Instruction
    FLAG_REG = 0Ah
    After Instruction
    FLAG_REG = 8Ah`
    
    `CLRF PORTD ; Initialize PORTD by
    ; clearing output
    ; data latches
    CLRF LATD ; Alternate method
    ; to clear output
    ; data latches
    MOVLW 0CFh ; Value used to 
    ; initialize data 
    ; direction
    MOVWF TRISD ; Set RD<3:0> as inputs
    ; RD<5:4> as outputs
    ; RD<7:6> as inputs`
    
    `CLRF PORTA ; Initialize PORTA by
    ; clearing output
    ; data latches
    CLRF LATA ; Alternate method
    ; to clear output
    ; data latches
    MOVLW 0Fh ; Configure A/D 
    MOVWF ADCON1 ; for digital inputs
    MOVLW 07h ; Configure comparators
    MOVWF CMCON ; for digital input
    MOVLW 0CFh ; Value used to 
    ; initialize data 
    ; direction
    MOVWF TRISA ; Set RA<3:0> as inputs
    ; RA<5:4> as outputs`