好吧,我有一些代码可以在 16F690 上完美运行,但我需要它在 16F18313 上运行。该代码的目的是检测箱盖何时打开和关闭,并打开风扇 25 秒。基本上,PIC 以睡眠模式启动,在中断时唤醒,检查盒盖是否打开和关闭,打开引脚 25 秒,然后清除中断并返回睡眠状态。我在这两个版本上都没有收到任何错误。当我在 16F18313 上运行代码时,RA0 始终保持高电平。
我包含了这两种设备的所有 C 代码。首先,这是 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;
}
答案是为 PORTA 启用数字 I/O。
并且您的代码清除更改中断的方式可能不起作用。
这段代码可能会更好:
/*
* File: SH_Fan_16F18313.c
* Author: marka
*
* Created on October 4, 2023, 10:39 AM
*
* PIC16F18313
* +---------:_:---------:
* 5v0 -> 1 : VDD VSS : 8 <- GND
* <> 2 : RA5/AN5 AN0/RA0 : 7 <> PGD
* <> 3 : RA4/AN4 AN1/RA1 : 6 <> PGC
* VPP -> 4 : RA3/MCLRn AN2/RA2 : 5 <>
* +---------------------+
* DIP-8
*/
#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) {
/*
* Add code to setup GPIO hardware
*/
ANSELA = 0; /* make all GPIO pins digital I/O DS40001799F-page 13 */
SLRCONA = 0; /* set output slew rate to maximum */
ODCONA = 0; /* set output as push-pull */
WPUA = 0; /* turn off weak pull-ups */
// 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
PORTA; /* Read PORTA to clear any mismatch for IOC */
IOCAF &= (~IOCAF); /* Clear IOC bits that changed */
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
PORTA; /* Read PORTA to clear any mismatch for IOC */
IOCAF &= (~IOCAF); /* Clear IOC bits that changed */
PIR0bits.IOCIF = 0;
}
}