C语言 由中断服务例程切换的易失变量更改未反映在 main() 中



我有一个真正的绊脚石,我已经坚持了一段时间。我正在用 C 语言编写一个PIC16F15376微控制器 Xpress 板。

我将一个名为buttonIntention的易失变量初始化为 0。当中断服务例程发生时,将调用buttonSet(1),从而有效地设置buttonIntention = 1。[注意:我也尝试直接切换buttonIntention变量,没有get或set函数。在main()中,不断轮询的while(1)检查ISR是否已关闭(这将设置buttonIntention = 1(,如果ISR关闭,则TESTPIN设置得很高,我可以在我的o-scope上查看它。

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

但是,我在main()while(1)中的if(buttonIntention)实际上从未看到 buttonIntention 设置为 1。我认为将此变量作为volatile可以解决此问题,因为它在 ISR 和main()之间共享,并且我认为在我的 8 位微控制器上使用uint8_t类型可以解决我读过但并不真正理解的任何"原子"问题......但是,如果语句永远不会看到 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。

我在第 155 页上读到的PIC16F15376规格:

IOCIF 位是所有 IOCAF-IOCEF 标志的逻辑 OR。 因此,若要清除 IOCIF 标志,应用程序固件必须清除 所有较低级别的IOCAF-IOCEF寄存器位。

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

最新更新