Пример #1
0
int main(int argc, char** argv) {
        //DDPCONbits.JTAGEN = 0; // Disable JTAG
        mPORTGSetPinsDigitalIn(0x00FF);
	mPORTBSetPinsAnalogIn(0x00FF);             //Enable all analog

	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
	CloseADC10();
	SetChanADC10(INITCH);
	OpenADC10(CONFIG1, CONFIG2, CONFIG3, CFGPORT, CFGSCAN);
	ConfigIntADC10(CFGINT);
	EnableADC10();

   	//Initialize the DB_UTILS IO channel
	DBINIT();

	// Display the introduction
	DBPRINTF("Welcome to the Analog Input Test.\n");
	DBPRINTF("The build date and time is (" __DATE__ "," __TIME__ ")\n");

	while (1){
		 //Get damper value from PIC analog input
		int i = 0;
		for(i;i<1;i++){
			int analog = ReadADC10(i);
			DBPRINTF("Hammer %d = %d", i, analog);
			DBPRINTF("\n");
		}
                int digital = mPORTGReadBits(BIT_0);
                DBPRINTF("digital: %X \n \n", digital );
	}
	return 0;
}
Пример #2
0
int main(void){
    mPORTBSetPinsAnalogIn(0xFFFF);             //Enable all analog
    mPORTDClearBits(BIT_0 |BIT_1 | BIT_8);
    mPORTDSetPinsDigitalOut(BIT_0 | BIT_1 | BIT_8);

    SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
    CloseADC10();
    SetChanADC10(INITCH);
    OpenADC10(CONFIG1, CONFIG2, CONFIG3, CFGPORT, CFGSCAN);
    ConfigIntADC10(CFGINT);
    EnableADC10();

    mPMPOpen(PMP_CONTROL, PMP_MODE, PMP_PORT, PMP_INT);
    mPMPEnable();

    //mPORTDClearBits(BIT_0 |BIT_1 | BIT_8);
    INTEnableSystemMultiVectoredInt();

    OpenTimer2(T2_ON | T2_PS_1_64, 256);
    ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_5 | T2_INT_SUB_PRIOR_2);
    EnableIntT2;
    
    mPORTDToggleBits(BIT_0);

    while(1){
        //Blink so we know we're still running
        noteOn(67, 100);
        //delayMs(1000);
        mPORTDToggleBits(BIT_0);
        //noteOff(67);
        DelayMs(1000);
        //mPORTDToggleBits(BIT_0);
    }
    return 0;
}
Пример #3
0
void adcConfigureAutoScan()
{
        // configure and enable the ADC
        CloseADC10();	// ensure the ADC is off before setting the configuration

        // define setup parameters for OpenADC10
		    // Turn module on | output in integer | trigger mode auto | enable  autosample
	#define PARAM1  ADC_MODULE_ON | ADC_FORMAT_INTG16 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON

	// define setup parameters for OpenADC10
			    // ADC ref external    | disable offset test    | enable scan mode | perform 2 samples | use one buffer | use MUXA mode
        // note: to read X number of pins you must set ADC_SAMPLES_PER_INT_X
	#define PARAM2  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_3 | ADC_ALT_BUF_ON | ADC_ALT_INPUT_OFF

	// define setup parameters for OpenADC10
	// 				  use ADC internal clock | set sample time
	#define PARAM3  ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_11

	// define setup parameters for OpenADC10
				// set AN4 and AN5
	#define PARAM4	ENABLE_AN13_ANA|ENABLE_AN12_ANA|ENABLE_AN25_ANA

	// define setup parameters for OpenADC10
	// do not assign channels to scan
	#define PARAM5	SKIP_SCAN_ALL//SKIP_SCAN_AN0|SKIP_SCAN_AN1|SKIP_SCAN_AN2|SKIP_SCAN_AN3|SKIP_SCAN_AN4|SKIP_SCAN_AN5|SKIP_SCAN_AN6|SKIP_SCAN_AN7|SKIP_SCAN_AN8|SKIP_SCAN_AN9|SKIP_SCAN_AN10|SKIP_SCAN_AN11|SKIP_SCAN_AN12|SKIP_SCAN_AN14|SKIP_SCAN_AN15|SKIP_SCAN_AN16|SKIP_SCAN_AN17|SKIP_SCAN_AN18|SKIP_SCAN_AN19|SKIP_SCAN_AN20|SKIP_SCAN_AN21|SKIP_SCAN_AN22|SKIP_SCAN_AN23|SKIP_SCAN_AN24|SKIP_SCAN_AN25|SKIP_SCAN_AN26|SKIP_SCAN_AN27
        // use ground as neg ref for A
	SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF ); // // configure to sample A13
	OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using parameter define above
        AD1CSSLSET = 0x2003000;
	EnableADC10(); // Enable the ADC
}
Пример #4
0
int main(void){
        mPORTASetPinsDigitalIn(0xFFFF);
	mPORTBSetPinsAnalogIn(0xFFFF);             //Enable all analog

	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
	CloseADC10();
	SetChanADC10(INITCH);
	OpenADC10(CONFIG1, CONFIG2, CONFIG3, CFGPORT, CFGSCAN);
	ConfigIntADC10(CFGINT);
	EnableADC10();

   	//Initialize the DB_UTILS IO channel
	DBINIT();

	// Display the introduction
	DBPRINTF("Welcome to the Analog Input Test.\n");
	DBPRINTF("The build date and time is (" __DATE__ "," __TIME__ ")\n");

	while (1){
		 //Get damper value from PIC analog input
		int i = 0;
		for(i;i<16;i++){
			int damper = ReadADC10(i);
			DBPRINTF("Damper %d = %d", i, damper);
			DBPRINTF("\n");
		}

            //DBPRINTF("%d \n", LATA );
	}
	return 0;
}
Пример #5
0
// Prepare Hardware Channel
int platform_adc_update_sequence( )
{
  elua_adc_dev_state *d = adc_get_dev_state( 0 );

  CloseADC10();   // Ensure the ADC is off before setting the configuration

  AD1CSSL = 0;
  AD1PCFG = 0xffffffff;

  platform_setup_adcs();

  d->seq_ctr = 0;
  while( d->seq_ctr < d->seq_len )
  {
    AD1CSSLSET = (1 << d->ch_state[ d->seq_ctr ]->id);
    AD1PCFGCLR = (1 << d->ch_state[ d->seq_ctr ]->id);
    d->seq_ctr++;
  }
  d->seq_ctr = 0;

  EnableADC10();

  // start conversion
  AD1CON1SET = _AD1CON1_ASAM_MASK;
  AD1CON1SET = _AD1CON1_SAMP_MASK;
  
  return PLATFORM_OK;
}
Пример #6
0
// initialize the analog input
void initAnalogInput(void)
{
    CloseADC10();
    #define PARAM1  ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
    #define PARAM2  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_16 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF
    #define PARAM3  ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_31
    #define PARAM4  ENABLE_AN0_ANA | ENABLE_AN1_ANA | ENABLE_AN2_ANA | ENABLE_AN3_ANA | ENABLE_AN5_ANA | ENABLE_AN15_ANA
    OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4,0);
    EnableADC10();
}
Пример #7
0
/**
 * @private
 * Initialise the Analog Digital Converter
 * @param CONFIG1 bit-masks first turns the module on, formats the output as 32-bit integers,
 * triggers its conversions automatically, and automatically samples.
 * @param CONFIG2 bit-masks sets the voltage references to Vdd and Vss, disables the offset calculation,
 * enables scanning, specifies how many samples are to be read per interrupt (which
 * should be changed if multiple analog pins are being read?ADC_SAMPLES_PER_INT_x where x is the
 * number of pins to be read), disables the buffer, and disables input sample mode.
 * @param CONFIG3 bit-masks selects which clock to use, and selects the auto-sample time bits.
 * @param CONFIGPORT bit-masks selects which analog pins are enabled. This is similar to setting the AD1PCFG register.
 * @param CONFIGSCAN bit-masks selects which analog pins are to be scanned or skipped.
 */
