基于MPLAB X与XC8:16F690代码正常16F18313 RA0持续高电平
PIC16F18313移植后RA0始终高电平的问题排查与修复
将运行于PIC16F690的盒盖开合检测+风扇控制代码移植到PIC16F18313后,编译无错误,但RA0引脚始终保持高电平,无法正常实现"休眠唤醒-检测开合-风扇运行25秒-回到休眠"的逻辑。
16F690上可正常运行的代码
/* * File: SH_Fan_16F690.c * Author: marka * * Created on October 4, 2023, 10:39 AM */ #include <xc.h> #include <stdint.h> #include <stdbool.h> // Configuration bits #pragma config FOSC = INTRCIO, WDTE = OFF, PWRTE = OFF, MCLRE = OFF, CP = OFF, CPD = OFF, BOREN = OFF, IESO = OFF, FCMEN = OFF #define _XTAL_FREQ 4000000UL //4MHz #define FAN_PIN PORTCbits.RC1 #define SENSOR_PIN PORTAbits.RA3 static uint8_t previous_state = 1; static uint8_t current_state = 1; static uint16_t timer = 0; void __interrupt() myISR(void); void main(void) { // Setup // Configure RC1 as output for the fan and RA3 as input for the sensor TRISC1 = 0; TRISA3 = 1; // Initialize fan pin to low FAN_PIN = 0; // Enable GPIO Change Interrupt on RA3 IOCA3 = 1; INTCONbits.RABIE = 1; // Enable Port A and B interrupt INTCONbits.PEIE = 1; // Enable peripheral interrupt // Clear any stray interrupt flags INTCONbits.RABIF = 0; // Enable global interrupts INTCONbits.GIE = 1; // Main loop while (1) { // Put the device to sleep. It will wake up on GPIO change. asm("SLEEP"); } } void __interrupt() myISR(void) { // Disable interrupts temporarily INTCONbits.GIE = 0; // Check if the interrupt is due to a GPIO change if (INTCONbits.RABIF) { // Debouncing logic: wait 20 ms before re-reading the sensor state __delay_ms(20); // Read the current state of the sensor current_state = SENSOR_PIN; // Check state transition if ((previous_state == 0) && (current_state == 1)) { // Box was open, now closed // Turn on the fan for 25 seconds FAN_PIN = 1; // Wait 25 seconds for (timer = 1; timer <= 25000; ++timer) { __delay_ms(1); } // Turn off the fan FAN_PIN = 0; } // Update the previous state previous_state = current_state; // Clear the interrupt flag INTCONbits.RABIF = 0; } // Re-enable interrupts INTCONbits.GIE = 1; }
16F18313移植后的问题代码
/* * File: SH_Fan_16F18313.c * Author: marka * * Created on October 4, 2023, 10:39 AM */ #include <xc.h> #include <stdint.h> #include <stdbool.h> // 'C' source line config statements // CONFIG1 #pragma config FEXTOSC = ECH // FEXTOSC External Oscillator mode Selection bits (EC (external clock) above 8 MHz) #pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with 2x PLL (32MHz)) #pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; I/O or oscillator function on OSC2) #pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed) #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled) // CONFIG2 #pragma config MCLRE = OFF // Master Clear Enable bit (MCLR/VPP pin function is MCLR; Weak pull-up enabled ) #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config WDTE = OFF // Watchdog Timer Enable bits (WDT enabled, SWDTEN is ignored) #pragma config LPBOREN = OFF // Low-power BOR enable bit (ULPBOR disabled) #pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out Reset enabled, SBOREN bit ignored) #pragma config BORV = LOW // Brown-out Reset Voltage selection bit (Brown-out voltage (Vbor) set to 2.45V) #pragma config PPS1WAY = OFF // PPSLOCK bit One-Way Set Enable bit (The PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle) #pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a Reset) #pragma config DEBUG = OFF // Debugger enable bit (Background debugger disabled) // CONFIG3 #pragma config WRT = OFF // User NVM self-write protection bits (Write protection off) #pragma config LVP = OFF // Low Voltage Programming Enable bit (Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored.) // CONFIG4 #pragma config CP = OFF // User NVM Program Memory Code Protection bit (User NVM code protection disabled) #pragma config CPD = OFF // Data NVM Memory Code Protection bit (Data NVM code protection disabled) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #define _XTAL_FREQ 4000000UL // 4MHz #define FAN_PIN LATAbits.LATA0 // Fan connected to RA0 #define SENSOR_PIN PORTAbits.RA2 // Sensor connected to RA2 static uint8_t previous_state = 1; static uint8_t current_state = 1; static uint16_t timer = 0; void __interrupt() myISR(void); void main(void) { // Setup TRISAbits.TRISA0 = 0; // RA0 as output for the fan TRISAbits.TRISA2 = 1; // RA2 as input for the sensor // Initialize fan pin to low FAN_PIN = 0; // Enable GPIO Change Interrupt on RA2 IOCAPbits.IOCAP2 = 1; PIE0bits.IOCIE = 1; // Enable Interrupt-On-Change INTCONbits.PEIE = 1; // Enable peripheral interrupt // Clear any stray interrupt flags PIR0bits.IOCIF = 0; // Enable global interrupts INTCONbits.GIE = 1; // Main loop while (1) { // Sleep mode asm("SLEEP"); } } void __interrupt() myISR(void) { // Disable interrupts temporarily INTCONbits.GIE = 0; // Check if the interrupt is due to a GPIO change if (PIR0bits.IOCIF) { // Debouncing: wait 20 ms before re-reading the sensor state __delay_ms(20); // Read the current state of the sensor current_state = SENSOR_PIN; // Check state transition if ((previous_state == 0) && (current_state == 1)) { // Box was open, now closed FAN_PIN = 1; // Turn on the fan for 25 seconds // Wait 25 seconds for (timer = 1; timer <= 25000; ++timer) { __delay_ms(1); } // Turn off the fan FAN_PIN = 0; } // Update the previous state previous_state = current_state; // Clear the interrupt flag PIR0bits.IOCIF = 0; } // Re-enable interrupts INTCONbits.GIE = 1; }
问题根源分析
- 时钟配置不匹配:
_XTAL_FREQ定义为4MHz,但CONFIG1中RSTOSC=HFINT32对应32MHz时钟,导致__delay_ms()计算完全错误,延时逻辑失效,引发中断内流程异常。 - IOC中断配置不全:仅开启RA2的上升沿中断,未开启下降沿中断,无法检测盒盖打开动作,导致
previous_state无法正确更新,可能误触发风扇开启逻辑。 - 中断标志清除错误:16F18313的IOC中断要求清除
IOCIF前必须先读取PORT寄存器,否则标志无法有效清除,导致中断反复触发,风扇持续被置高。 - 弱上拉未处理:RA2默认弱上拉开启,若传感器为低电平触发,会导致初始电平状态异常。
修复方案与修改后代码
修改后的16F18313代码
/* * File: SH_Fan_16F18313.c * Author: marka * * Created on October 4, 2023, 10:39 AM */ #include <xc.h> #include <stdint.h> #include <stdbool.h> // CONFIG1 #pragma config FEXTOSC = ECH // FEXTOSC External Oscillator mode Selection bits (EC (external clock) above 8 MHz) #pragma config RSTOSC = HFINT8 // Power-up default value for COSC bits (HFINTOSC (8MHz)) #pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; I/O or oscillator function on OSC2) #pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed) #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled) // CONFIG2 #pragma config MCLRE = OFF // Master Clear Enable bit (MCLR/VPP pin function is MCLR; Weak pull-up enabled ) #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config WDTE = OFF // Watchdog Timer Enable bits (WDT disabled) #pragma config LPBOREN = OFF // Low-power BOR enable bit (ULPBOR disabled) #pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled) #pragma config BORV = LOW // Brown-out Reset Voltage selection bit (Brown-out voltage (Vbor) set to 2.45V) #pragma config PPS1WAY = OFF // PPSLOCK bit One-Way Set Enable bit (The PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle) #pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a Reset) #pragma config DEBUG = OFF // Debugger enable bit (Background debugger disabled) // CONFIG3 #pragma config WRT = OFF // User NVM self-write protection bits (Write protection off) #pragma config LVP = OFF // Low Voltage Programming Enable bit (Low voltage programming disabled) // CONFIG4 #pragma config CP = OFF // User NVM Program Memory Code Protection bit (User NVM code protection disabled) #pragma config CPD = OFF // Data NVM Memory Code Protection bit (Data NVM code protection disabled) #define _XTAL_FREQ 8000000UL // 匹配8MHz内部时钟 #define FAN_PIN LATAbits.LATA0 // Fan connected to RA0 #define SENSOR_PIN PORTAbits.RA2 // Sensor connected to RA2 static uint8_t previous_state = 1; static uint8_t current_state = 1; static uint8_t timer = 0; void __interrupt() myISR(void); void main(void) { // Setup TRISAbits.TRISA0 = 0; // RA0 as output for the fan TRISAbits.TRISA2 = 1; // RA2 as input for the sensor // Disable weak pull-up on RA2 (适配低电平触发传感器) WPUEbits.WPUE2 = 0; // Initialize fan pin to low FAN_PIN = 0; // Enable GPIO Change Interrupt on RA2 (上升沿+下降沿) IOCAPbits.IOCAP2 = 1; IOCANbits.IOCAN2 = 1; PIE0bits.IOCIE = 1; // Enable Interrupt-On-Change INTCONbits.PEIE = 1; // Enable peripheral interrupt // 清除残留中断标志:先读PORT再清标志 uint8_t dummy = PORTA; PIR0bits.IOCIF = 0; // Enable global interrupts INTCONbits.GIE = 1; // Main loop while (1) { // Sleep mode asm("SLEEP"); } } void __interrupt() myISR(void) { // Disable interrupts temporarily INTCONbits.GIE = 0; // Check if the interrupt is due to a GPIO change if (PIR0bits.IOCIF) { // Debouncing: wait 20 ms before re-reading the sensor state __delay_ms(20); // Read the current state of the sensor current_state = SENSOR_PIN; // Check state transition (盒盖从打开到关闭) if ((previous_state == 0) && (current_state == 1)) { // Turn on the fan FAN_PIN = 1; // Wait 25 seconds (用1秒延时替代循环,提升效率) for (timer = 0; timer < 25; ++timer) { __delay_ms(1000); } // Turn off the fan FAN_PIN = 0; } // Update the previous state previous_state = current_state; // 正确清除IOC中断标志:先读PORT再清标志 uint8_t dummy = PORTA; PIR0bits.IOCIF = 0; } // Re-enable interrupts INTCONbits.GIE = 1; }
内容的提问来源于stack exchange,提问作者mokeymark
相关产品推荐
相关产品推荐

