2014-01-26 14 views
0

Мы должны сделать цифровой секундомер, и одно из требований - выключенный переключатель. Для этого у меня есть переменная, которая видит, был ли нажат переключатель или нет, и идет оттуда ... однако он не останавливается, он только запускает его.Не знаете, почему мой код не работает

Вот мой код ... есть ли у кого-нибудь идеи относительно того, почему это не сработает? Любая помощь будет большой.

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

Более описательное название может помочь ваш вопрос будет найден/ответил. –

+1

Вы ожидаете от нас отладки аппаратного драйвера без аппаратного обеспечения. Это высокий порядок. Вы можете начать с указания имени чипа, который вы пытаетесь запрограммировать (PIC немного расплывчато) и платформы, на которую вы нацеливаете. Но, честно говоря, я не ожидал бы ответа на такие специализированные вопросы. –

ответ

4

В вашей последней функции __ attribute__ вы забыли другое до второго, если.

Первый, если true, устанавливает переключатель в ноль. Сразу после этого вы проверяете, равен ли переключатель нулю, и установите его на один. Таким образом, никогда не выходя из состояния 1.

вобще еще, если вместо того, чтобы, если ...

if(switch1==1) 
{ 
    T1CONbits.TON = 0; 
     switch1=0; 
} 
else if(switch1==0) //HERE 
{ 
    ... 
} 
Смежные вопросы