void _initADC(){
    CloseADC10();       
    SetChanADC10(ADC_CH0_NEG_SAMPLEA_NVREF);
    OpenADC10(CONFIG1, CONFIG2, CONFIG3, CONFIGPORT, CONFIGSCAN);
    EnableADC10();
    // mAD1GetIntFlag() checks the interrupt flag for the AD10.
    // Waits till a conversion is finished so that there's
    // values in the ADC result registers.
    while (!mAD1GetIntFlag() ) {
    
    }
}
Пример #8
0
void platform_setup_adcs()
{
  unsigned id;

  for( id = 0; id < NUM_ADC; id ++ )
    adc_init_ch_state( id );
  
  CloseADC10();   // Ensure the ADC is off before setting the configuration
  OpenADC10(PARAM1, PARAM2, PARAM3, PARAM4, PARAM5);
  ConfigIntADC10(ADC_INT_ON | ADC_INT_PRI_3 | ADC_INT_SUB_PRI_1);

  platform_adc_set_clock( 0, 0 );
}
Пример #9
0
void __ISR(_ADC_VECTOR, ipl3) ADCInterruptHandler(void)
{
  // Clear the ADC's interrupt flag.
  IFS1CLR = ADC_INT_FLAG;

  elua_adc_dev_state *d = adc_get_dev_state( 0 );
  elua_adc_ch_state *s;
  
  if ( AD1CON1bits.DONE )
  {
    d->seq_ctr = 0;
    while( d->seq_ctr < d->seq_len )
    {
      s = d->ch_state[ d->seq_ctr ];
      d->sample_buf[ d->seq_ctr ] = ( u16 )ReadADC10(0);
      s->value_fresh = 1;

      // Fill in smoothing buffer until warmed up
      if ( s->logsmoothlen > 0 && s->smooth_ready == 0)
        adc_smooth_data( s->id );
#if defined( BUF_ENABLE_ADC )
      else if ( s->reqsamples > 1 )
      {
        buf_write( BUF_ID_ADC, s->id, ( t_buf_data* )s->value_ptr );
        s->value_fresh = 0;
      }
#endif
      // If we have the number of requested samples, stop sampling
      if ( adc_samples_available( s->id ) >= s->reqsamples && s->freerunning == 0 )
        platform_adc_stop( s->id );

      d->seq_ctr++;
    }
    d->seq_ctr = 0;
  }

  if( d->running == 1 )
    adc_update_dev_sequence( 0 );

  if ( d->clocked == 0 && d->running == 1 )
  {
      // start conversion
      AD1CON1SET = _AD1CON1_ASAM_MASK;
      AD1CON1SET = _AD1CON1_SAMP_MASK;
  }

  if(!d->running)
  {
      CloseADC10();
  }
}
Пример #10
0
void fis_stop_expFis(void){
    //ISR
    DisableIntT5;
    DisableIntT4;
    DisableIntADC1;

    #if (SCH_FISICA_VERBOSE>=1)
        con_printf("expFis ISRs are down..\r\n");
    #endif
    //Modules
    CloseTimer4();
    CloseTimer5();
    CloseADC10();
}
Пример #11
0
void init_adc(void) {
    // configure and enable the ADC
    // A0 = AN2, A1 = AN4
    CloseADC10();    // ensure the ADC is off before setting the configuration
    AD1CHS = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN2; // configure to sample AN2
    mPORTBSetPinsAnalogIn(SKIP_SCAN_ALL);
    AD1CSSL = ~(ENABLE_AN2_ANA | ENABLE_AN4_ANA);
    AD1CON3 = ADC_CONV_CLK_PB | ADC_SAMPLE_TIME_15 | ADC_CONV_CLK_16Tcy;
    AD1CON2 = ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | \
              ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF;
    AD1CON1 = ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_MANUAL | ADC_AUTO_SAMPLING_OFF;
    EnableADC10(); // Enable the ADC
    //AcquireADC10();
}
Пример #12
0
void  ADC_Init (void)
{
	//!!!!!!!! PORTB BIT_2 --> GP2_6 ne fonctionne pas !!!!!!!!!! CBE 29/04/2010
	//Disable all comparators
	DisableCN2;
	DisableCN4;
	DisableCN6;
	DisableCN8;
	DisableCN10;
	DisableCN11;
	PORTSetPinsAnalogIn(IOPORT_B, BIT_2 | BIT_4 | BIT_6 | BIT_8 | BIT_10 | BIT_11);	// Set AN pin as analog input.
	CloseADC10();														// ensure the ADC is off before setting the configuration
    ADC_Config();                                                       /* Configure ADC settings                           */
    EnableADC10();                                                      /* Enable the ADC                                   */
}
Пример #13
0
void AdcInit(unsigned char clock_select)
{
	unsigned int adc1con1 = (ADC_MODULE_OFF | 
                    		ADC_IDLE_STOP | 
                    		ADC_FORMAT_INTG | 
                    		ADC_CLK_AUTO |
                    		ADC_AUTO_SAMPLING_OFF);
	unsigned int adc1con2 = (ADC_VREF_EXT_AVSS | /*Used external 2.5v ref*/
		                    ADC_SCAN_ON | 
		                    ADC_INTR_7_CONV| 
		                    ADC_ALT_BUF_OFF |
							ADC_ALT_INPUT_OFF);
	unsigned int adc1con3 = (ADC_CONV_CLK_INTERNAL_RC | 
		                    ADC_SAMPLE_TIME_20 | 
		                    ADC_CONV_CLK_20Tcy);

	PMD1bits.ADC1MD = 0;    // See device errata
	CloseADC10();
	
	if (clock_select == ADC_CLOCK_SYSTEM_PLL)
	{
		 adc1con3 =  /*config3*/
                    (ADC_CONV_CLK_SYSTEM |
                    ADC_SAMPLE_TIME_31 | 
                    ADC_CONV_CLK_2Tcy);	
	}
	else if (clock_select == ADC_CLOCK_SYSTEM)
	{
		 adc1con3 =  /*config3*/
                    (ADC_CONV_CLK_SYSTEM |   
                    ADC_SAMPLE_TIME_31 | 
                    ADC_CONV_CLK_1Tcy);	
	}
	else; // Using rc source - already done

    // Configure ADC for RCOSC
     OpenADC10(adc1con1,adc1con2,adc1con3,
		                    /*configport_L*/
		                    (ADC_SELECT_L),
		                    /*configport_H*/
		                    (ADC_SELECT_H),
		                    /*configscan_L*/
		                    ((~ADC_SELECT_L)&0xfffe), /*We dont want to scan the Vref really - it will always be 1023*/
		                    /*configscan_H*/
		                    (~ADC_SELECT_H));
	EnableADC1;
}
Пример #14
0
void GetBatt(void)
{
	PMD1bits.ADC1MD = 0;    // See device errata
	CloseADC10();
    // Configure ADC for RCOSC
    OpenADC10(	/*config1*/
                (ADC_MODULE_OFF | 
                ADC_IDLE_STOP | 
                ADC_FORMAT_INTG | 
                ADC_CLK_AUTO |
                ADC_AUTO_SAMPLING_OFF), 
                /*config2*/
                (ADC_VREF_AVDD_AVSS | 
                ADC_SCAN_ON | 
                ADC_INTR_3_CONV| 
                ADC_ALT_BUF_OFF |
                ADC_ALT_INPUT_OFF), 
                /*config3*/
                (ADC_CONV_CLK_INTERNAL_RC |     /* INTERNAL RC */
                ADC_SAMPLE_TIME_20 | 
                ADC_CONV_CLK_20Tcy),
                /*configport_L*/
                (ADC_SELECT_L),
                /*configport_H*/
                (ADC_SELECT_H),
                /*configscan_L*/
                (~ADC_SELECT_L),
                /*configscan_H*/
                (~ADC_SELECT_H)
            );
 	EnableADC1;

	IFS0bits.AD1IF = 0;		// Clear interrupt flag
	AD1CON1bits.ASAM = 1;	// Begin auto sampling
	while (!IFS0bits.AD1IF);// Wait for the allotted number of conversions set by ADC_INTR_?_CONV in config2
	AD1CON1bits.ASAM = 0;	// Stop auto sampling
	while(!AD1CON1bits.DONE);// Wait for partially complete samples
	
    adcResult.batt = ReadADC10(ADC_INDEX_BATT); // Battery
//    adcResult.ldr  = ReadADC10(ADC_INDEX_LDR);  // LDR
//    adcResult.temp = ReadADC10(ADC_INDEX_TEMP); // Temp
	AD1CON1bits.ADON = 0;
	PMD1bits.ADC1MD = 1; // See device errata
}
Пример #15
0
void initPIC() {
    SYSTEMConfig(SYS_FREQ, SYS_CFG_ALL);

    initLEDs();
    initSerialNU32v2();

    // Setup and turn off electromagnets
    EMAG1 = 0;
    EMAG2 = 0;
    TRISEbits.TRISE7 = 0;
    TRISCbits.TRISC1 = 0;

    // Direction Output
    DIR = 1;
    TRISAbits.TRISA9 = 0;

    setup_counters();

    CloseADC10();
#define PARAM1  ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
#define PARAM2  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_16 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF
#define PARAM3  ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_31
#define PARAM4  ENABLE_AN0_ANA | ENABLE_AN1_ANA | ENABLE_AN2_ANA | ENABLE_AN3_ANA | ENABLE_AN5_ANA | ENABLE_AN15_ANA
    OpenADC10(PARAM1, PARAM2, PARAM3, PARAM4, 0);
    EnableADC10();

    // 20kHz PWM signal, duty from 0-1000, pin D3
    OpenTimer2(T2_ON | T2_PS_1_4, MAX_DUTY);
    OpenOC4(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0);
    SetDCOC4PWM(0);

    // 200 Hz ISR
    OpenTimer3(T3_ON | T3_PS_1_256, (6250/4 - 1));
    //OpenTimer3(T3_ON | T3_PS_1_256, (62500 - 1));
    mT3SetIntPriority(1);
    mT3ClearIntFlag();
    mT3IntEnable(1);

    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
    INTEnableSystemMultiVectoredInt();
}
void initADC()
{
    //portb as anaog input
    mPORTBSetPinsAnalogIn(BIT_3);

    // configure and enable the ADC
    CloseADC10(); // ensure the ADC is off before setting the configuration

    // define setup parameters for OpenADC10
    // Turn module on | output in integer | trigger mode auto | enable  autosample
#define PARAM1  ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON

    // define setup parameters for OpenADC10
    // ADC ref external    | disable offset test    | enable scan mode | perform 2 samples | use one buffer | use MUXA mode
    // note: to read X number of pins you must set ADC_SAMPLES_PER_INT_X
#define PARAM2  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_1 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF

    // define setup parameters for OpenADC10
    // 				  use ADC internal clock | set sample time
#define PARAM3  ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15

    // define setup parameters for OpenADC10
    // set AN3
#define PARAM4	ENABLE_AN3_ANA

    // define setup parameters for OpenADC10
    // do not assign channels to scan
#define PARAM5	SKIP_SCAN_AN0 | SKIP_SCAN_AN1 | SKIP_SCAN_AN2 | SKIP_SCAN_AN4 | SKIP_SCAN_AN5 | SKIP_SCAN_AN6 | SKIP_SCAN_AN7 | SKIP_SCAN_AN8 | SKIP_SCAN_AN9 | SKIP_SCAN_AN10 | SKIP_SCAN_AN11 | SKIP_SCAN_AN12 | SKIP_SCAN_AN13 | SKIP_SCAN_AN14 | SKIP_SCAN_AN15

    // use ground as neg ref for A
    SetChanADC10(ADC_CH0_NEG_SAMPLEA_NVREF); // use ground as the negative reference
    OpenADC10(PARAM1, PARAM2, PARAM3, PARAM4, PARAM5); // configure ADC using parameter define above

    EnableADC10(); // Enable the ADCd

    while (!mAD1GetIntFlag())
    {
    } // wait for the first conversion to complete so there will be valid data in ADC result registers
}
Пример #17
0
		virtual void setup_analog_in()		{
			CloseADC10(); // ERROR: Only open 2 channels?
			SetChanADC10(ADC_CH0_NEG_SAMPLEA_NVREF|ADC_CH0_POS_SAMPLEA_AN0);

			PORTSetPinsAnalogIn(ltr(), num());

			enabled_ADCs |= (typeof(enabled_ADCs)) (1 << bit); // PIC32 SPECIFIC
			uint32_t count_enabled = __builtin_popcount(enabled_ADCs); // 32bit cuz we bitshift below

			/* 1: Turn module on | ouput in integer | trigger mode auto | enable autosample
			 * 2: ADC ref external    | disable offset test    | disable scan mode | perform 2 samples | use dual buffers | use alternate mode
			 * 3: use ADC internal clock | set sample time
			 * 4: enable all ANX not in use
			 * 5: do not assign other channels to scan */
			#define PARAM1	ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
			#define PARAM2	ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | (count_enabled << _AD1CON2_SMPI_POSITION) | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF // use Channel A only
			#define PARAM3	ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15
			#define PARAM4	ENABLE_AN0_ANA | ENABLE_AN1_ANA
			#define PARAM5	SKIP_SCAN_ALL

			OpenADC10( PARAM1, PARAM2, PARAM3, enabled_ADCs, ~enabled_ADCs ); // setup ADC
			EnableADC10();
		}
Пример #18
0
/*
 * Set up ADC10 to read Cerebot battery monitor AN8.
 * If you are having problems, verify jumper JP4 is on.
 */
void init_battery_monitor()
{
   // configure and enable the ADC
   // ensure the ADC is off before setting the configuration
   CloseADC10();

   // use ground as neg ref for A | use AN8 for input A
   // configure to sample AN8
   SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN8);

   // Configure the ADC
   // 16 bit integer, auto convert, auto sampling select.
   //
   // Use AVDD and AVSS for reference, disable calibration, do not scan,
   // interrupt at completion of every 2nd sample, use 2 8 word buffers,
   // alternate between muxes.
   //
   // Use internal clock, set sample time
   //
   // Do not assign channels to scan
   //
   // Set AN8 as analog input
   OpenADC10(
      ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON,
      ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF |
           ADC_SAMPLES_PER_INT_2 | ADC_ALT_BUF_ON | ADC_ALT_INPUT_ON,
      ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15,
      ENABLE_AN8_ANA,
      SKIP_SCAN_ALL);

   // Enable the ADC
   EnableADC10();

   // Wait for the first conversion to complete so there will be vaild
   // data in ADC result registers
   while ( ! mAD1GetIntFlag() );
}
Пример #19
0
int main(void) {
	timesec=0;
	// set PIC32 to max computing power
	SYSTEMConfig(SYS_FREQ, SYS_CFG_ALL);
	INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
	INTEnableSystemMultiVectoredInt();   
    initLEDs(); 	
    LED0 = 1;
    LED1 = 1;
    initSerialNU32v2();
	setup_counters();

  	CloseADC10();
  	#define PARAM1  ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
  	#define PARAM2  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_16 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF
  	#define PARAM3  ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_31
  	#define PARAM4  ENABLE_AN0_ANA | ENABLE_AN1_ANA | ENABLE_AN2_ANA | ENABLE_AN3_ANA | ENABLE_AN5_ANA | ENABLE_AN15_ANA
  	OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4,0);
  	EnableADC10();


    // Setup and turn off electromagnets
    EMAG1 = 0; EMAG2 = 0; 
    TRISEbits.TRISE7 = 0; TRISCbits.TRISC1 = 0;
	//Direction Output
	DIR = 1;
  	TRISAbits.TRISA9 = 0;
	//g-select Outputs
	GSEL1 = 0; GSEL2 = 0;
  	TRISEbits.TRISE2 = 0; TRISCbits.TRISC13= 0;
  	//0g Inputs
  	TRISAbits.TRISA0 = 1;
  	TRISAbits.TRISA4 = 1;

  	// 20kHz PWM signal, duty from 0-1000, pin D3
  	OpenTimer2(T2_ON | T2_PS_1_4, 1000);
  	OpenOC4(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0);
  	HBridgeDuty = 0;
  	SetDCOC4PWM(HBridgeDuty);

    // 20Hz ISR
    OpenTimer3(T3_ON | T3_PS_1_256, 15625);
    mT3SetIntPriority(1);
    mT3ClearIntFlag();
    mT3IntEnable(1);


	while(1) {
		if(start){
			EMAG2=0;
			SetDCOC4PWM(100);
			delaysec(delay1);
			SetDCOC4PWM(1000);		
			delaysec(delay2);
			SetDCOC4PWM(500);
			delaysec(delay3);
			EMAG2=1;
			SetDCOC4PWM(0);	

//			EMAG1=0;
//			SetDCOC4PWM(900);
//			DIR = 0;
//			delaysec(delay1);
//			SetDCOC4PWM(0);
//			delaysec(delay2);
//			SetDCOC4PWM(700);
//			delaysec(delay1);
//			SetDCOC4PWM(1000);
//			EMAG1=1;

			start=0;
		}
	}
}
Пример #20
0
/****************************************************************************
  Function:
    static void InitializeBoard(void)

  Description:
    This routine initializes the hardware.  It is a generic initialization
    routine for many of the Microchip development boards, using definitions
    in HardwareProfile.h to determine specific initialization.

  Precondition:
    None

  Parameters:
    None - None

  Returns:
    None

  Remarks:
    None
  ***************************************************************************/
static void InitializeBoard(void)
{
    // Note: WiFi Module hardware Initialization handled by StackInit() Library Routine

    // Enable multi-vectored interrupts
    INTEnableSystemMultiVectoredInt();

    // Enable optimal performance
    SYSTEMConfigPerformance(GetSystemClock());

    // Use 1:1 CPU Core:Peripheral clocks
    mOSCSetPBDIV(OSC_PB_DIV_1);     

    // Disable JTAG port so we get our I/O pins back, but first
    // wait 50ms so if you want to reprogram the part with
    // JTAG, you'll still have a tiny window before JTAG goes away.
    // The PIC32 Starter Kit debuggers use JTAG and therefore must not
    // disable JTAG.
    DelayMs(50);
    DDPCONbits.JTAGEN = 0;

    // UART line configuration

    UARTConfigure(SENSOR_UART, UART_ENABLE_PINS_TX_RX_ONLY);
    UARTSetFifoMode(SENSOR_UART, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
    UARTSetLineControl(SENSOR_UART, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
    UARTSetDataRate(SENSOR_UART, GetPeripheralClock(), 9600);
    UARTEnable(SENSOR_UART, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));


    UARTConfigure(SENSOR_UART2, UART_ENABLE_PINS_TX_RX_ONLY);
    UARTSetFifoMode(SENSOR_UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
    UARTSetLineControl(SENSOR_UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
    UARTSetDataRate(SENSOR_UART2, GetPeripheralClock(), 9600);
    UARTEnable(SENSOR_UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));

    // ADC configuration

    // Define setup parameters for OpenADC10 function
    // Attention, the sample number need to be changed for multi sensor use, not 1
// Turn module on | Ouput in integer format | Trigger mode auto | Disable autosample    , manually controlled
#define config1     ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON | ADC_MODULE_ON | ADC_IDLE_STOP
// ADC ref external | Disable offset test | Disable scan mode | Perform 1 samples | Use dual buffers | Use alternate mode
#define config2     ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_2 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF
// Use ADC internal clock | Set sample time
#define config3     ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_31
// Do not assign channels to scan
#define configscan  ~(ENABLE_AN2_ANA | ENABLE_AN3_ANA)
  // Define the port for ADC, need to be modified for practical prox sensors
 #define configport  ENABLE_AN2_ANA | ENABLE_AN3_ANA

    CloseADC10();
    // here use on-board potentiometer for testing, change pin configuration when for actually prox sensor use
    SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF);
    // Configure ADC using the parameters defined above
    OpenADC10( config1, config2, config3, configport, configscan );

    EnableADC10(); // Enable the ADC

    // LEDs
    LEDS_OFF();
    LED0_TRIS = 0;
    LED1_TRIS = 0;
    LED2_TRIS = 0;

    // Push Button
    SW0_TRIS = 1;

        // MPU 9150 I2C Init and chip init
      Mpu_I2c_Init(I2C_NUM);      // config the I2C module on PIC32
        // wait at least 100 ms for sensor chip warm up, based on 40Mhz sysclk 4M times
     DelayMs(100);
    if (Mpu_Init(I2C_NUM)==I2C_ERROR)         // Initialize the 9150 chip
       // UARTTxBuffer("Sensor is not connected",strlen("Sensor is not connected"));
     LEDS_ON();
}
Пример #21
0
void GP2D12_Init(){
    // Intitialize distance!
    distance = 0.0;

    // configure and enable the ADC
    CloseADC10();	// ensure the ADC is off before setting the configuration

    // define setup parameters for OpenADC10
    // 				Turn module on | ouput in integer | trigger mode auto | enable autosample
    DWORD PARAM1 = ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON;

    // define setup parameters for OpenADC10
    //		    ADC ref external    | disable offset test    | disable scan mode | perform 2 samples | use dual buffers | use alternate mode
    DWORD PARAM2 =  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_1 | ADC_BUF_8 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF;

    // define setup parameters for OpenADC10
    //				  use ADC internal clock | set sample time
    DWORD PARAM3 = ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_1 | (0x00FF << _AD1CON3_ADCS_POSITION);


    // define setup parameters for OpenADC10
    //               set AN4 and AN5 as analog inputs
    DWORD PARAM4 = 0;
    DWORD PARAM6 = 0;
    //ENABLE_AN4_ANA | ENABLE_AN5_ANA
    switch(GP2D12_ADC_CHANNEL){
        case 0: PARAM4 = ENABLE_AN0_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN0; break;
        case 1: PARAM4 = ENABLE_AN1_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN1; break;
        case 2: PARAM4 = ENABLE_AN2_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN2; break;
        case 3: PARAM4 = ENABLE_AN3_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN3; break;
        case 4: PARAM4 = ENABLE_AN4_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4; break;
        case 5: PARAM4 = ENABLE_AN5_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN5; break;
        case 6: PARAM4 = ENABLE_AN6_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN6; break;
        case 7: PARAM4 = ENABLE_AN7_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN7; break;
        case 8: PARAM4 = ENABLE_AN8_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN8; break;
        case 9: PARAM4 = ENABLE_AN9_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN9; break;
        case 10: PARAM4 = ENABLE_AN10_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN10; break;
        case 11: PARAM4 = ENABLE_AN11_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN11; break;
        case 12: PARAM4 = ENABLE_AN12_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN12; break;
        case 13: PARAM4 = ENABLE_AN13_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN13; break;
        case 14: PARAM4 = ENABLE_AN14_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN14; break;
        case 15: PARAM4 = ENABLE_AN15_ANA, PARAM6 = ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN15; break;
        default: PARAM4 = ENABLE_ALL_DIG; break;
    }

    if(PARAM4 == ENABLE_ALL_DIG)
        return;

    // define setup parameters for OpenADC10
    // do not assign channels to scan
    DWORD PARAM5 = SKIP_SCAN_ALL;

    // use ground as neg ref for A | use AN4 for input A      |  use ground as neg ref for A | use AN5 for input B

    // configure to sample AN4 & AN5
    SetChanADC10(PARAM6); // configure to sample AN4 & AN5
    OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using the parameters defined above

    //ConfigIntADC10(ADC_INT_PRI_3 | ADC_INT_SUB_PRI_2 | ADC_INT_ON);
    ConfigIntADC10(ADC_INT_OFF);
    EnableADC10(); // Enable the ADC

}
Пример #22
0
//*********************************************************************
int analogRead(uint8_t pin)
{
int				analogValue;
unsigned int	offset;	// buffer offset to point to the base of the idle buffer
unsigned int	param4;
uint8_t			channelNumber;

	//*	in most cases (except the uno board) this will be a 1 to 1 mapping
	channelNumber	=	analogInPinToBit(pin);


	//*	May 1, 2011	Gene Apperson of Digitlent sent me PIC32MX5XX-6XX-7XX Errata.pdf
	//*	item #26 documents a condition that I/O pins take time if previously set to outputs
	
	


	//*	first attempt, set all to 0
//	AD1PCFG	=	0;

	// configure and enable the ADC
	CloseADC10();	// ensure the ADC is off before setting the configuration

	// define setup parameters for OpenADC10
	//				 Turn module on | ouput in integer | trigger mode auto | enable autosample
	#define PARAM1	ADC_FORMAT_INTG16 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON

	// define setup parameters for OpenADC10
	//				 ADC ref external	| disable offset test	| disable scan mode | perform 2 samples | use dual buffers | use alternate mode
	#define PARAM2	ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_1 | ADC_ALT_BUF_ON 

	// define setup parameters for OpenADC10
	//					use ADC internal clock | set sample time
	#define PARAM3	ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15


	// define setup parameters for OpenADC10
	//				set AN4 and AN5 as analog inputs
	param4	=	channelNumber;
	
	// define setup parameters for OpenADC10
	// do not assign channels to scan
	#define PARAM5	SKIP_SCAN_ALL
	
	// use ground as neg ref for A | use AN4 for input A	  | use ground as neg ref for A | use AN5 for input B

//	SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4 |  ADC_CH0_NEG_SAMPLEB_NVREF | ADC_CH0_POS_SAMPLEB_AN5); // configure to sample AN4 & AN5
//	SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN0 |  ADC_CH0_NEG_SAMPLEB_NVREF | ADC_CH0_POS_SAMPLEB_AN1); // configure to sample AN4 & AN5
//	OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using the parameters defined above

	switch(channelNumber)
	{
		case 0:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN0);
			break;
			
		case 1:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN1);
			break;

		case 2:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN2);
			break;

		case 3:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN3);
			break;

		case 4:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4);
			break;

		case 5:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN5);
			break;

		case 6:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN6);
			break;

		case 7:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN7);
			break;

		case 8:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN8);
			break;

		case 9:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN9);
			break;

		case 10:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN10);
			break;

		case 11:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN11);
			break;

		case 12:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN12);
			break;

		case 13:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN13);
			break;

		case 14:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN14);
			break;

		case 15:
			SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN15);
			break;
	}

	
	// configure to sample what ever pin was specified
	
	OpenADC10( PARAM1, PARAM2, PARAM3, param4, PARAM5 ); // configure ADC using the parameters defined above

	EnableADC10(); // Enable the ADC

	//*	A delay is needed for the the ADC start up time
	//*	this value started out at 1 millisecond, I dont know how long it needs to be
	//*	99 uSecs will give us the same approximate sampling rate as the AVR chip
