//PULSE_WIDTH_1 //Program to measure pulse widths from receiver and use LED to reveal results //Written for PIC12F683 to be compiled with the CC5X compiler //Clock frequency left at 4MHz default //Copyright David Theunissen 2007 www.flyelectric.ukgateway.net //PREPROCESSOR section ======================================================== #include "12F683.H" //PIC chip file //#include "delay_ms.h" #pragma config = 0b.0.1100.0100 //Brown-out Detect disabled (first 0) //Code Protect off (1) //MCLR set to 'Internal' (1) //Powerup timer enabled (0) //Watchdog timer disabled (0) //Oscillator set to INTRCIO (100) //VARIABLES section =========================================================== #define LED1 GPIO.0 //Define pin7 as LED1 #define ESC GPIO.1 //ESC output defined (pin6) #define RX GPIO.3 //Pin4 is input from Receiver (NB: Disconnect from Rx when programming) //Change to GP5 if available (does not interfere with programming as does pin3 char PULSE; //Variable to store duration of high pulse //FUNCTIONS section =========================================================== void main() { //Main port settings TRISIO0 = 0; //Sets GP0 (pin7) to Output (LED1) TRISIO1 = 0; //Sets GP1 (pin6) to Output (ESC) TRISIO3 = 1; //Sets GP3 (pin4) to Intput (RX) ANS0 = 0; //Disables ADC port AN0 ANS1 = 0; //Disables ADC port AN1 CMCON0 = 0b.111; //Switches all 3 comparator ports off while(1) { //measure receiver's throttle pulse---------------------------------------- PULSE = 0; //set pulse width variable to zero while (RX==0); //Wait for RX signal to be low (to ensure full length of next high is measured) while (RX==1) //Wait for RX signal to go high { PULSE++; //increment pulse width variable while RX pulse is high } //Measured at 100% ATV: Low+low_trim=123, Low+mid_trim=140, mid=190, full=242 //Divide PULSE count by 125 to get pulse width in ms (eg: 140/125=1.12ms) //At 4MHz this implies 8 instructions per count @ 1uS each (eg: 140*8*1=1120uS) //use LED to test which throttle positions make LED come on/off--------------- if (PULSE>=140) //change number to tease out range of operation { LED1=1; //yes } else { LED1=0; //no } } //end while } //end main