1 /********************************************************************* 2 FileName: ADC.c 3 Dependencies: See #includes 4 Processor: PIC32MZ 5 Hardware: MainBrain MZ 6 Complier: XC32 4.40 7 Author: Larry Knight 2023 8 /********************************************************************* 9 10 Software License Agreement: 11 12 Licensed under the Apache License, Version 2.0 (the "License"); 13 you may not use this file except in compliance with the License. 14 You may obtain a copy of the License at 15 16 http://www.apache.org/licenses/LICENSE-2.0 17 18 Unless required by applicable law or agreed to in writing, software 19 distributed under the License is distributed on an "AS IS" BASIS, 20 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 See the License for the specific language governing permissions and 22 limitations under the License. 23 24 Description: 25 System Clock = 200 - 250 MHz 26 27 File Description: 28 29 Change History: 30 31 /***********************************************************************/ 32 33 #include <xc.h> 34 #include "MainBrain.h" 35 36 void ADC_init(void) 37 { 38 /* Configure ADCCON1 */ 39 ADCCON1 = 0; // No ADCCON1 features are enabled including: Stop-in-Idle, turbo, 40 // CVD mode, Fractional mode and scan trigger source. 41 ADCCON1bits.SELRES = 3; // ADC7 resolution is 12 bits 42 ADCCON1bits.STRGSRC = 1; // Select scan trigger. 43 /* Configure ADCCON2 */ 44 ADCCON2bits.SAMC = 5; // ADC7 sampling time = 5 * TAD7 45 ADCCON2bits.ADCDIV = 1; // ADC7 clock freq is half of control clock = TAD7 46 /* Initialize warm up time register */ 47 ADCANCON = 0; 48 ADCANCONbits.WKUPCLKCNT = 5; // Wakeup exponent = 32 * TADx 49 /* Clock setting */ 50 ADCCON3bits.ADCSEL = 0; // Select input clock source 51 ADCCON3bits.CONCLKDIV = 1; // Control clock frequency is half of input clock 52 ADCCON3bits.VREFSEL = 0; // Select AVDD and AVSS as reference source 53 ADC0TIMEbits.ADCDIV = 1; // ADC0 clock frequency is half of control clock = TAD0 54 ADC0TIMEbits.SAMC = 5; // ADC0 sampling time = 5 * TAD0 55 ADC0TIMEbits.SELRES = 3; // ADC0 resolution is 12 bits 56 /* Select analog input for ADC modules, no presync trigger, not sync sampling */ 57 ADCTRGMODEbits.SH0ALT = 0; // ADC0 = AN0 58 /* Select ADC input mode */ 59 ADCIMCON1bits.SIGN0 = 0; // unsigned data format 60 ADCIMCON1bits.DIFF0 = 0; // Single ended mode 61 ADCIMCON1bits.SIGN6 = 0; // unsigned data format 62 ADCIMCON1bits.DIFF6 = 0; // Single ended mode 63 /* Configure ADCGIRQENx */ 64 ADCGIRQEN1 = 0; // No interrupts are used. 65 ADCGIRQEN2 = 0; 66 /* Configure ADCCSSx */ 67 ADCCSS1 = 0; // Clear all bits 68 ADCCSS2 = 0; 69 ADCCSS1bits.CSS0 = 1; // AN0 (Class 1) set for scan 70 ADCCSS1bits.CSS6 = 1; // AN6 (Class 2) set for scan 71 72 /* Configure ADCCMPCONx */ 73 ADCCMPCON1 = 0; // No digital comparators are used. Setting the ADCCMPCONx 74 ADCCMPCON2 = 0; // register to '0' ensures that the comparator is disabled. 75 ADCCMPCON3 = 0; // Other registers are ?don't care?. 76 ADCCMPCON4 = 0; 77 ADCCMPCON5 = 0; 78 ADCCMPCON6 = 0; 79 /* Configure ADCFLTRx */ 80 ADCFLTR1 = 0; // No oversampling filters are used. 81 ADCFLTR2 = 0; 82 ADCFLTR3 = 0; 83 ADCFLTR4 = 0; 84 ADCFLTR5 = 0; 85 ADCFLTR6 = 0; 86 87 /* Set up the trigger sources */ 88 ADCTRG1bits.TRGSRC0 = 3; // Set AN0 (Class 1) to trigger from scan source 89 ADCTRG2bits.TRGSRC6 = 3; // Set AN6 (Class 2) to trigger from scan source 90 91 /* Early interrupt */ 92 ADCEIEN1 = 0; // No early interrupt 93 ADCEIEN2 = 0; 94 /* Turn the ADC on */ 95 ADCCON1bits.ON = 1; 96 /* Wait for voltage reference to be stable */ 97 while(!ADCCON2bits.BGVRRDY); // Wait until the reference voltage is ready 98 while(ADCCON2bits.REFFLT); // Wait if there is a fault with the reference voltage 99 /* Enable clock to analog circuit */ 100 ADCANCONbits.ANEN0 = 1; // Enable the clock to analog bias ADC0 101 ADCANCONbits.ANEN7 = 1; // Enable, ADC7 102 /* Wait for ADC to be ready */ 103 while(!ADCANCONbits.WKRDY0); // Wait until ADC0 is ready 104 while(!ADCANCONbits.WKRDY7); // Wait until ADC7 is ready 105 /* Enable the ADC module */ 106 ADCCON3bits.DIGEN0 = 1; // Enable ADC0 107 ADCCON3bits.DIGEN7 = 1; // Enable ADC7 108 } 109