//	delay(1);	
	delayMicroseconds(99);

	while ( ! mAD1GetIntFlag() ) 
	{ 
		// wait for the first conversion to complete so there will be vaild data in ADC result registers
	}

	analogValue	=	ReadADC10(0);
	
	mAD1ClearIntFlag();

	return (analogValue);
}
Пример #23
0
// === Main  ======================================================
void main(void) {
    //SYSTEMConfigPerformance(PBCLK);
    ANSELA = 0; ANSELB = 0; 
    // === config threads ==========
    // turns OFF UART support and debugger pin, unless defines are set
    PT_setup();
    // === setup system wide interrupts  ========
    INTEnableSystemMultiVectoredInt();
    CloseADC10();	// ensure the ADC is off before setting the configuration
    #define PARAM1  ADC_FORMAT_INTG16 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_OFF //
    // define setup parameters for OpenADC10
    // ADC ref external  | disable offset test | disable scan mode | do 1 sample | use single buf | alternate mode off
    #define PARAM2  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_1 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF
    // Define setup parameters for OpenADC10
    // use peripherial bus clock | set sample time | set ADC clock divider
    // ADC_CONV_CLK_Tcy2 means divide CLK_PB by 2 (max speed)
    // ADC_SAMPLE_TIME_5 seems to work with a source resistance < 1kohm
    #define PARAM3 ADC_CONV_CLK_PB | ADC_SAMPLE_TIME_5 | ADC_CONV_CLK_Tcy2 //ADC_SAMPLE_TIME_15| ADC_CONV_CLK_Tcy2
    // define setup parameters for OpenADC10
    // set AN11 and  as analog inputs
    #define PARAM4	ENABLE_AN11_ANA // pin 24
    // define setup parameters for OpenADC10
    // do not assign channels to scan
    #define PARAM5	SKIP_SCAN_ALL
    // use ground as neg ref for A | use AN11 for input A     
    // configure to sample AN11 
    SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN11 ); // configure to sample AN4 
    OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using the parameters defined above
    EnableADC10(); // Enable the ADC      
    OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_1, 400); 
    mPORTAClearBits(BIT_0 );		//Clear bits to ensure light is off.
    mPORTASetPinsDigitalOut(BIT_0 );    //Set port as output
    int CVRCON_setup;
    // set up the Vref pin and use as a DAC
    // enable module| eanble output | use low range output | use internal reference | desired step
    CVREFOpen( CVREF_ENABLE | CVREF_OUTPUT_ENABLE | CVREF_RANGE_LOW | CVREF_SOURCE_AVDD | CVREF_STEP_0 );
    // And read back setup from CVRCON for speed later
    // 0x8060 is enabled with output enabled, Vdd ref, and 0-0.6(Vdd) range
    CVRCON_setup = CVRCON; //CVRCON = 0x8060
	// Open the desired DMA channel.
	// We enable the AUTO option, we'll keep repeating the same transfer over and over.
	DmaChnOpen(dmaChn, 0, DMA_OPEN_AUTO);
	// set the transfer parameters: source & destination address, source & destination size, number of bytes per event
    // Setting the last parameter to one makes the DMA output one byte/interrut
    //DmaChnSetTxfer(dmaChn, sine_table, (void*) &CVRCON, sizeof(sine_table), 1, 1);
	// set the transfer event control: what event is to start the DMA transfer
    // In this case, timer2 
	DmaChnSetEventControl(dmaChn,  DMA_EV_START_IRQ(_TIMER_2_IRQ));
	// once we configured the DMA channel we can enable it
	// now it's ready and waiting for an event to occur...
	//DmaChnEnable(dmaChn);
  
    int i;
    for(i=0; i <256; i++){
        ball_scored[i] = 0x60|((unsigned char)((float)255/2*(sin(6.2832*(((float)i)/(float)256))+1))) ;
        //ball_lost[i]
       // game_over
    }

        
    // init the threads
    PT_INIT(&pt_timer);
    PT_INIT(&pt_adc);
    PT_INIT(&pt_launch);
    PT_INIT(&pt_anim);
    // init the display
    tft_init_hw();
    tft_begin();
    tft_fillScreen(ILI9340_BLACK);
    tft_setRotation(1); // Use tft_setRotation(1) for 320x240
    while (1){
        PT_SCHEDULE(protothread_timer(&pt_timer));
        PT_SCHEDULE(protothread_adc(&pt_adc));
        PT_SCHEDULE(protothread_launch_balls(&pt_launch));
        PT_SCHEDULE(protothread_anim_balls(&pt_anim));
    }
  } // main
