Okay, I have some code that works perfectly on a 16F690, but I need it to work on a 16F18313. The purpose of the code is to detect when a box lid is opened and closed, and turn on a fan for 25 seconds. Basically, the PIC starts in a sleep mode, wakes on interrupt, checks that the box lid was opened and closed, turns on a pin for 25 seconds, then clears the interrupts and goes back to sleep. I don't get any errors on either version. When I run the code on the 16F18313, RA0 just stays high all the time.
I'm including all the C codes for both devices. First, here is the working code on the 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;
}
Here is the code on the 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;
}
The answer is to enable digital I/O for PORTA.
And the way your code clears the Interrupt-On-Change may not work.
This code may be better: