{"id":159,"date":"2012-05-29T07:57:12","date_gmt":"2012-05-29T07:57:12","guid":{"rendered":"http:\/\/www.electronic-products-development.com\/?p=159"},"modified":"2022-02-18T15:37:49","modified_gmt":"2022-02-18T15:37:49","slug":"atod","status":"publish","type":"post","link":"https:\/\/ibex.tech\/embedded\/microchip\/pic24\/c30-compiler\/atod","title":{"rendered":"AtoD"},"content":{"rendered":"<h4>\nPIC24FJ256GB106 Auto Sampling<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\/\/----- SETUP THE A TO D PINS -----\r\n\t\/\/AN0, AN1, AN11 analog\r\n\t\/\/Auto scan of inputs\r\n\tAD1PCFGL = 0xF7FC;\t\t\/\/Set the analog pins (0 = analog)\r\n\tAD1PCFGH = 0xFFFF;\r\n\tAD1CON2 = 0x0408;\r\n\tAD1CON3 = 0x1F05;\r\n\tAD1CHS = 0x0000;\r\n\tAD1CSSL = 0x0803;\t\t\/\/Set the inputs to scan\r\n\tAD1CON1 = 0x80E4;\t\t\/\/AD1CON2 SMPI (interrupts after # conversions) must &gt;= the number of inputs being scanned otherwise the higher inputs will not be sampled (if &gt;  the number of inputs being scanned then the scannign will repeat fillign up the buffer before the interrupt flag is set, se you can set to 2x the number of inputs being scanned to get 2 samples of all the inputs before an interrupt etc).\r\n\tIFS0bits.AD1IF = 0;\t\t\/\/Clear the interrput flag\r\n\r\n\t\r\n\t\/\/----- READ THE A TO D PINS -----\r\n\tif (IFS0bits.AD1IF)\r\n\t{\r\n\t\t\/\/Interrupt - the number of samples specified by AD1CON2 SMPI has occured.  ADC1BUF# will have been filled up by the number of samples with the specified inputs to scan in lowest to highest order and repeating if number of samples is &gt; the number of inputs to scan specified\r\n\t\tatod_0 = ADC1BUF0;\r\n\t\tatod_1 = ADC1BUF1;\r\n\t\tatod_2 = ADC1BUF2;\t\t\r\n\t\tIFS0bits.AD1IF = 0;\t\t\/\/Reset the irq\r\n\t}\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<h4>\nPIC24FJ256GB106 Manual Sampling<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\/\/----- SETUP THE A TO D PINS -----\r\n\tAD1PCFGH = 0b1111111111111111;\t\/\/Select analog pins (1 = digital, 0 = analog)\r\n\tAD1PCFGL = 0b1111111111100000;\t\/\/Select analog pins (1 = digital, 0 = analog)\r\n\tAD1CON1 = 0b0000000011100000;\t\/\/Internal counter ends sampling and starts conversion, manual sampling trigger\r\n\tAD1CON2 = 0b0000000000000000;\t\/\/ALTS = 0 = Always uses MUX A input multiplexer settings\r\n\tAD1CON3 = 0b0001111100000010;\t\/\/ADCS = 4 (Tad must be 75nS min for PIC24FJ256GB110 family)\r\n\tAD1CHS0 = 0;\t\t\t\t\t\/\/AtoD input select\r\n\r\n\tAD1CON1bits.ADON = 1;\r\n\r\n\tAD1CHS0 = 0;\t\t\t\t\t\t\/\/Select analog input to sample\r\n\tAD1CON1bits.SAMP = 1;\t\t\t\t\/\/Start sampling\r\n\r\n\r\n\r\n\r\n\r\n\/\/****************************************\r\n\/\/****************************************\r\n\/\/********** READ ANALOG INPUTS **********\r\n\/\/****************************************\r\n\/\/****************************************\r\nvoid read_pic_analog_inputs (void)\r\n{\r\n\tBYTE doing_input;\r\n\tstatic BYTE sample_count = 0;\r\n\tstatic DWORD psu_voltage_atod_buffer;\r\n\tstatic DWORD vref_atod_buffer;\r\n\r\n\r\n\r\n\t\/\/----- WAIT FOR LAST CONVERSION TO COMPLETE -----\r\n\tif (!update_atod)\t\t\t\t\t\t\/\/We only check every 1mS as we have no need for the PIC&#39;s very high AtoD sampling speed\r\n\t\treturn;\r\n\t\t\r\n\tif (AD1CON1bits.SAMP || !AD1CON1bits.DONE)\r\n\t\treturn;\r\n\r\n\tdoing_input = AD1CHS0 &amp; 0x000f;\r\n\tupdate_atod = 0;\r\n\r\n\tswitch (doing_input)\r\n\t{\r\n\tcase 0:\r\n\t\t\/\/----- DONE AN0 - PSU VOLTAGE -----\r\n\t\tpsu_voltage_atod_buffer += ADC1BUF0;\r\n\r\n\t\tAD1CHS0 = 1;\t\t\t\t\t\t\/\/Select analog input to sample\r\n\t\tAD1CON1bits.SAMP = 1;\t\t\t\t\/\/Start sampling\r\n\t\tbreak;\r\n\t\t\r\n\tcase 1:\r\n\t\t\/\/----- DONE AN1 - Vref -----\r\n\t\tvref_atod_buffer += ADC1BUF0;\r\n\r\n\t\tAD1CHS0 = 0;\t\t\t\t\t\t\/\/Select analog input to sample\r\n\t\tAD1CON1bits.SAMP = 1;\t\t\t\t\/\/Start sampling\r\n\t\t\r\n\t\t\/\/-------------------------------------\r\n\t\t\/\/---- END OF SAMPLING ALL INPUTS -----\r\n\t\t\/\/-------------------------------------\r\n\t\tsample_count++;\r\n\t\tif (sample_count &gt;= 16)\r\n\t\t{\r\n\t\t\tsample_count = 0;\r\n\r\n\t\t\tpsu_voltage_atod_value = (WORD)(psu_voltage_atod_buffer &gt;&gt; 4);\r\n\t\t\tvref_atod_value = (WORD)(vref_atod_buffer &gt;&gt; 4);\r\n\t\t\t\r\n\t\t\tpsu_voltage_atod_buffer = 0;\r\n\t\t\tvref_atod_buffer = 0;\r\n\t\t\t\r\n\t\t\tprocess_atod_inputs();\r\n\t\t}\r\n\t\t\r\n\t\t\r\n\t\tbreak;\r\n\t\t\r\n\tdefault:\r\n\t\t\/\/Error - shouldn&#39;t be here\r\n\t\twhile (1)\r\n\t\t\t;\t\t\r\n\t\t\r\n\t\tbreak;\r\n\t}\r\n\r\n}<font face=\"Century Gothic, Arial, Helvetica, sans-serif\"><span style=\"font-size: 6px; white-space: normal;\">\r\n<\/span><\/font><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PIC24FJ256GB106 Auto Sampling \/\/&#8212;&#8211; SETUP THE A TO D PINS &#8212;&#8211; \/\/AN0, AN1, AN11 analog \/\/Auto scan of inputs AD1PCFGL = 0xF7FC; \/\/Set the analog pins (0 = analog) AD1PCFGH = 0xFFFF; AD1CON2 = 0x0408; AD1CON3 = 0x1F05; AD1CHS = 0x0000; AD1CSSL = 0x0803; \/\/Set the inputs to scan AD1CON1 = 0x80E4; \/\/AD1CON2 SMPI (interrupts [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-159","post","type-post","status-publish","format-standard","hentry","category-c30-compiler"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/159","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=159"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/159\/revisions"}],"predecessor-version":[{"id":539,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/159\/revisions\/539"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/media?parent=159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/categories?post=159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/tags?post=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}