Пример #24
0
Файл: AD.c Проект: mgsit/Hand
/****************************************************************************
 Function
    AD_End

 Parameters
    None

 Returns
     None

 Description
    disables the A/D subsystem and release the pins used
 Notes
     None.

 Author
 Max Dunne, 2011.12.10
 ****************************************************************************/
void AD_End(void) {
    UsedPins = 0;
    PinCount = 0;
    CloseADC10();
    AD1PCFG = 0xFF;
}
int main(void)
{
  UINT8   strBuf[bufLen];
  UINT8   bank = 0;
  #define NSENSORSPERBANK 4
  UINT32  cPot, cSensor[2][NSENSORSPERBANK];
  UINT8   i;
  #define NSENSORSINSTALLED 5
  UINT16 min_levels[NSENSORSINSTALLED], max_levels[NSENSORSINSTALLED], discriminators[NSENSORSINSTALLED];
  #define DISCRIMINATORLEVELPCT ( 75 )
  int steps_remaining_in_evaluation = 0;

  BOOL noteIsOn[NSENSORSINSTALLED] = {0, 0, 0, 0, 0};
  UINT32 sensorValue;

  SYSTEMConfigPerformance (FCY);
 
  LED_TRIS = 0;
  LED_MIDI_TRIS = 0;
  BTN_TRIS = 1;

  STEPPER0_TRIS = 0;
  STEPPER1_TRIS = 0;
  STEPPER2_TRIS = 0;
  STEPPER3_TRIS = 0;

  PWR_BANK0_TRIS = 0;  // disable both banks to start
  PWR_BANK1_TRIS = 1;

  UART2_RX_PPS_REG = UART2_RX_PPS_ITM;
  UART2_TX_PPS_REG = UART2_TX_PPS_ITM;

  UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY);
  UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
  UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
  if (DO_MIDI) {
    UARTSetDataRate(UART2, FPB, 31500);
  } else {
    UARTSetDataRate(UART2, FPB, 115200);
  }
  UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));

  // timer interrupt stuff...

  // configure the core timer roll-over rate
  // the internal source is the Peripheral Clock
  // timer_preset = SYSCLK_freq / pb_div / additional prescale / desired_ticks_per_sec


  UINT16 timer_preset = (clock_interrupt_period_us * (FPB / 1000000)) / 8;
  OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_8, timer_preset);
 
  // set up timer interrupt with a priority of 2 and sub-priority of 0
  ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);

  // enable multi-vector interrupts
  INTEnableSystemMultiVectoredInt();


  // configure and enable the ADC
  
  CloseADC10();	// ensure the ADC is off before setting the configuration
  
  /*
    Notes from ref chapter
      must set the TRIS bits for these channels (1 (desired) by default)
      AD1CON1
        want form 32-bit integer (0b100)
        we set the SAMP bit to begin sampling --no--
          the ASAM bit will begin autosampling
          combine this with CLRASAM which will clear ASAM after one set of samples
          conversion should be auto after that
        autoconvert when sampling has concluded (SSRC = 0b111)
        Tad min is 65ns, which is 2.6 PBclock cycles
        Tad min is 83.33ns, which is 3.3 PBclock cycles
      AD1CON2
        CSCNA on enables scanning of inputs according to AD1CSSL
        maybe BUFM 2 8-word buffers rather than 1 16-word buffer
        ALTS alternates between mux A and mux B
      AD1CON3
        ADRC use peripheral bus clock ADC_CONV_CLK_PB
        TAD: use ADC clock divisor of 4 to give TAD of 100ns to exceed min of 83.33
        Sample time (SAMC): use 2 TADs to provide 200ns sampling to exceed min of 132
              
        New strategy: set the bit to power the necessary bank, wait 200 us,
        and then kick off a sample.
        
      AD1CHS
        CH0SA and CH0SB not used when scanning channels; CH0NA and CH0NB *are* used
      AD1PCFG
        zero bits to configure channels as analog; set to zero on reset
      AD1CSSL
        one bits to select channel for scan
        
      Will want to scan all channels on one bank, then interrupt to switch banks
      Protect user code by writing valid stuff during interrupt to user area
      
      
      
      settling time after switching is say 200 us (78us to 0.63 rise)
      
  */
  AD1CON1 =   ADC_FORMAT_INTG32           // output in integer
            | ADC_CLK_AUTO                // conversion begins with clock
            | ADC_AUTO_SAMPLING_OFF       // don't yet start autosample
          ;

  AD1CON2 =   ADC_VREF_AVDD_AVSS          // ADC ref internal
            | ADC_OFFSET_CAL_DISABLE      // disable offset test
            | ADC_SCAN_ON                 // enable scan mode (CSCNA)
            | ADC_SAMPLES_PER_INT_5 
            | ADC_ALT_BUF_OFF             // use single buffer
            | ADC_ALT_INPUT_OFF           // use MUX A only
          ;
 
  AD1CON3 =   ADC_SAMPLE_TIME_2           // use 2 TADs for sampling
            | ADC_CONV_CLK_PB             // use PBCLK
            | ADC_CONV_CLK_Tcy            // 4 PBCLK cycles, so TAD is 100ns
          ;

  // AD1CHS
  //          CH0NA negative input select for MUX A is Vref-
  //          ASAM will begin a sample sequence
  //          CLRASAM will stop autosampling after completing one sequence
  //          begin conversions automatically
  SetChanADC10 (ADC_CH0_NEG_SAMPLEA_NVREF);
  
  // AD1PCFG
  
  // these are in the order in which they will appear in BUF0
  AD1CSSL =   LS0_ADC_ITM 
            | LS1_ADC_ITM 
            | LS2_ADC_ITM 
            | LS3_ADC_ITM 
            | POT_ADC_ITM
          ;  
  
  EnableADC10();

  while(1) {

    // enableADC is set periodically (ADC_INTERVAL_MS) by an interrupt service routine
    if (enableADC) {
      
      // NOTE: the interrupt routine set AD1CON1bits.SAMP = 1;
      // use AN0, pin 2
      
      // wait for the conversions to complete
      // so there will be vaild data in ADC result registers
      while ( ! AD1CON1bits.DONE );
  
      for (i = 0; i < NSENSORSPERBANK; i++) {
        cSensor[bank][i] = ReadADC10(i);
      }
      cPot = POT_ADC_VAL;

      // current setup has 5 sensors: 2 in bank 0, 3 in bank 1
      if (1 && enablePrint && !DO_MIDI) {
        snprintf (strBuf, bufLen, 
          "%5d <-> %5d | %5d <-> %5d | %5d <-> %5d | %5d <-> %5d | %5d <-> %5d || %5d %3.2f\n",
          cSensor[0][0], discriminators[0],
          cSensor[0][1], discriminators[1],
          cSensor[1][0], discriminators[2],
          cSensor[1][1], discriminators[3],
          cSensor[1][2], discriminators[4],
          cPot,
          rpm
         );
        SendDataBuffer (strBuf, strlen(strBuf));
        enablePrint = 0;
      }
      smoothedPotValue = smoothedPotValue * (1.0 - POTEWMAVALUE)
                        + cPot * POTEWMAVALUE;
      cPot = floor(smoothedPotValue + 0.5);
    

#if (0)
      // direction not needed for BOOM32, but coded for testing
      if (cPot > 512) {
        stepDirection = 1;
      } else {
        stepDirection = -1;
      }

      // NOTE: my stepper asks for 12V and uses more than 200mA, which is the
      //   limit of my bench power supply...
      // That may be the limit of how fast we can step.
      
      // interval is least at extremes of pot travel
      float scaledValue = abs(cPot - 512.0) / 512.0;  // 1 at ends to 0 in middle
      scaledValue = 1.0 - sqrt(scaledValue);                 // 0 at ends to inf in middle
      int temp = floor(scaledValue * 1000000 + 0.5);
      #define min_stepper_interval_us 1000
      #define max_stepper_interval_us 125000
      stepper_interval_us = min_stepper_interval_us +
                    scaledValue * (max_stepper_interval_us-min_stepper_interval_us);

#else
      // desired us/phase:
      //                / 40-160 bpm
      //                * 16-32 beats per rev
      //                      ==> 40/32=1.25 - 160/16=10 RPM
      //                / 200 steps/rev
      //                / 4 coils / step
      //                / 2 phases/coil
      //                * 60 sec/min
      //                * 1e6 us/sec
      //                      --> factor = 15e6/400 = 3.75e4
      //                ==> is 3.75e3 (fast) - 6.0e4
      // is 7500 to 60000 us/phase
      #define min_stepper_interval_us   3750
      #define max_stepper_interval_us  30000
      stepper_interval_us = min_stepper_interval_us
        + ((1023 - cPot) *
          (max_stepper_interval_us - min_stepper_interval_us)) / 1024;
      rpm = ( 60 * 1e6 / stepper_interval_us) / PHASES_PER_REVOLUTION;
#endif

      steps_remaining_in_evaluation--;
      if (steps_remaining_in_evaluation <= 0) {
        for (i = 0; i < NSENSORSINSTALLED; i++) {
          discriminators[i] = min_levels[i] + (max_levels[i] - min_levels[i]) * DISCRIMINATORLEVELPCT / 100;
          min_levels[i] = 1024;
          max_levels[i] = 0;
        }
        // SendDataBuffer ("\n\n", 2);
        steps_remaining_in_evaluation = ADC_READINGS_PER_LEVEL_REEVALUATION;
      }
      
      // make determinations about what's on and what's off
      for (i = 0; i < NSENSORSINSTALLED; i++) {
        if (i < 2) {
          sensorValue = cSensor[0][i];
        } else {
          sensorValue = cSensor[1][i - 2];
        }
        if (sensorValue < min_levels[i]) min_levels[i] = sensorValue;
        if (sensorValue > max_levels[i]) max_levels[i] = sensorValue;
        
        BOOL currentValue = sensorValue < discriminators[i];
        if (noteIsOn[i] != currentValue) {
          if (currentValue) {
            // turn note on
            if (DO_MIDI) {
              snprintf (strBuf, bufLen, "%c%c%c", (char) 0x99, (char) i, (char) 64);
            } else if (NOTE_NOTES) {
              snprintf (strBuf, bufLen, "NoteOn (%d)\n", i);
            }
          } else {
            // turn note off
            if (DO_MIDI) {
              snprintf (strBuf, bufLen, "%c%c%c", (char) 0x89, (char) i, (char) 64);
            } else if (NOTE_NOTES) {
              snprintf (strBuf, bufLen, "NoteOff (%d)\n", i);
            }
          }
          if (i == 0) LED_MIDI = currentValue;
          SendDataBuffer (strBuf, strlen(strBuf));
          noteIsOn[i] = currentValue;
        }
      }

      if (0) {
        snprintf  (strBuf, bufLen, "%8d : %3d %3d : %3d %3d : %3d %3d : %3d %3d : %3d %3d :\n",
                    steps_remaining_in_evaluation,
                    min_levels[0], max_levels[0],
                    min_levels[1], max_levels[1],
                    min_levels[2], max_levels[2],
                    min_levels[3], max_levels[3],
                    min_levels[4], max_levels[4]
                  );
        SendDataBuffer (strBuf, strlen(strBuf));
      }


      
      // switch banks
      bank = 1 - bank;
      if (bank == 0) {
        PWR_BANK1 = 0;
        PWR_BANK1_TRIS = 1;
        PWR_BANK0 = 1;
        PWR_BANK0_TRIS = 0;
      } else {
        PWR_BANK0 = 0;
        PWR_BANK0_TRIS = 1;
        PWR_BANK1 = 1;
        PWR_BANK1_TRIS = 0;
      }

      AD1CON1bits.DONE = 0;
      enableADC = 0;

    }  // ADC enabled

    if (enableStep) {
      theStep += stepDirection;
      if (theStep >= PHASES_PER_STEP) theStep = 0;
      if (theStep < 0) theStep = PHASES_PER_STEP - 1;

      STEPPER0 = stepSequence[theStep][0];
      STEPPER1 = stepSequence[theStep][1];
      STEPPER2 = stepSequence[theStep][2];
      STEPPER3 = stepSequence[theStep][3];

      enableStep = 0;
    }

    if (enablePrint) {
//      snprintf (strBuf, bufLen, "  stepper_interval_ms = %d\n", stepper_interval_ms);
//      SendDataBuffer (strBuf, strlen(strBuf));
//      enablePrint = 0;
    }
  }  // infinite loop

  return -1;
}
Пример #26
0
int main(void)
{
	/******************* Setup *****************************/
	mPORTBSetPinsAnalogIn(0xFFFF);             //Enable all analog
	//mPORTDSetPinsDigitalOut(BIT_0 | BIT_1 | BIT_8);
	
	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
	CloseADC10();
	SetChanADC10(INITCH);
	OpenADC10(CONFIG1, CONFIG2, CONFIG3, CFGPORT, CFGSCAN);
	ConfigIntADC10(CFGINT);
	EnableADC10();
	
	//Initialize the DB_UTILS IO channel
	DBINIT();

	// Display the introduction
	DBPRINTF("Welcome to the PIC32 Starter Kit Tutorial.\n");
	DBPRINTF("The build date and time is (" __DATE__ "," __TIME__ ")\n");

	
	/********************* Declarations *********************/
	
	int damper = 0; //ch1 binary read
	int damperchnum = 0; //damper adc number
	int hammerchnum = 1; //hammer adc number
	int samplesize = 1000; 
	
	int hammerdata[10][samplesize]; //collection of hammer values
	int damperdata[10][samplesize]; //collection of damper values
	
	int index = 0; //index of hammerdata and damperdata arrays
	int collection[2] = {0,0}; 
	int count = 0;
	
	/******************** Initialize Arrays ******************/
	int x; 
	int y;
	for( x = 0; x < 10; x++)
	{
		for( y = 0; y < samplesize; y++)
		{
			damperdata[x][y] = 0;
		}
	}
	
	/************************ Stuff to do *********************/
	while (count < 4){  
		damper = ReadADC10(damperchnum);
		if(damper > 100)
		{			
			//store data
			collection[0] = collection[1]; //we are collecting data, falling edge detection
			collection[1] = 1;
			if(index<samplesize)
			{
				hammerdata[count][index] = ReadADC10(hammerchnum);
				damperdata[count][index] = damper;
				index++;
				//DBPRINTF("Damper: %d \n", damperdata[count][index-1]);
			}
		}else{
			collection[0] = collection[1]; 
			collection[1] = 0;
		}
		if(collection[1] < collection[0]) //rising edge detection
		{
			DBPRINTF("Count: %d \n", count);
			count++;
			index = 0;
		}
	}
	
	int i;
	
	/******************** Output Memory at End **********************/
	for( i = 0; i < 1000; i++)
	{
		DBPRINTF("Damper data: %d ", damperdata[1][i]);
		DBPRINTF("\n");
	} 	

	return 0;
}
int main(void)
{
	unsigned int temp;

	// Configure the device for maximum performance but do not change the PBDIV
	// Given the options, this function will change the flash wait states, RAM
	// wait state and enable prefetch cache but will not change the PBDIV.
	// The PBDIV value is already set via the pragma FPBDIV option above..
	SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

/*PORT CONFIGURATION for UART*/
	// Blinky LED for Uart activity
	//mPORTAClearBits(BIT_7);
	//mPORTASetPinsDigitalOut(BIT_7);

// PINS used for the buttons
    PORTSetPinsDigitalIn(IOPORT_B, BIT_2 | BIT_3 | BIT_4);
	#define CONFIG          (CN_ON | CN_IDLE_CON)
	#define INTERRUPT       (CHANGE_INT_ON | CHANGE_INT_PRI_2)
	mCNOpen(CONFIG, CN4_ENABLE | CN5_ENABLE | CN6_ENABLE,
			CN4_PULLUP_ENABLE | CN5_PULLUP_ENABLE | CN6_PULLUP_ENABLE);
	temp = mPORTBRead();

//Analog input
	CloseADC10();
	#define PARAM1 ADC_MODULE_ON | ADC_FORMAT_INTG32 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
	#define PARAM2 ADC_VREF_AVDD_AVSS | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_2 | ADC_BUF_16 | ADC_ALT_INPUT_OFF
	#define PARAM3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_5
	#define PARAM4	ENABLE_AN0_ANA | ENABLE_AN1_ANA
	#define PARAM5	SKIP_SCAN_AN2 | SKIP_SCAN_AN3 | SKIP_SCAN_AN4 | SKIP_SCAN_AN5 | SKIP_SCAN_AN6 | SKIP_SCAN_AN7 | SKIP_SCAN_AN8 | SKIP_SCAN_AN9 | SKIP_SCAN_AN10 | SKIP_SCAN_AN11 | SKIP_SCAN_AN12 | SKIP_SCAN_AN13 | SKIP_SCAN_AN14 | SKIP_SCAN_AN15
	SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN0);
	OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 );
	EnableADC10();

