由中断服务例程切换的 volatile 变量的更改未反射(reflect)在 main() 中

标签 c interrupt volatile

我有一个真正的难题,我已经被困了一段时间了。我正在用 C 语言对 PIC16F15376 微 Controller Xpress 板进行编程。

我将一个名为 buttonIntention 的 volatile 变量初始化为 0。当中断服务例程发生时,buttonSet(1) 被调用,有效设置 buttonIntention = 1。 [注意:我也尝试过直接切换 buttonIntention 变量,而不使用 get 或 set 函数。] 在 main() 中,while(1) 不断轮询检查 ISR 是否已关闭(这将设置 buttonIntention = 1),如果 ISR 已关闭,则将 TESTPIN 设置为高电平在我的示波器上查看它。

我已经确定,当我按下按钮时,ISR 确实会触发。然后在 ISR 中将 buttonIntention 设置为 1,因为我尝试的 ISR 末尾注释掉的代码工作正常。

然而,我的main()中的while(1)中的if(buttonIntention)实际上从未看到buttonIntention设置为1。我我认为将此变量设置为 volatile 可以解决此问题,因为它在 ISR 和 main() 之间共享,并且我认为使用 uint8_t 类型在我的 8 位微 Controller 上可以解决我读过但不太理解的任何“原子”问题...但是,仍然存在问题,if 语句永远看不到 1。

有什么想法吗?

这是我的代码:

// CONFIG1
#pragma config FEXTOSC = OFF    // External Oscillator mode selection bits->Oscillator not enabled
#pragma config RSTOSC = HFINT32    // Power-up default value for COSC bits->HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1
#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 bit->FSCM timer enabled

// CONFIG2
#pragma config MCLRE = ON    // Master Clear Enable bit->MCLR pin is Master Clear function
#pragma config PWRTE = OFF    // Power-up Timer Enable bit->PWRT disabled
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit->ULPBOR disabled
#pragma config BOREN = ON    // Brown-out reset enable bits->Brown-out Reset Enabled, SBOREN bit is ignored
#pragma config BORV = LO    // Brown-out Reset Voltage Selection->Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices
#pragma config ZCD = ON    // Zero-cross detect disable->Zero-cross detect circuit is disabled at POR.
#pragma config PPS1WAY = ON    // Peripheral Pin Select one-way control->The PPSLOCK bit can be cleared and set only once in software
#pragma config STVREN = ON    // Stack Overflow/Underflow Reset Enable bit->Stack Overflow or Underflow will cause a reset

// CONFIG3
#pragma config WDTCPS = WDTCPS_31    // WDT Period Select bits->Divider ratio 1:65536; software control of WDTPS
#pragma config WDTE = OFF    // WDT operating mode->WDT Disabled, SWDTEN is ignored
#pragma config WDTCWS = WDTCWS_7    // WDT Window Select bits->window always open (100%); software control; keyed access not required
#pragma config WDTCCS = SC    // WDT input clock selector->Software Control

// CONFIG4
#pragma config WRTC = OFF    // UserNVM self-write protection bits->Write protection off
//#pragma config SCANE = available    // Scanner Enable bit->Scanner module is available for use
#pragma config LVP = ON    // Low Voltage Programming Enable bit->Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.

// CONFIG5
#pragma config CP = OFF    // UserNVM Program memory code protection bit->UserNVM code protection disabled
//#pragma config CPD = OFF    // DataNVM code protection bit->DataNVM code protection disabled

// ==========================================================================
// Import Header Files
// ==========================================================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xc.h>
#include <math.h>

// ==========================================================================
// Define statements
// ==========================================================================

// Inputs from buttons
#define startButton         PORTCbits.RC4
#define profileButton       PORTCbits.RC5
#define incrementButton     PORTCbits.RC6
#define decrementButton     PORTCbits.RC7

#define TESTPIN LATCbits.LATC0

// ==========================================================================
// Global Variables
// ==========================================================================

// ISR Toggle Variables (MUST be declared volatile)
volatile uint8_t buttonIntention = 0;

// ==========================================================================
// General Configuration
// ==========================================================================

