我们必须制作数字秒表,其中一项要求是开关。为了做到这一点,我有一个变量来看看是否按下开关并从那里开始......但是它没有停止,它只是启动它。
这是我的代码......有没有人有任何想法,为什么它不起作用?任何帮助都会很棒。
#include <p24fj128ga010.h>
#include "main.h"
_CONFIG2( FNOSC_FRCPLL & OSCIOFNC_OFF ); // Internal oscillator - 4xPLL giving 16 MIPS
// pin RC15 has the oscillator
//Function prototypes
extern void update_display(const unsigned int min, const unsigned int tens, const unsigned int units, const unsigned int tenths, const unsigned int hund);
//Global variables
unsigned int minutes; //Minutes - For display 0
unsigned int units; //Seconds (tens) - For display 1
unsigned int tens; //Seconds (units) - For display 2
unsigned int tenths; //Tenths of a second - For display 3
unsigned int hund;
unsigned int displayOn=0; //Switch for Display ON or OFF - shared by two ISR routines
unsigned int IC1BUFFERVAL; //Used to read IC1 timer buffer
char switch1 =0;
unsigned int IC2BUFFERVAL; //Used to read IC2 timer buffer
io_port_A *display = (io_port_A*)&LATA; // Address of Output latch (that connects to PORTA) - it is SAFER to write to the LATCH
//Main code
int main()
{
//Configure the device to use the internal oscillator
OSCCONbits.COSC = 1u; // Internal oscillator with PLL
CLKDIVbits.RCDIV = 0u; // Internal oscillator not divided (8MHz)
CLKDIVbits.DOZEN =0; // No post scaling
CLKDIVbits.DOZE =0; //
//set up ports
TRISA = 0x0070; //Least-significant 4 and most significant 8 bits are digital outputs
LATA = 0xFFFF; //Write zero to PORTA via the latch
//Initialisation of variables and display
minutes = 0;
units = 0;
tens = 0;
tenths = 0;
hund = 0;
update_display(1,2,3,4,5);
update_display(minutes, tens, units, tenths, hund);
//TIMER1 Configuration Register
T1CON = 0;
T1CONbits.TCKPS = 0b11; //Prescale = 256
TMR1 = 0; //Reset Timer Counter
//Configure TIMER1 interrupt
_T1IP = 4; //Interrupt priority = 4
PR1 = 625-1; //Period Register - set for 0.1s. Took a 0 out to make hund work.
_T1IF = 0; //Clear timer interrupt flag
//Configure the input capture (IC1)
_IC1IP = 4; //Set interrupt priority for the Input Capture
_TRISD8 = 1; //Set RD8 to be an input
IC1CON = 0x0003; //InputCaptureCONfiguration register - falling edge, 3 makes it rising
_IC1IF = 0; //Reset the Input Capture 1 Interrupt Flag
//Configure the input capture (IC2) //button 2 initialisation
_IC2IP = 4; //Set interrupt priority for the Input Capture
_TRISD9 = 1; //Set RD9 to be an input
IC2CON = 0x0003; //InputCaptureCONfiguration register - rising edge
_IC2IF = 0;
//Configure the input capture (IC3)
_IC3IP = 4; //Set interrupt priority for the Input Capture
_TRISD10 = 1; //Set RD8 to be an input
IC3CON = 0x0003; //InputCaptureCONfiguration register - falling edge
_IC3IF = 0; //Reset the Input Capture 1 Interrupt Flag
//ENABLE INTERRUPTS
_IC1IE = 1; //Set the Input Capture 1 Interrupt Enable
_IC2IE = 1; //Set the Input Capture 2 Interrupt Enable
_IC3IE = 1; //Set the Input Capture 3 Interrupt Enable
_T1IE = 1; //Switch on timer 1 interrupt enable
//Main Loop
while (1)
{
Idle(); //Switch off main core - peripherals continue
update_display(minutes, tens, units, tenths, hund);
} //end while
return 0;
}
//************************************************************************
//********************* INTERRUPT SERVICE ROUTINES ***********************
//************************************************************************
//ISR FOR THE TIMER
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt()
{
//Increment the counter on every interrupt
hund++;
//Update minutes, tens, units and tenths for the display (BCD)
//Check for an overflow - ripple to the next digit
if (hund == 10)
{
hund = 0;
tenths ++;
}
if (tenths == 10) {
tenths = 0;
units++;
}
if (units == 10) {
units = 0;
tens++;
}
if (tens == 6) {
tens = 0;
minutes++;
}
if (minutes == 10) {
minutes=0;
}
update_display(minutes, tens, units, tenths, hund);
//Reset interrrupt flag before returning
_T1IF = 0;
}
//ISR FOR THE INPUT CAPTURE
void __attribute__((interrupt, no_auto_psv)) _IC1Interrupt()
{
// INSERT CODE TO HANDLE INPUT CAPTURE (IC1) INTERRUPT
if(switch1==1)
{
T1CONbits.TON = 0;
switch1=0;
}
if (switch1==0)
{
T1CONbits.TON = 1;
switch1=1;
}
//Read the IC1 buffer (we are not using this yet)
IC1BUFFERVAL = IC1BUF; //This is needed to prevent buffer overflow
_IC1IF = 0; //Reset the interrupt flag
}
答案 0 :(得分:4)
在你最后的__ attribute__函数中,你忘记了第二个if之前的其他内容。
第一个if,当为true时,将设置切换为零。在它之后,您正在检查开关是否为零并将其设置为1。因此永远不会离开1的状态。
如果不是,那就去做别的......
if(switch1==1)
{
T1CONbits.TON = 0;
switch1=0;
}
else if(switch1==0) //HERE
{
...
}