简体   繁体   English

不确定为什么我的代码不起作用

[英]Not sure why my code isn't working

We have to make a digital stop watch and one of the requirements is an on off switch. 我们必须制造一个数字秒表,其中一项要求是开关。 To do this I have got a variable to sees if the switch has been pressed or not and goes from there...however it is not stopping, it is only starting it. 为此,我有一个变量来查看开关是否被按下并从那里离开...但是它没有停止,只是在启动它。

Here is my code...does anyone have any ideas as to it why it won't work? 这是我的代码...有人对它为什么不起作用有任何想法吗? Any help would be great. 任何帮助都会很棒。

#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  
}

At your last __ attribute__ function you forgot an else before the second if. 在最后一个__ attribute__函数中,您在第二个if之前忘记了else。

The first if, when true, is setting switch to zero. 如果为true,则第一个将开关设置为零。 Right after it you are checking if switch is zero and setting it back to one. 在它之后,您正在检查switch是否为零并将其设置回1。 Thus never leaving the state of 1. 因此永远不会离开状态1。

Just do an else if instead of if... 如果不是,则执行其他操作...

if(switch1==1)
{
    T1CONbits.TON = 0;
        switch1=0;
}
else if(switch1==0)  //HERE
{   
   ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM