void initADCDMA (void) { // Configure Oscillator to operate the device at 40Mhz // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2 // Fosc= 8M*40/(2*2)=80Mhz for 8M input clock PLLFBD=38; // M=40 CLKDIVbits.PLLPOST=0; // N1=2 CLKDIVbits.PLLPRE=0; // N2=2 OSCTUN=0; // Tune FRC oscillator, if FRC is used // Disable Watch Dog Timer RCONbits.SWDTEN=0; // clock switching to incorporate PLL __builtin_write_OSCCONH(0x03); // Initiate Clock Switch to Primary Oscillator with PLL (NOSC=0b011) __builtin_write_OSCCONL(OSCCON || 0x01); // Start clock switching while (OSCCONbits.COSC != 0x03); // Wait for Clock switch to occur while(OSCCONbits.LOCK!=1) {}; // Wait for PLL to lock // Peripheral Initialisation initDma0(); // Initialise the DMA controller to buffer ADC data in conversion order initAdc1(); // Initialize the A/D converter to convert Channel 5 //Loop Endlessly - Execution is interrupt driven //from this point on. }
void adc_open_v1q() { NUM_CHS2SCAN = 4; TRISBbits.TRISB3 = 1; // OPTIONAL TRISBbits.TRISB8 = 1; // input TRISBbits.TRISB9 = 1; // input TRISAbits.TRISA7 = 1; // input AD1CON1bits.FORM = 0b10; // Data Output Format: Fractional AD1CON1bits.SSRC = 0b111; // Sample Clock Source: internal timer (auto-convert) AD1CON1bits.ASAM = 1; // ADC Sample Control: Sampling begins immediately after conversion AD1CON1bits.AD12B = 0; // 12-bit ADC operation AD1CON2bits.CSCNA = 1; // Scan Input Selections for CH0+ during Sample A bit AD1CON2bits.CHPS = 0; // Converts CH0 AD1CON2bits.VCFG = 0b000; // use AVDD and AGND AD1CON3bits.ADRC = 0; // ADC Clock is derived from Systems Clock (0; 1=internal clock) AD1CON3bits.ADCS = 63; // ADC Conversion Clock Tad=Tcy*(ADCS+1)= (1/40M)*64 = 1.6us (625Khz) // ADC Conversion Time for 10-bit Tc=12*Tab = 19.2us AD1CON3bits.SAMC = 31; // auto sample time bits /* Conversion time = sample + conversion = 31*tad + 14*tad (12-bit) Conversion time * 7 * 8 = 0.0007s */ AD1CON1bits.ADDMABM = 0; // DMA buffers are built in scatter/gather mode AD1CON2bits.SMPI = (NUM_CHS2SCAN-1); // 4 ADC Channel is scanned AD1CON4bits.DMABL = 3; // Each buffer contains 8 words //AD1CSSH/AD1CSSL: A/D Input Scan Selection Register AD1CSSH = 0x0000; AD1CSSL = 0x0000; AD1CSSLbits.CSS3 = 1; // Enable AN0 for channel scan AD1CSSLbits.CSS8 = 1; AD1CSSLbits.CSS9 = 1; AD1CSSHbits.CSS23 = 1; // RA7 //AD1PCFGH/AD1PCFGL: Port Configuration Register AD1PCFGL=0xFFFF; AD1PCFGH=0xFFFF; AD1PCFGLbits.PCFG3 = 0; // AN0 as Analog Input AD1PCFGLbits.PCFG8 = 0; AD1PCFGLbits.PCFG9 = 0; AD1PCFGHbits.PCFG23 = 0; IFS0bits.AD1IF = 0; // Clear the A/D interrupt flag bit IEC0bits.AD1IE = 0; // Do Not Enable A/D interrupt initDma0(); adc_start(); }
void initAdc1(void) { AD1CON1bits.FORM = 0; // Data Output Format: Integer AD1CON1bits.SSRC = 2; // Sample Clock Source: GP Timer starts conversion AD1CON1bits.ASAM = 1; // ADC Sample Control: Sampling begins immediately after conversion AD1CON1bits.AD12B = 0; // 10-bit ADC operation AD1CON1bits.SIMSAM = 1; // Simultaneous Sampling AD1CON1bits.ADDMABM = 0; // DMA buffers are built in scatter/gather mode AD1CON2bits.CSCNA = 0; // Do not scan inputs AD1CON2bits.CHPS = 2; // Converts CH0, CH1, CH2, and CH3 AD1CON2bits.SMPI = (NUM_CHS2SCAN-1); // 4 ADC Channels are scanned AD1CON3bits.ADRC = 0; // ADC Clock is derived from Systems Clock AD1CON3bits.ADCS = 63; // ADC Conversion Clock Tad=Tcy*(ADCS+1)= (1/40M)*64 = 1.6us (625Khz) // ADC Conversion Time for 10-bit Tc=12*Tab = 19.2us AD1CON4bits.DMABL = 5; // 32 DMA buffer locations per analog input bits AD1CHS123bits.CH123SA = 1; // CH1 positive input is AN3, CH2 positive input is AN4, CH3 positive input is AN5 AD1CHS123bits.CH123NA = 0; // CH1, CH2, CH3 negative input is VREFL AD1CHS0bits.CH0SA = 6; // Channel 0 positive input is AN6 AD1CHS0bits.CH0NA = 0; // AD1PCFGL: Port Configuration Register AD1PCFGL=0xFFFF; AD1PCFGLbits.PCFG4 = 0; // AN4 as analog input AD1PCFGLbits.PCFG5 = 0; // AN5 as analog input AD1PCFGLbits.PCFG6 = 0; // AN6 as analog input TRISBbits.TRISB2 = 1; // Setting the direction of I/O pin B2 as input TRISBbits.TRISB3 = 1; // Setting the direction of I/O pin B3 as input TRISCbits.TRISC0 = 1; // Setting the direction of I/O pin C0 as input IFS0bits.AD1IF = 0; // Clear the A/D interrupt flag bit IEC0bits.AD1IE = 0; // Do Not Enable A/D interrupt AD1CON1bits.ADON = 1; // Turn on the A/D converter initTmr3(); // Initialize TMR3 initDma0(); // Initialize the DMA controller to buffer ADC data in conversion order }
void adcSetup(void){ adcSetupPeripheral(); initDma0(); //DMA is needed to read multiple values from the ADC core }
int main (void) { int dummy; //int matrix; //only for test of ledmdrv int i; //int temp_current_val; //temporary //int first_ec=1; //Setup oscillator/ports/pins first setup_oscillator(); setup_ports(); setup_peripheral_pin_select(); setup_interrupt_priorities(); #ifdef USE_DIO setup_dio(); #endif #ifdef USE_CONTROL setup_control(); #endif #ifdef USE_ADC_SPI setup_adc_spi(); #endif #ifdef USE_ADC setup_adc(); initDma0(); // Initialise the DMA controller to buffer ADC data in conversion order #endif #ifdef USE_TIMER3 //setup_timer3(); #endif #ifdef USE_UART setup_uart(); #endif ecat_isr_running = 0; #ifdef USE_ETHERCAT while (!eeprom_loaded()) //Wait until ESC is ready //ToggleHeartbeatLED(); //Success SetHeartbeatLED; //#ifdef ECAT_DMA cfgDma0SpiTx(); cfgDma1SpiRx(); //#endif setup_ethercat(); ClrHeartbeatLED; #endif #if defined PWR_0_2 || defined PWR_0_3 || defined PWR_0_4 || defined PWR_0_5 ClrEnableMotor; #endif setup_interrupt_priorities(); dummy = U1RXREG; while(1){ if (i++%20001==0) { //ToggleHeartbeatLED(); } #if defined USE_ETHERCAT if (!IEC0bits.DMA1IE) { DISABLE_AL_EVENT_INT; step_ethercat(); ENABLE_AL_EVENT_INT; } #endif } }