You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用MPASM v5.87无法点亮PIC18F4550的LED,编程失败求助

PIC18F4550 MPASM编程失败问题排查

找不到PIC18F4550的MPASM教程,参考PIC10F200的教程编写了如下汇编代码,编译正常但编程设备时报错,原逻辑是配置TRISA0为输出并置高电平点亮LED,需要排查问题。

编写的代码

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

编程报错信息

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

问题排查与修正建议

1. 配置位冲突问题

代码中存在注释掉的__CONFIG行,与上方的CONFIG语句重复,MPASM处理时可能出现异常,删除注释掉的__CONFIG行,确保配置位定义唯一。

2. TRISA/PORTA操作逻辑错误

PIC18F系列中:

  • TRISA寄存器的位:置位(BSF)=输入,清零(BCF)=输出
  • 输出高电平需要操作的是PORTA寄存器,而非TRISA

原代码中,先通过MOVLW ~(1 << TRISA0)将TRISA0位清零(设为输出)写入TRISA,紧接着又BSF TRISA, TRISA0把该位设为输入,逻辑完全矛盾,且错误地操作TRISA来试图输出高电平。

修正后的初始化代码:

ORG 0x0000
INIT
    MOVLW 0xFF          ; 初始化PORTA所有引脚为输入(默认状态)
    MOVWF TRISA
    BCF TRISA, TRISA0   ; 将TRISA0位清零,配置为输出
    BSF PORTA, PORTA0   ; 置位PORTA0,输出高电平点亮LED
LOOP
    GOTO LOOP
END

3. 硬件与编程器连接检查

报错显示地址0的预期值与实际值不匹配,需排查:

  • 编程器与目标板的接线(MCLR、VDD、VSS、PGD、PGCK)是否牢固无虚接
  • 目标板电源是否稳定,电压符合PIC18F4550的要求(3.3V或5V)
  • MCLR引脚状态:因CONFIG中MCLRE=ON,需确保MCLR接高电平(通过上拉电阻或编程器提供)

4. 配置位优化(调试阶段)

当前CONFIG中WDT=ON,调试阶段建议关闭看门狗,避免意外复位:

; CONFIG2H
CONFIG  WDT = OFF              ; Watchdog Timer Enable bit (WDT disabled)

另外检查振荡器配置FOSC=INTOSC_XT是否与硬件匹配:如果未接外部XT晶振,应改为内部振荡器模式(如FOSC=INTOSC_HS)。

内容的提问来源于stack exchange,提问作者noobyme

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 04:50:23