{"id":302,"date":"2013-08-15T12:49:42","date_gmt":"2013-08-15T12:49:42","guid":{"rendered":"http:\/\/www.electronic-products-development.com\/?p=302"},"modified":"2022-02-18T15:37:49","modified_gmt":"2022-02-18T15:37:49","slug":"atod-2","status":"publish","type":"post","link":"https:\/\/ibex.tech\/embedded\/microchip\/pic32\/c32-compiler\/atod-2","title":{"rendered":"AtoD"},"content":{"rendered":"<h4>\nRegister Based Working Example<br \/>\n<\/h4>\n<h5>\nSet up AtoD<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\r\n\t\/\/----- SETUP AtoD -----\r\n\tAD1PCFG = 0xFFFC;\t\t\t\t\/\/AN1:0 analog, reset digital\r\n\tAD1CHSbits.CH0SA = 0;\t\t\t\/\/Channel 0 positive input is AN0\r\n\tAD1CON1bits.SSRC = 7;\t\t\t\/\/Internal counter ends sampling and starts conversion (auto-convert)\r\n\tAD1CON1bits.ASAM = 1;\t\t\t\/\/Sampling begins when SAMP bit is set\r\n\r\n\t\/\/The PIC32 can sample up to 1000ksps, but we don&#39;t need that.\r\n\t\/\/We want to sample at 44.1kHz to give a Nyquist frequency of 22.05kHz. So 1 sample every 22.6757uS\r\n\t\/\/\r\n\t\/\/Set the ADC Conversion Clock (TAD)\r\n\t\/\/TAD is the basic timng value of the AtoD .\r\n\t\/\/TAD must be &gt;= 83.33nS\r\n\t\/\/A conversion takes 12 x TAD + the number of sampling TAD clocks specified by SAMC (i.e. the fastest possible conversion takes 13 x TAD)\r\n\t\/\/\tSystem clock of 80MHz and PBDIV = 2\r\n\t\/\/\tTPB = (1 \/ 80MHz) * 2 = 25ns (this is the time of the peripheral bus)\r\n\t\/\/\r\n\t\/\/\tTAD = (TPB * 2) * (ADCS + 1) * 2\r\n\t\/\/\t\tTAD = 25ns * (# + 1)\r\n\t\/\/\t\t\r\n\tAD1CON3bits.ADCS = 11;\t\t\t\/\/TAD=11=600ns\r\n\r\n\t\/\/Auto-Sample Time\r\n\t\/\/Set number of TAD clock cycles between start of acquisition and the start of conversion.\r\n\t\/\/SAMC=1=1TAD to SAMC=31=31TAD\r\n\t\/\/Add this to the 12 TAD clock cycles a conversion takes and you have the total AtoD conversion time per input sample\r\n\tAD1CON3bits.SAMC = 25;\t\t\t\/\/SAMC=25=22.2uS\r\n\r\n\tIPC6bits.AD1IP = 6;    \/\/AtoD1 Interrupt Priority\r\n\tIPC6bits.AD1IS = 3;    \/\/AtoD1 Subpriority\r\n\tIFS1CLR = 2;           \/\/Clear ADC interrupt flag\r\n\tIEC1bits.AD1IE = 1;    \/\/ADC interrupt enable\r\n\tAD1CON1bits.ON = 1;    \/\/ADC enable\r\n\r\n<\/code><\/pre>\n<h5>\nInterrupt Per Read<br \/>\n<\/h5>\n<pre>\r\n<code>\r\nvoid __ISR(_ADC_VECTOR, ipl6) ADCInterruptHandler()\r\n{\r\n\tIFS1CLR = 2;\t\t\/\/Clear irq flag\r\n\r\n\tsamples[sampleCounter++] = ADC1BUF0 - 512;\r\n}\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<h4>\nLibrary Based Working Example<br \/>\n<\/h4>\n<h5>\nSetting Up The AtoD<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\t\/\/----- SETUP THE A TO D PINS -----\r\n\t\/\/Pins have allready been set to digital in.  This next line not needed as OpenADC10 will do the job\r\n\t\/\/mPORTBSetPinsAnalogIn(BIT_2 | BIT_3 | BIT_4 | BIT_8);\t\t\t\t\/\/Pins have allready been set to digital in, set these to analog (this updates AD1PCFG)\r\n\r\n\tCloseADC10();\/\/ ensure the ADC is off before setting the configuration\r\n\tOpenADC10(\r\n\t\t\t(ADC_MODULE_ON | ADC_IDLE_STOP | ADC_FORMAT_INTG16 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON | ADC_SAMP_ON), \/\/AD1CON1 register\r\n\t\t\t(ADC_VREF_AVDD_AVSS | ADC_SCAN_ON | ADC_ALT_INPUT_OFF | ADC_SAMPLES_PER_INT_10),\t\t\t\t\t\t\/\/AD1CON2 register (SCAN_ON so that all enabled analog inputs are automatically scanned_\r\n\t\t\t(ADC_SAMPLE_TIME_31 | ADC_CONV_CLK_PB | ADC_CONV_CLK_32Tcy),\t\t\t\t\t\t\t\t\t\t\t\/\/AD1CON3 register (Sets sampling time - 31:32 makes it slow as possible)\r\n\t\t\t(ENABLE_AN4_ANA | ENABLE_AN3_ANA | ENABLE_AN2_ANA),\t\t\t\t\t\t\t\t\t\t\t\t\t\t\/\/AD1PCFG register &lt;&lt; SET ANALOG PINS\r\n\t\t\t(0)\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\/\/AD1CSSL register\r\n\t\t\t);\r\n\tEnableADC10();\r\n<\/code><\/pre>\n<h5>\nReading the values<br \/>\n<\/h5>\n<pre>\r\n<code>\r\nvoid read_atod (void)\r\n{\r\n\tWORD w_temp;\r\n\t\r\n\tw_temp = ReadADC10(2);\r\n\r\n\tw_temp = ReadADC10(3);\r\n\t\t\r\n\tw_temp = ReadADC10(4);\r\n}\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Register Based Working Example Set up AtoD \/\/&#8212;&#8211; SETUP AtoD &#8212;&#8211; AD1PCFG = 0xFFFC; \/\/AN1:0 analog, reset digital AD1CHSbits.CH0SA = 0; \/\/Channel 0 positive input is AN0 AD1CON1bits.SSRC = 7; \/\/Internal counter ends sampling and starts conversion (auto-convert) AD1CON1bits.ASAM = 1; \/\/Sampling begins when SAMP bit is set \/\/The PIC32 can sample up to 1000ksps, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-302","post","type-post","status-publish","format-standard","hentry","category-c32-compiler"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/comments?post=302"}],"version-history":[{"count":4,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/302\/revisions"}],"predecessor-version":[{"id":521,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/302\/revisions\/521"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/media?parent=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/categories?post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/tags?post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}