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
}
//setup the Servo PWM
void bpServo(void)
{
        unsigned int PWM_period, PWM_dutycycle;
		unsigned char entryloop=0;
        float PWM_pd;

        // Clear timers 
        T2CON = 0;              // clear settings
        T4CON = 0;
        OC5CON = 0;
        
        if(AUXmode == AUX_PWM){         //PWM is on, stop it
                if(cmdbuf[((cmdstart + 1)& CMDLENMSK)] == 0x00){//no extra data, stop servo
	                AUXPIN_RPOUT = 0;       //remove output from AUX pin
	                BPMSG1028;
	                AUXmode = AUX_IO;
	                return; // return if no arguments to function
				}
        }

        cmdstart=(cmdstart+1)&CMDLENMSK;

        // Get servo position from command line or prompt for value
        consumewhitechars();
        PWM_pd = getint();
        if (cmderror || (PWM_pd > 180)) {
                cmderror = 0;
                BPMSG1254;
                PWM_pd = getnumber(90, 0, 180, 0);
				entryloop=1;
        }


        // Setup multiplier for 50 Hz
servoset:   T2CONbits.TCKPS1 = 1;
        T2CONbits.TCKPS0 = 1;
        PWM_period = 1250;;
        PWM_pd /= 3500;
        PWM_dutycycle = (PWM_period * PWM_pd) + 62;

        //assign pin with PPS
        AUXPIN_RPOUT = OC5_IO;
        OC5R = PWM_dutycycle;
        OC5RS = PWM_dutycycle;
        OC5CON = 0x6;                   
        PR2     = PWM_period;   
        T2CONbits.TON = 1;      
        BPMSG1255;
        AUXmode=AUX_PWM;

		if(entryloop==1){
	       PWM_pd = getnumber(-1, 0, 180, 1);
			if(PWM_pd<0){
				bpWBR;
				return;
			}
			goto servoset;
		}

}
//setup the PWM/frequency generator
void bpPWM(void){
        unsigned int PWM_period, PWM_dutycycle, PWM_freq, PWM_div;
        int done;
        float PWM_pd;

        //cleanup timers 
        T2CON=0;                // clear settings
        T4CON=0;
        OC5CON =0;
        
        if(AUXmode==AUX_PWM){ //PWM is on, stop it
                AUXPIN_RPOUT = 0;        //remove output from AUX pin
                //bpWline(OUMSG_AUX_PWM_OFF);
                BPMSG1028;
                AUXmode=AUX_IO;

                if(cmdbuf[((cmdstart+1)&CMDLENMSK)]==0x00)  return; // return if no arguments to function
        }

        done=0;

        cmdstart=(cmdstart+1)&CMDLENMSK;
        //cmdstart&=CMDLENMSK;

        //get any compound commandline variables
        consumewhitechars();
        PWM_freq=getint();
        consumewhitechars();
        PWM_pd=getint();

        //sanity check values
        if((PWM_freq>0)&&(PWM_freq<4000)) done++;
        if((PWM_pd>0)&&(PWM_pd<100)) done++;


        //calculate frequency:
        if(done!=2)//no command line variables, prompt for PWM frequency
        {       cmderror=0;

                //bpWline(OUMSG_AUX_PWM_NOTE);
                BPMSG1029;
                //bpWstring(OUMSG_AUX_PWM_FREQ);
                BPMSG1030;
                PWM_freq=getnumber(50,1, 4000, 0);
        }

        //choose proper multiplier for whole range
        //bpWstring(OUMSG_AUX_PWM_PRESCALE);
        //BPMSG1031;
        if(PWM_freq<4){//use 256 //actual max is 62500hz
                //bpWline("256");
                PWM_div=62;//actually 62500
                T2CONbits.TCKPS1=1;
                T2CONbits.TCKPS0=1;
        }else if(PWM_freq<31){//use 64
                //bpWline("64");
                PWM_div=250;
                T2CONbits.TCKPS1=1;
                T2CONbits.TCKPS0=0;
        }else if(PWM_freq<245){//use 8
                //bpWline("8");
                PWM_div=2000;
                T2CONbits.TCKPS1=0;
                T2CONbits.TCKPS0=1;
        }else{//use 1
                //bpWline("1");
                PWM_div=16000;
                T2CONbits.TCKPS1=0;
                T2CONbits.TCKPS0=0;
        }
        PWM_period=(PWM_div/PWM_freq)-1;
        //bpWstring("PR2:");
        //BPMSG1032; 
        //bpWintdec(PWM_period);        //echo the calculated value
        //bpBR;

        if(done!=2)//if no commandline vairable, prompt for duty cycle
        {       //bpWstring(OUMSG_AUX_PWM_DUTY);
                BPMSG1033;
        //      PWM_pd=bpUserNumberPrompt(2, 99, 50);
                PWM_pd=getnumber(50,0,99,0);
        }

        PWM_pd/=100;
        PWM_dutycycle=PWM_period * PWM_pd;
        //bpWdec(PWM_dutycycle);

        //assign pin with PPS
        AUXPIN_RPOUT = OC5_IO;
        // should be fine on bpv4

        OC5R = PWM_dutycycle;
        OC5RS = PWM_dutycycle;
        OC5CON = 0x6;                   
        PR2     = PWM_period;   
        T2CONbits.TON = 1;      

        //bpWline(OUMSG_AUX_PWM_ON);
        BPMSG1034;
        AUXmode=AUX_PWM;

}
Ejemplo n.º 4
0
void uart_setup_prepare(void) {
  int speed, dbp, sb, rxp, output, brg = 0;
  // autobaud detection; multi uses
  unsigned long abd = 0;

#ifdef BUSPIRATEV4
  uart_settings.autobaud = OFF;
#endif /* BUSPIRATEV4 */

  consumewhitechars();
  speed = getint();
  if (speed == 10) // weird this is totaly ignored later as the Speed == 0 check
                   // later skips the calculation I added it below..
  {
    consumewhitechars();
    brg = getint();
  }
  consumewhitechars();
  dbp = getint();
  consumewhitechars();
  sb = getint();
  consumewhitechars();
  rxp = getint();
  consumewhitechars();
  output = getint();

  if ((speed > 0) && (speed <= 10)) {
    mode_configuration.speed = speed - 1;
  } else {
    speed = 0; // when speed is 0 we ask the user
  }

  if ((dbp > 0) && (dbp <= 4)) {
    uart_settings.databits_parity = dbp - 1;
  } else {
    speed = 0; // when speed is 0 we ask the user
  }

  if ((sb > 0) && (sb <= 2)) {
    uart_settings.stop_bits = sb - 1;
  } else {
    speed = 0; // when speed is 0 we ask the user
  }

  if ((rxp > 0) && (rxp <= 2)) {
    uart_settings.receive_polarity = rxp - 1;
  } else {
    speed = 0; // when speed is 0 we ask the user
  }

  if ((output > 0) && (output <= 2)) {
    mode_configuration.high_impedance = ~(output - 1);
  } else {
    speed = 0; // when speed is 0 we ask the user
  }

  if (speed == 0) {
    mode_configuration.command_error = NO;

    MSG_UART_SET_PORT_SPEED;

#if defined(BUSPIRATEV4)
    // BPv4 Mode; has custom BAUD entry and auto-baud detection

    mode_configuration.speed = getnumber(1, 1, 11, 0) - 1; // get user reply

    if (mode_configuration.speed == 10) {
      mode_configuration.speed = 8; // Set to 115200 for now
      abd = 1;                      // trigger to run baud detection
      uart_settings.autobaud = ON;
      MSG_BAUD_DETECTION_SELECTED;
    }

    if (mode_configuration.speed == 9) {
      MSG_UART_CUSTOM_BAUD_RATE_PROMPT;
      abd = getlong(115200, 1, 999999, 0); // get the baud rate from user
      abd = (((32000000 / abd) / 8) - 1);  // calculate BRG
      brg = abd;                           // set BRG
      abd = 0; // set abd to 0; so 'Auto Baud Detection' routine isnt ran below
      // hack hack hakc
      U2BRG = brg; // passing the brg variable to U2BRG so the UARTsetup_exc can
                   // use it to start UART2setup..
    }
#else
    // Normal mode; input BRG and no autobaud detection
    mode_configuration.speed = getnumber(1, 1, 10, 0) - 1; // get user reply

    if (mode_configuration.speed == 9) {
      MSG_UART_RAW_BRG_PROMPT;
      brg = getnumber(34, 1, 32767, 0);
      // hack hack hack
      U2BRG = brg; // passing the brg variable to U2BRG so the UARTsetup_exc can
                   // use it to start UART2setup..
    }
#endif /* BUSPIRATEV4 */

    MSG_UART_BITS_PARITY_PROMPT;
    uart_settings.databits_parity = getnumber(1, 1, 4, 0) - 1;

    MSG_UART_BITS_STOP_PROMPT;
    uart_settings.stop_bits = getnumber(1, 1, 2, 0) - 1;

    MSG_UART_POLARITY_PROMPT;
    uart_settings.receive_polarity = getnumber(1, 1, 2, 0) - 1;

    MSG_PIN_OUTPUT_TYPE_PROMPT;
    mode_configuration.high_impedance = ~(getnumber(1, 1, 2, 0) - 1);
  } else {
    if (mode_configuration.speed == 9) {
      abd = brg;
      abd = (((32000000 / abd) / 8) - 1); // calculate BRG
      brg = abd;                          // set BRG
      abd = 0; // set abd to 0; so 'Auto Baud Detection' routine isnt ran below
      // hack hack hakc
      U2BRG = brg; // passing the brg variable to U2BRG so the UARTsetup_exc can
                   // use it to start UART2setup..
    }
    UARTsettings();
  }
}