由中断服务程序切换为易失性变量的更改未反映在main()中

问题描述 投票:0回答:1

我有一个真正的树桩,我已经坚持了一段时间。我正在用C对PIC16F15376单片机Xpress板进行编程。

我将名为buttonIntention的易失性变量初始化为0。发生中断服务程序时,将调用buttonSet(1),有效地设置了buttonIntention = 1。 [注意:我也尝试过直接切换buttonIntention变量,而没有获取或设置函数。]在main()中,不断轮询的while(1)检查ISR是否已消失(这将设置buttonIntention = 1 ]),如果ISR这样做了,则TESTPIN设置为高电平,以便可以在我的O型镜上查看。

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

但是,我在if(buttonIntention)中的while(1)中的main()从未真正将buttonIntention设置为1。我认为将此变量设为volatile将解决此问题,因为它在ISR和main()之间共享,而且我认为在我的8位微控制器上使用uint8_t类型可以解决我已阅读但不太了解的所有“原子”问题……但是,如果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;
}

我有一个真正的树桩,我已经坚持了一段时间。我正在用C对PIC16F15376微控制器Xpress板进行编程。我将一个名为buttonIntention的易失性变量初始化为0。当...

c interrupt volatile
1个回答
0
投票

我不知道这个MCU,但允许我在黑暗中拍摄两次。

© www.soinside.com 2019 - 2024. All rights reserved.