The software has two main portions:
-CCP1/2 Interrupt Routines
-Main Switch Routine
The Low and High Priority interrupts are assigned to CCP1 and CCP2 and every time an interrupt is triggered a timer value is read and then reset. This allows for real-time tracking of the input signal and output to the LED and motor. Below is the interrupt routines for the CCP modules capturing input signals. Every time there is a rising or falling edge, the interrupt is triggered, timer value read and cleared and the capture mode switched between rising/falling edge captures.
Interrupt Handler Code
------------« Begin Code »------------
void InterruptHandlerHigh() { if(PIR1bits.CCP1IF){ timer1_value = ReadTimer1(); WriteTimer1( 0x0000 ); PIR1bits.CCP1IF = 0; //Switch Capture Rising/Falling Edge if(CCP1CONbits.CCP1M0 == 1) CCP1CONbits.CCP1M0 = 0; else CCP1CONbits.CCP1M0 = 1; } else if(PIR1bits.TMR1IF){ PIR1bits.TMR1IF = 0; } INTCONbits.GIEH = 1; return; } void InterruptHandlerLow() { if(PIR2bits.CCP2IF){ timer0_value = ReadTimer0(); WriteTimer0( 0x0000 ); PIR2bits.CCP2IF = 0; //Switch Capture Rising/Falling Edge if(CCP2CONbits.CCP2M0 == 1) CCP2CONbits.CCP2M0 = 0; else CCP2CONbits.CCP2M0 = 1; } else if(INTCONbits.TMR0IF){ INTCONbits.TMR0IF = 0; } INTCONbits.GIEL = 1; return; }
------------« End Code »------------
The next set of code is where the main function evaluates and outputs according to the current input values. Since the input values can be constantly changing, the main function needs to act on these changes and update the output.
LED and Motor Input/Output Control
------------« Begin Code »------------
.. ... //Channl 0 Control if(timer1_value > 0x03E8 && timer1_value < 0x07D0) temp = (timer1_value>>5)-30; //Channel 1 Control //Use a Switch Statement next time Chris...>_<<br /> if(timer0_value < 0x03E8){ forward = 0; reverse = 0; } else if(timer0_value > 0x03E8 && timer0_value <= 0x0430){ forward = 0; reverse = 55; } .. ... ... //LED Dimmer Control if(cnt < temp) PORTA = 0x01; else PORTA = 0x00; //Move Motor Forward if(forward > 0 && reverse == 0){ if(cnt < forward){ PORTB = PORTB ^ 0b11111110; } else{ PORTB = 0x00; } } //Move Motor Reverse else if(reverse > 0 && forward == 0){ if(cnt < reverse) PORTB = PORTB ^ 0b11111101; else PORTB = 0x00; } else PORTB = PORTB & 0b00000000; if(cnt == 60) cnt = 0; cnt++; ... ..
------------« End Code »------------
This system is very much a 2 way input, 2 way output straight forward system. The interrupt routines allow for us to act in real time on signal input from the receiver and to create the output we desire. The LED brightness varies depending on the CCP1, timer1 value. The motor controller forward and reverse are set at different values depending upon the CCP2, timer0 value.