void picinit(void) { int mode, delay; int interactive; consumewhitechars(); mode=getint(); consumewhitechars(); delay=getint(); interactive=0; if(!((mode>0)&&(mode<=2))) { interactive=1; } if((delay>0)&&(delay<=2)) { piccmddelay=delay; } else { interactive=1; } if(interactive) { cmderror=0; //bpWline("Commandmode"); //bpWline("1. 6b/14b"); //bpWline("2. 4b/16b"); BPMSG1072; mode=getnumber(1,1,2,0); //bpWline("Delay"); BPMSG1073; delay=getnumber(1,1,2,0); } switch(mode) { case 1: picmode=PICMODE6; break; case 2: picmode=PICMODE4; break; default: break; } piccmddelay=delay; if(!interactive) { //bpWstring("PIC(mod dly)=("); BPMSG1074; bpWdec(picmode); bpSP; bpWdec(piccmddelay); bpWline(")"); } modeConfig.HiZ=1; // to allow different Vcc modeConfig.int16=1; bbL(MOSI|CLK, PICSPEED); // pull both pins to 0 before applying Vcc and Vpp }
//self test, showProgress=1 displays the test results in the terminal, set to 0 for silent mode //errors are counted in the global errors variable //returns number of errors unsigned char selfTest(unsigned char showProgress, unsigned char jumperTest){ //toggle display of test results with show Progress variable errors=0; if(!showProgress) bpConfig.quiet=1; //instructions (skip pause if no display output) if(showProgress && jumperTest){ //bpPOSTWline("Disconnect any devices"); //bpPOSTWline("Connect (Vpu to +5V) and (ADC to +3.3V)"); BPMSG1163; BPMSG1251; // //bpPOSTWline("Press a key to start"); //JTR Not required while(!UART1RXRdy()); //wait for key UART1RX();//discard byte } //bpPOSTWline("Ctrl"); BPMSG1164; BP_AUX0=1; BP_AUX0_DIR=0; //bpPOSTWstring("AUX"); BPMSG1165; bpTest(BP_AUX0,1); BP_AUX0=0; BP_AUX0_DIR=1; BP_LEDMODE=1; BP_LEDMODE_DIR=0; //bpPOSTWstring("MODE LED"); BPMSG1166; bpTest(BP_LEDMODE,1); BP_LEDMODE=0; BP_PULLUP_ON(); //bpPOSTWstring("PULLUP H"); BPMSG1167; bpTest(BP_PULLUP,1); BP_PULLUP_OFF(); //bpPOSTWstring("PULLUP L"); BPMSG1168; bpTest(BP_PULLUP,0); BP_VREG_ON(); bpDelayMS(2);//in silent mode there's not enought delay for the power supplied to come on //bpPOSTWstring("VREG"); BPMSG1169; bpTest(BP_VREGEN,1); #if defined (BUSPIRATEV4) BPMSG1265; //bpWline("EEPROM"); BPMSG1266; //bpWstring("SCL"); bpTest(BP_EE_SCL, 1); BPMSG1267; //bpWstring("SDA"); bpTest(BP_EE_SDA, 1); BPMSG1268; //bpWstring("WP"); bpTest(BP_EE_WP, 1); BPMSG1269; //bpWstring("ACK"); bpTest(eetest(), 0); #endif //ADC check //bpPOSTWline("ADC and supply"); BPMSG1170; ADCON(); // turn ADC ON #if defined (BUSPIRATEV4) BPMSG1270; //bpWstring("Vusb"); bpADCPinTest(BP_ADC_USB,V5L, V5H); //bpPOSTWstring("5V"); BPMSG1171; bpADCPinTest(BP_ADC_5V0,V5L, V5H); //enable 5v0 pullup and test BP_5VPU_ON(); BPMSG1171; //bpWstring("5V0 VPU"); bpWstring(" "); BPMSG1172; //VPU bpDelayMS(2); bpADCPinTest(BP_ADC_VPU,V5L, V5H); BP_5VPU_OFF(); //ADC test (3.3 volts) if(jumperTest){ //ADC is connected to 3.3 volts //bpPOSTWstring("ADC"); BPMSG1174; bpADCPinTest(BP_ADC_PROBE,V33L, V33H); } //bpPOSTWstring("3.3V"); BPMSG1173; bpADCPinTest(BP_ADC_3V3,V33L, V33H); //enable 3v3 pullup and test BP_3V3PU_ON(); BPMSG1173; //bpWstring("3V3 VPU"); bpWstring(" "); BPMSG1172; //VPU bpDelayMS(2); bpADCPinTest(BP_ADC_VPU,V33L, V33H); BP_3V3PU_OFF(); #elif defined (BUSPIRATEV3) //v3 test //0x030F is 5volts //bpPOSTWstring("5V"); BPMSG1171; bpADCPinTest(BP_ADC_5V0,V5L, V5H); if(jumperTest){ //Vpullup is connected to 5volts //bpPOSTWstring("VPU"); BPMSG1172; bpADCPinTest(BP_ADC_VPU,V5L, V5H); } //0x0208 is 3.3volts //bpPOSTWstring("3.3V"); BPMSG1173; bpADCPinTest(BP_ADC_3V3,V33L, V33H); if(jumperTest){ //ADC is connected to 3.3volts //bpPOSTWstring("ADC"); BPMSG1174; bpADCPinTest(BP_ADC_PROBE,V33L, V33H); } #endif ADCOFF(); // turn ADC OFF //************* // // Test bus pins three ways, also tests on-board pullup resistors: // 1. normal/high, 2. open collector ground, 3. open collector high. // //*************** //pullup off, pins=output & high, read input, high? //bpPOSTWline("Bus high"); BPMSG1175; IODIR&= ~(ALLIO);//output IOLAT|=ALLIO; //high bpDelayMS(100); bpBusPinsTest(1); //pullup on, pins=output & low, read input, low? //bpPOSTWline("Bus Hi-Z 0"); BPMSG1176; IOLAT&= ~(ALLIO); //low if(jumperTest){ #if defined (BUSPIRATEV4) BP_3V3PU_ON(); #endif BP_PULLUP_ON(); } bpDelayMS(100); bpBusPinsTest(0); if(jumperTest){ //pullup on, pins=input & low, read input, high? //bpPOSTWline("Bus Hi-Z 1"); BPMSG1177; IODIR|=ALLIO;//output bpDelayMS(100); bpBusPinsTest(1); #if defined (BUSPIRATEV4) BP_3V3PU_OFF(); #endif } //instructions (skip pause if no display output) if(showProgress && jumperTest){ BP_VREG_ON(); BP_MODELED_ON(); //bpPOSTWline("MODE and VREG LEDs should be on! Any key exits."); #if defined (BUSPIRATEV4) BP_USBLED_ON(); #endif BPMSG1178; BPMSG1250; //JTR Not required while(!UART1RXRdy()); UART1RX(); #ifdef BUSPIRATEV4 BP_USBLED_OFF(); #endif BP_MODELED_OFF(); BP_VREG_OFF(); } bpInit();//clean up BPMSG1179; bpWdec(errors); BPMSG1180; bpConfig.quiet=0; return errors; }
//frequency measurement void bpFreq(void){ // frequency accuracy optimized by selecting measurement method, either // counting frequency or measuring period, to maximize resolution. // Note: long long int division routine used by C30 is not open-coded */ unsigned long long f, p; if(AUXmode==AUX_PWM){ //bpWline(OUMSG_AUX_FREQ_PWM); BPMSG1037; return; } //bpWstring(OUMSG_AUX_FREQCOUNT); BPMSG1038; //setup timer T4CON=0; //make sure the counters are off T2CON=0; //timer 2 external AUXPIN_DIR=1;//aux input RPINR3bits.T2CKR=AUXPIN_RPIN; //assign T2 clock input to aux input // should be good on bpv4 T2CON=0b111010; //(TCKPS1|TCKPS0|T32|TCS); // prescale to 256 f=bpFreq_count(); // all measurements within 26bits (<67MHz) // counter only seems to be good til around 6.7MHz, // use 4.2MHz (nearest power of 2 without exceeding 6.7MHz) for reliable reading if(f>0x3fff){ // if >4.2MHz prescaler required f*=256; // adjust for prescaler }else { // get a more accurate reading without prescaler //bpWline("Autorange"); BPMSG1245; T2CON=0b001010; //(TCKPS1|TCKPS0|T32|TCS); prescale to 0 f=bpFreq_count(); } // at 4000Hz 1 bit resolution of frequency measurement = 1 bit resolution of period measurement if(f>3999){ // when < 4 KHz counting edges is inferior to measuring period(s) bpWlongdecf(f); // this function uses comma's to seperate thousands. bpWline(" Hz"); }else if (f>0) { BPMSG1245; p=bpPeriod_count(f); // don't output fractions of frequency that are less then the frequency // resolution provided by an increment of the period timer count. if (p>400000) { // f <= 40 Hz // 4e5 < p <= 1,264,911 (625us tics) // 12.61911 < f <= 40 Hz // output resolution of 1e-5 f=16e11/p; bpWlongdecf(f/100000); UART1TX('.'); f = f % 100000; if (f < 10000) UART1TX('0'); if (f < 1000) UART1TX('0'); if (f < 100) UART1TX('0'); if (f < 10) UART1TX('0'); bpWlongdec(f); // at p=126,491.1 frequency resolution is .001 } else if (p>126491) { // f <= 126.4911 // 126,491 < p <= 4e5 (625us tics) // 40 < f <= 126.4911 Hz // output resolution of .0001 f=16e10/p; bpWlongdecf(f/10000); UART1TX('.'); f = f % 10000; if (f < 1000) UART1TX('0'); if (f < 100) UART1TX('0'); if (f < 10) UART1TX('0'); bpWintdec(f); // at p=40,000 frequency resolution is .01 } else if (p>40000) { // f <= 400 Hz // 4e4 < p <= 126,491 (625us tics) // 126.4911 < f <= 400 Hz // output resolution of .001 f=16e9/p; bpWlongdecf(f/1000); UART1TX('.'); f = f % 1000; // frequency resolution < 1e-2 if (f < 100) UART1TX('0'); if (f < 10) UART1TX('0'); bpWintdec(f); // at p=12,649.11 frequency resolution is .1 }else if (p>12649) { // f <= 1264.911 // 12,649 < p <= 4e4 (625us tics) // 400 < f < 1,264.911 Hz // output resolution of .01 f=16e8/p; bpWlongdecf(f/100); UART1TX('.'); f = f % 100; // frequency resolution < 1e-1 if (f < 10) UART1TX('0'); bpWdec(f); // at p=4,000 frequency resolution is 1 }else { // 4,000 < p <= 12,649 (625us tics) // 1,264.911 < f < 4,000 Hz // output resolution of .1 f=16e7/p; bpWlongdecf(f/10); UART1TX('.'); f = f % 10; // frequency resolution < 1 bpWdec(f); } bpWline(" Hz"); //END of IF(f>0) }else bpWline("Frequencies < 1Hz are not supported."); //return clock input to other pin RPINR3bits.T2CKR=0b11111; //assign T2 clock input to nothing T4CON=0; //make sure the counters are off T2CON=0; }