Exemple #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;
}
Exemple #2
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
}
Exemple #3
0
void  ADC_Config (void) 
{
    CPU_INT32U  config1;
    CPU_INT32U  config2;
    CPU_INT32U  config3;
    CPU_INT32U  config4;
    CPU_INT32U  config5;
      
    config1 = ADC_IDLE_STOP
            | ADC_FORMAT_INTG
            | ADC_CLK_AUTO
            | ADC_AUTO_SAMPLING_ON
            | ADC_SAMP_ON;
            
    config2 = ADC_VREF_AVDD_AVSS
            | ADC_OFFSET_CAL_DISABLE
            | ADC_SCAN_OFF
            | ADC_SAMPLES_PER_INT_1
            | ADC_ALT_BUF_ON
            | ADC_ALT_INPUT_OFF;
    
    config3 = ADC_SAMPLE_TIME_0
            | ADC_CONV_CLK_INTERNAL_RC
            | ADC_CONV_CLK_Tcy2;
           
    config4 = ENABLE_AN2_ANA | ENABLE_AN4_ANA | ENABLE_AN6_ANA | ENABLE_AN8_ANA | ENABLE_AN10_ANA | ENABLE_AN11_ANA;
    
	config5 = SKIP_SCAN_ALL;

	// configure to sample AN4
	SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN1 ); //|  ADC_CH0_NEG_SAMPLEB_NVREF | ADC_CH0_POS_SAMPLEB_AN5); // configure to sample AN4 & AN5
    OpenADC10(config1, config2, config3, config4, config5);
}
Exemple #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;
}
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;
}
Exemple #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();
}
Exemple #7
0
/**
 * @Function AD_SetPins
 * @param None
 * @return SUCCESS OR ERROR
 * @brief  Connects pins in PinstoAdd to the A/D system and removes the pins in PinsToRemove
 * @note  Private Function. DO NOT USE.
 * @author Max Dunne, 2013.08.15 */
char AD_SetPins(void)
{
    if (!ADActive) {
        return ERROR;
    }
    unsigned int cssl = 0;
    unsigned int pcfg = 0;
    unsigned int rempcfg = 0;
    unsigned char CurPin = 0;
    unsigned int CurPinOrder = 0x00;
    int ADMapping[NUM_AD_PINS_UNO];
    AD1CON1CLR = _AD1CON1_ON_MASK; //disable A/D system and interrupt
    INTEnable(INT_AD1, INT_DISABLED);
    PinCount = 0;

    //determine the new set of active pins
    ActivePins |= PinsToAdd;
    ActivePins &= ~PinsToRemove;
    //initialize the mapping array to -1
    for (CurPin = 0; CurPin < NUM_AD_PINS_UNO; CurPin++) {
        ADMapping[CurPin] = -1;
    }
    // memset(ADMapping,-1,NUM_AD_PINS_UNO);
    for (CurPin = 0; CurPin < NUM_AD_PINS; CurPin++) {
        PortMapping[CurPin] = -1; //reset all ports to unmapped
        if ((ActivePins & (1 << CurPin)) != 0) { //if one of the pins is active
            //build masks and remap pins
            cssl |= AD1CSSL_MASKS[CurPin];
            pcfg |= AD1PCFG_MASKS[CurPin];
            ADMapping[AD1PCFG_POS[CurPin]] = CurPin;
            PinCount++;
        }
        if ((PinsToRemove & (1 << CurPin)) != 0) {//generate removal masks
            rempcfg |= AD1PCFG_MASKS[CurPin];
        }
    }
    for (CurPin = 0; CurPin < NUM_AD_PINS_UNO; CurPin++) {//translate AD Mapping to Port Mapping
        if (ADMapping[CurPin] != -1) {
            PortMapping[ADMapping[CurPin]] = CurPinOrder;
            CurPinOrder++;
        }
    }
    cssl = ~cssl;
    OpenADC10(ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON,
            ADC_VREF_AVDD_AVSS | ADC_SCAN_ON | ((PinCount - 1) << _AD1CON2_SMPI_POSITION) | ADC_BUF_16,
            ADC_SAMPLE_TIME_29 | ADC_CONV_CLK_51Tcy2 | ADC_CONV_CLK_PB, pcfg, cssl);
    AD1PCFGSET = rempcfg;
    //recalculate interval between battery samples
    PointsPerBatSamples = (POINTS_PER_SECOND_PER_PIN / PinCount) / (float) FREQUENCY_TO_SAMPLE;
    PinsToAdd = 0;
    PinsToRemove = 0;
    INTEnable(INT_AD1, INT_ENABLED);
    return SUCCESS;
}
/**
 * @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() ) {
    
    }
}
Exemple #9
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 );
}
Exemple #10
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;
}
Exemple #11
0
int main(void)
{
    BOARD_Init();

    // Configure Timer 1 using PBCLK as input. This default period will make the LEDs blink at a
    // pretty reasonable rate to start.
    OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_8, 0xFFFF);

    // Set up the timer interrupt with a priority of 4.
    INTClearFlag(INT_T1);
    INTSetVectorPriority(INT_TIMER_1_VECTOR, INT_PRIORITY_LEVEL_4);
    INTSetVectorSubPriority(INT_TIMER_1_VECTOR, INT_SUB_PRIORITY_LEVEL_0);
    INTEnable(INT_T1, INT_ENABLED);



    // Enable interrupts for the ADC
    ConfigIntADC10(ADC_INT_PRI_2 | ADC_INT_SUB_PRI_0 | ADC_INT_ON);

    // Set B2 to an input so AN0 can be used by the ADC.
    TRISBCLR = 1 << 2;

    // Configure and start the ADC
    // Read AN0 as sample a. We don't use alternate sampling, so setting sampleb is pointless.
    SetChanADC10(ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN2);
    OpenADC10(
            ADC_MODULE_ON | ADC_IDLE_CONTINUE | ADC_FORMAT_INTG16 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON,
            ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_8 |
            ADC_BUF_16 | ADC_ALT_INPUT_OFF,
            ADC_SAMPLE_TIME_29 | ADC_CONV_CLK_PB | ADC_CONV_CLK_51Tcy2,
            ENABLE_AN2_ANA,
            SKIP_SCAN_ALL);
    EnableADC10();

    /***************************************************************************************************
     * Your code goes in between this comment and the following one with asterisks.
     **************************************************************************************************/
    printf("Welcome to the Lab 6 Extra Credit blank. Please remove before starting.");


    /***************************************************************************************************
     * Your code goes in between this comment and the preceding one with asterisks
     **************************************************************************************************/

    while (1);
}
Exemple #12
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
}
Exemple #13
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
}
Exemple #15
0
static  void  ADC_Config (void) 
{
    CPU_INT32U  config;
    CPU_INT32U  config1;
    CPU_INT32U  config2;
    CPU_INT32U  config3;
    CPU_INT32U  config4;
    CPU_INT32U  config5;
    
    
    config  = ADC_CH0_NEG_SAMPLEA_NVREF
            | ADC_CH0_POS_SAMPLEA_AN5;
    
    config1 = ADC_MODULE_ON
            | ADC_IDLE_STOP
            | ADC_FORMAT_INTG
            | ADC_CLK_TMR
            | ADC_AUTO_SAMPLING_ON
            | ADC_SAMP_ON;
            
    config2 = ADC_VREF_AVDD_AVSS
            | ADC_OFFSET_CAL_DISABLE
            | ADC_SCAN_OFF
            | ADC_SAMPLES_PER_INT_16
            | ADC_ALT_BUF_ON
            | ADC_ALT_INPUT_ON;
    
    config3 = ADC_SAMPLE_TIME_0
            | ADC_CONV_CLK_INTERNAL_RC
            | ADC_CONV_CLK_Tcy2;
           
    config4 = SKIP_SCAN_ALL;
     
    config5 = ENABLE_AN5_ANA;
    
    SetChanADC10(config);        
    OpenADC10(config1, config2, config3, config4, config5);
}
Exemple #16
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();
		}
