//Port Mirror 2 //Program to mirror one port (Receiver) to another (Controller) //Written for PIC12F683 / CC5X compiler //Personal and non-commercial use encouraged //Copyright 2007 - David Theunissen - See www.flyelectric.ukgateway.net //PREPROCESSOR section ======================================================== #include "12F683.H" //PIC header file from CC5X #include "delay_ms.h" #pragma config = 0b.11.1100.0100//Core device configuration //VARIABLES section =========================================================== #define ESC GPIO.1 //GP1 (Pin6) #define RX GPIO.3 //GP3 (Pin4) #define LED GPIO.5 //GP5 (Pin2) char p; //FUNCTIONS section =========================================================== void esc_rx() //Function to mirror RX input to ESC output { //Time high pulse while (RX==0); //Wait for RX to finish being low TMR2ON = 1; //Start Timer2 (increments TMR2 to 255 before looping) //Tests using delay_ms(): 63=1ms, 126=2ms, 188=3ms while (RX==1); //Wait for RX to finish being high TMR2ON = 0; //Stop Timer2 //Make ESC go high for same duration PR2 = TMR2; //Timer 2 counts to the value set in PR2 TMR2=0; //Initialise Timer2 ESC=1; TMR2ON = 1; //Start Timer2 (timer increments to PR2 value) while(TMR2IF==0); //TMR2IF flag is set to 1 when Timer2 reaches PR2 //Make ESC low and rest flags ESC = 0; TMR2ON = 0; //Stop Timer2 TMR2IF = 0; //Reset interrupt flag TMR2 = 0; //Initialise Timer2 PR2 = 255; //Set PR2 ('period register') to its max so as to not inhibit Timer2 //In theory this should happen automatically each time it is used //But this requires interrupts to be enabled which has other effects } //----------------------------------------------------------------------------- void main() //main program { //Main port settings // TRISIO0 = 1; //Sets GP0/AN0 (pin7) to Input (Cell1) TRISIO1 = 0; //Sets GP1/AN1 (pin6) to Output (ESC) // TRISIO2 = 1; //sets GP2/AN2 (pin5) to Input (Cell3) TRISIO3 = 1; //sets GP3 (pin4) to Input (RX) // TRISIO4 = 0; //sets GP4/AN3 (pin3) to Output TRISIO5 = 0; //Sets GP5 (pin2) to Output (LED) // ANS0 = 1; //Sets AN0 (pin7) to analogue ANS1 = 0; //Sets AN1 (pin6) to digital (ESC) // ANS2 = 1; //Sets AN2 (pin5) to analogue // ANS3 = 0; //Sets AN3 (pin3) to digital CMCON0 = 0b.111; //Switches all 3 comparator ports off //Static Timer2 settings T2CON.1 = 1; //Prescale 16 //Initialise ESC output ESC=0; //Start ESC low for safety //Flash LED three times on startup for (p=0; p<3; p++) { LED=1; delay_ms(250); LED=0; delay_ms(250); } //----------------------------------------------------------------------------- while(1) { esc_rx(); //Mirror RX input to ESC output each time RX state changes //Put in a separate function to keep main function easier to read //Function ends with ESC low //Put main program here (runs once per RX pulse cycle so must be <20ms long) } //end while } //end main