// --- --- --- --- --- --- ---
// Configure Inputs and Outputs
// --- --- --- --- --- --- ---
void config_IO(void) {
    // --- Set output for 5 gate drivers and LCD rs and en: ---
    TRISA = 0; // Set all Port A I/O to output
    LATA = 0; // Set all Port A outputs to LOW/0
    ANSELA = 0; // Turn Port A analog off (Digital only)

    // --- Set output for  LCD data: ---
    TRISB = 0; // Set all Port B I/O to output
    LATB = 0; // Set all Port B outputs to LOW/0
    ANSELB = 0; // Turn Port B analog off (Digital only)

    // --- Set input from buttons: ---
    //TRISC = 0b11111000;  // Set Port C 3-7 to input for the buttons
    TRISC = 0xF8;  // Set Port C 3-7 to input for the buttons
    LATC = 0;  // Set all Port C outputs LOW
    ANSELC = 0;  // Turn Port C analog off (Digital only)
}

// --- --- --- --- --- --- ---
// Configure Change Notification for Button Interrupts
// --- --- --- --- --- --- ---
void config_CN(void){

    //Clear Interrupt flag    
    IOCIF = 0; 
    // Clear all of Port C 0-7 interrupt flags
    IOCCF = 0x00;

    // Enable interrupts on Positive Edge of the buttons RC3-RC7
    IOCCP3 = 1;
    IOCCP4 = 1;
    IOCCP5 = 1;
    IOCCP6 = 1;
    IOCCP7 = 1;        

    IOCIE = 1; //Enable Interrupt
}

// --- --- --- --- --- --- ---
// Configure the oscillator: 
// --- --- --- --- --- --- ---

void config_OSC (void) { 
    // Clear registers
    OSCCON1 = 0x00;
    OSCCON2 = 0x00;
    OSCCON3 = 0x00;

    // OSCCON1:
    // Use High Freq. Internal Oscillator (HFINTOSC @ 1 - 32 MHz)
    //OSCCON1bits.NOSC = 0b110;
    OSCCON1bits.NOSC = 0x6;

    // OSCFRQ: 
    // Configure HFINTOSC to 32 MHz
    // OSCFRQbits.HFFRQ = 0b110;
    OSCFRQbits.HFFRQ = 0x6;    

    // Divide clock by 1
    //OSCCON1bits.NDIV = 0b0000;
    OSCCON1bits.NDIV = 0x0; 
}

// ==========================================================================
// Button Function
// ==========================================================================

void buttonSet(uint8_t setter){
    buttonIntention = setter;
}
uint8_t buttonGet(void){
    return buttonIntention;
}

// ==========================================================================
// Interrupt service routine
// ==========================================================================

void __interrupt() isr(void)
{    
    // If button is pressed...
    if(IOCIF == 1 && buttonIntention == 0) {
        //buttonIntention = 1; // Change state to indicate that button was somehow pressed intentionally or unintentionally
        //OR
        buttonSet(1);     
        IOCIF = 0; //Clear Interrupt flag
    }
//if(buttonIntention){
//TESTPIN = 1;
//}
}

// ========================================================================== 
// Main function
// ==========================================================================

int main(void) {
    WDTCON0bits.SWDTEN = 0x0; // Ensure Watchdog Timer is totally disabled

    // Register 10-1 INTCON
    INTCONbits.PEIE = 1; // Enable peripheral interrupt
    INTCONbits.GIE = 1; // Enable global interrupt  

    // --- Call configuration functions: ---    
    config_IO();
    config_OSC();
    config_CN();

    // --- Loop forever: ---
    while (1) {
        if(buttonGet()){
            TESTPIN = 1;              
        }
    }
    return 0;
}

最佳答案

我不了解这个 MCU,但请允许我在黑暗中拍摄两次。

在我看来,你可以通过简单的赋值指令WDTCON0bits.SWDTEN = 0;关闭W/D,这听起来很奇怪。也许看门狗会重置您的 MCU。

spec of PIC16F15376我在第 155 页读到:

The IOCIF bit is the logical OR of all the IOCAF-IOCEF flags. Therefore, to clear the IOCIF flag, application firmware must clear all of the lower level IOCAF-IOCEF register bits.

我认为简单的 IOCIF=0 不会按预期工作。

关于由中断服务例程切换的 volatile 变量的更改未反射(reflect)在 main() 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59113818/

相关文章:

linux-kernel - do_IRQ 中的中断向量和 irq 映射

c - 数组指针 while 循环输入

java - java中interrupt()方法与线程状态的精确行为

c - 无法让中断处理程序在 C 中工作

c++ - volatile 关键字和 RAII 习语 (C++)

c - 主代码中 Global 上的 volatile 限定符,但 ISR 中没有

字符数组不使用 strcpy 复制

c - C 中对变量求和的函数

c - 使用 qsort 函数对 C 中的结构体数组进行排序

c - 在反汇编中演示 volatile 的示例 C 代码?