//PORT D for motors
	mPORTDClearBits(BIT_0 | BIT_1 | BIT_2 | BIT_3 |
					BIT_4 | BIT_5 | BIT_6 | BIT_7 |
					BIT_8 | BIT_9 | BIT_10 | BIT_11 |
					BIT_12 | BIT_13 | BIT_14 | BIT_15); 		// Turn off PORTD on startup.
	mPORTDSetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_3 |
					BIT_4 | BIT_5 | BIT_6 | BIT_7 |
					BIT_8 | BIT_9 | BIT_10 | BIT_11 |
					BIT_12 | BIT_13 | BIT_14 | BIT_15);	// Make PORTD output.


	// Explorer-16 uses UART2 to connect to the PC.
	// This initialization assumes 36MHz Fpb clock. If it changes,
	// you will have to modify baud rate initializer.
    UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY);
    UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
    UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
    UARTSetDataRate(UART2, GetPeripheralClock(), DESIRED_BAUDRATE);
    UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));

	// Configure UART2 RX Interrupt
	INTEnable(INT_SOURCE_UART_RX(UART2), INT_ENABLED);
    INTSetVectorPriority(INT_VECTOR_UART(UART2), INT_PRIORITY_LEVEL_2);
    INTSetVectorSubPriority(INT_VECTOR_UART(UART2), INT_SUB_PRIORITY_LEVEL_0);


	// Congifure Change/Notice Interrupt Flag
	ConfigIntCN(INTERRUPT);

    // configure for multi-vectored mode
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);

    // enable interrupts
    INTEnableInterrupts();


	WriteString("*** UART Interrupt-driven Example ***\r\n");

	unsigned int channel1, channel2;
	unsigned int motor1Time, motor2Time;
	// Let interrupt handler do the work
	while (1) {
		
		while ( ! mAD1GetIntFlag() );

		channel1 = ReadADC10(0);
		channel2 = ReadADC10(1);
		motor1Time = (channel1*(60000)/(1023) + 80000);
		motor2Time = (channel2*(60000)/(1023) + 80000);
		//maximo valor de motorTime = 140000

		//use motor time for stepping delay		
		if (MotorsON) {
			if (M1forward) {
				adelante(1, motor1Time, 1);
			} else {
				atras(1, motor1Time, 1);
			}

			if (M2forward) {
				adelante(1, motor2Time, 3);
			} else {
				atras(1, motor2Time, 3);
			}

		}
	
			mAD1ClearIntFlag();
		}
			
		return 0;
}
Пример #28
0
void AdcInitJustMic(void)
{
	PMD1bits.ADC1MD = 0;    // See device errata
	CloseADC10();
	
    // See if we're using the internal RC oscillator
    if (OSCCONbits.COSC == 0b101)
    {
        // Configure ADC for RCOSC
        OpenADC10(	/*config1*/
                    (ADC_MODULE_OFF | 
                    ADC_IDLE_STOP | 
                    ADC_FORMAT_INTG | 
                    ADC_CLK_AUTO |
                    ADC_AUTO_SAMPLING_OFF), 
                    /*config2*/
                    (ADC_VREF_AVDD_AVSS | 
                    ADC_SCAN_ON | 
                    ADC_INTR_EACH_CONV| 
                    ADC_ALT_BUF_OFF |
                    ADC_ALT_INPUT_OFF), 
                    /*config3*/
                    (ADC_CONV_CLK_INTERNAL_RC |     /* INTERNAL RC */
                    ADC_SAMPLE_TIME_20 | 
                    ADC_CONV_CLK_20Tcy),
                    /*configport_L*/
                    (ADC_SELECT_L),
                    /*configport_H*/
                    (ADC_SELECT_H),
                    /*configscan_L*/
                    (0b0000010000000000), /*We dont want to scan the Vref really - it will always be 1023*/
                    /*configscan_H*/
                    (~ADC_SELECT_H)
                );
    }
    else
    {
        // Configure ADC for normal oscillator
        OpenADC10(	/*config1*/
                    (ADC_MODULE_OFF | 
                    ADC_IDLE_STOP | 
                    ADC_FORMAT_INTG | 
                    ADC_CLK_AUTO |
                    ADC_AUTO_SAMPLING_OFF), 
                    /*config2*/
                    (ADC_VREF_AVDD_AVSS | 
                    ADC_SCAN_ON | 
                    ADC_INTR_EACH_CONV| 
                    ADC_ALT_BUF_OFF |
                    ADC_ALT_INPUT_OFF), 
                    /*config3*/
                    (ADC_CONV_CLK_SYSTEM |      /* SYSTEM CLOCK */
                    ADC_SAMPLE_TIME_20 | 
                    ADC_CONV_CLK_20Tcy),
                    /*configport_L*/
                    (ADC_SELECT_L),
                    /*configport_H*/
                    (ADC_SELECT_H),
                    /*configscan_L*/
                    ((0b0000010000000000)), /*We dont want to scan the Vref really - it will always be 1023*/
                    /*configscan_H*/
                    (~ADC_SELECT_H)
                );
    }

	EnableADC1;
}