Exemple #17
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() );
}
Exemple #18
0
Fichier : AD.c Projet : mgsit/Hand
/****************************************************************************
 Function
     AD_Init

 Parameters
    Pins, used #defined AD_PORTxxx OR'd together for each A/D Pin

 Returns
     SUCCESS or ERROR

 Description
    Initializes the A/D pins requested into analog inputs and configures the A/D subsystem.
    It then generates the mapping for correctly reading the pin and then starts the A/D system.
 Notes
     None.

 Author
 Max Dunne, 2011.12.10
 ****************************************************************************/
unsigned char AD_Init(unsigned int Pins) {
    unsigned int cssl = 0;
    unsigned int pcfg = 0;
    unsigned char CurPin = 0;
    unsigned int PinCount2 = 0x00;
    int ADMapping[NUM_AD_PINS_UNO];

    if ((Pins == 0) || (Pins > 0x3FFF)) {
        return ERROR;
    }
    for (CurPin = 0; CurPin < NUM_AD_PINS_UNO; CurPin++) {
        ADMapping[CurPin] = -1;
    }
    for (CurPin = 0; CurPin < NUM_AD_PINS; CurPin++) {
        PortMapping[CurPin] = -1;
        if ((Pins & (1 << CurPin)) != 0) {
            cssl |= AD1CSSL_MASKS[CurPin];
            pcfg |= AD1PCFG_MASKS[CurPin];
            ADMapping[AD1PCFG_POS[CurPin]] = CurPin;
            PinCount++;
        }
    }
    for (CurPin = 0; CurPin < NUM_AD_PINS_UNO; CurPin++) {
        if (ADMapping[CurPin] != -1) {
            PortMapping[ADMapping[CurPin]] = PinCount2;
            PinCount2++;
        }
    }
    UsedPins = Pins;
    cssl = ~cssl;
    OpenADC10(ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON, ADC_VREF_AVDD_AVSS
            | ADC_SCAN_ON | ((PinCount - 1) << _AD1CON2_SMPI_POSITION) | ADC_BUF_16, ADC_SAMPLE_TIME_31 | ADC_CONV_CLK_32Tcy | ADC_CONV_CLK_PB, pcfg, cssl);
    ConfigIntADC10(ADC_INT_ON | ADC_INT_PRI_1 | ADC_INT_SUB_PRI_3);

    EnableADC10();
    return SUCCESS;
}
Exemple #19
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;
}
/****************************************************************************
  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();
}
Exemple #21
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
Exemple #22
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

}
Exemple #23
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;
		}
	}
}
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;
}
Exemple #25
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);
}
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;
}