<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: using ADC  Vrefint to check Vcc</title>
		<link>http://forums.leaflabs.com/topic.php?id=1886</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:08:50 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=1886" rel="self" type="application/rss+xml" />

		<item>
			<title>manitou on "using ADC  Vrefint to check Vcc"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1886#post-11260</link>
			<pubDate>Wed, 13 Jun 2012 14:39:27 +0000</pubDate>
			<dc:creator>manitou</dc:creator>
			<guid isPermaLink="false">11260@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;OK, the default 55.5 rate works, so I can use Occam's razor to cut out the setting of regs-&#38;gt;SMPR1. (I had used the 239.5 cycles setting from the internal temperature example on the wiki ...)&#60;/p&#62;
&#60;p&#62;thanks for the broader view
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "using ADC  Vrefint to check Vcc"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1886#post-11207</link>
			<pubDate>Mon, 11 Jun 2012 11:41:29 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">11207@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thanks for posting!&#60;/p&#62;
&#60;p&#62;I'm not sure that this is what you mean, though:&#60;/p&#62;
&#60;p&#62;&#60;code&#62;regs-&#38;gt;SMPR1 =  ADC_SMPR1_SMP17;  //  sample rate for VREFINT ADC channel&#60;/code&#62;&#60;/p&#62;
&#60;p&#62;While this code seems to work if you're only reading V_REFINT, that's not what the &#60;code&#62;ADC_SMPRx_SMPn&#60;/code&#62; bits are for. (You can read more about &#60;a href=&#34;http://leaflabs.com/docs/libmaple/overview.html#register-bit-definitions&#34;&#62;register bit definitions&#60;/a&#62; in the libmaple overview.)&#60;/p&#62;
&#60;p&#62;The &#60;code&#62;ADC_SMPR1_SMP17&#60;/code&#62; register bit definition is for &#60;em&#62;extracting&#60;/em&#62; or &#60;em&#62;clearing&#60;/em&#62; the SMP17 bits in ADC_SMPR1; it's not meant to be used as a value to set.&#60;/p&#62;
&#60;p&#62;Here's its definition:&#60;/p&#62;
&#60;p&#62;&#60;code&#62;#define ADC_SMPR1_SMP17                 (0x7 &#38;lt;&#38;lt; 21)&#60;/code&#62;&#60;/p&#62;
&#60;p&#62;Looking at ST's docs for ADC_SMPR1 (RM0008 section 11.12.4), setting &#60;code&#62;regs-&#38;gt;SMPR1&#60;/code&#62; to &#60;code&#62;ADC_SMPR1_SMP17&#60;/code&#62; has the following effects:&#60;/p&#62;
&#60;p&#62;1. Sets the sample time for channel 17 (the V_REFINT channel) to 0x7, or 239.5 ADC cycles (the longest available).&#60;br /&#62;
2. Sets the ADC sample time for channels 10 through 16 to 0x0, or 1.5 ADC cycles (the shortest available).&#60;/p&#62;
&#60;p&#62;The second effect in particular is worrisome. It will speed up ADC conversion on channels 10-16 greatly, but drastically lowers the acceptable maximum impedance of your ADC input (if the impedance is too high for the sample time, you'll get inaccurate readings).  We deliberately set the ADC sample time much higher during &#60;code&#62;init()&#60;/code&#62; to allow for high impedance inputs:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;https://github.com/leaflabs/libmaple/blob/v0.0.12/wirish/boards.cpp#L122&#34; rel=&#34;nofollow&#34;&#62;https://github.com/leaflabs/libmaple/blob/v0.0.12/wirish/boards.cpp#L122&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;If you really want to set the channel 17 sample time to 239.5 ADC cycles, you should instead use a read-modify-write sequence, like this:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;uint32 smpr = regs-&#38;gt;SMPR1; // read SMPR1 register&#38;#39;s current value
smpr &#38;amp;= ~ADC_SMPR1_SMP17;  // clear the SMP17 bits, but leave the other bits alone
smpr &#124;= (0x7 &#38;lt;&#38;lt; 21);       // set the SMP17 bits to 0x7, meaning 239.5 ADC cycles
regs-&#38;gt;SMPR1 = smpr;        // write the modified value back to SMPR1&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;You can also globally set the sample time for &#60;em&#62;every&#60;/em&#62; channel on ADC1 to 239.5 ADC cycles with &#60;code&#62;adc_set_sample_rate(ADC1, ADC_SMPR_239_5)&#60;/code&#62;. The Wirish default is 55.5 cycles, or &#60;code&#62;ADC_SMPR_55_5&#60;/code&#62;. The available sample rates for this function are here (heads up: they're different on Maple 2):&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://leaflabs.com/docs/libmaple/api/adc.html#project0adc_8h_1ace843f98737343a7f8d1ef7ac8f9b519&#34; rel=&#34;nofollow&#34;&#62;http://leaflabs.com/docs/libmaple/api/adc.html#project0adc_8h_1ace843f98737343a7f8d1ef7ac8f9b519&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>blackswords on "using ADC  Vrefint to check Vcc"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1886#post-11201</link>
			<pubDate>Mon, 11 Jun 2012 02:32:54 +0000</pubDate>
			<dc:creator>blackswords</dc:creator>
			<guid isPermaLink="false">11201@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;thanks for this code, wouldn't it be better to put this code inside 2 functions or in a class so that it could be added as a library?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>manitou on "using ADC  Vrefint to check Vcc"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1886#post-11196</link>
			<pubDate>Sun, 10 Jun 2012 15:49:23 +0000</pubDate>
			<dc:creator>manitou</dc:creator>
			<guid isPermaLink="false">11196@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;A couple of Arduino sketches use internal voltage ADC channel to check battery voltage:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://www.ikalogic.com/avr-monitor-power-supply-voltage-for-free/&#34; rel=&#34;nofollow&#34;&#62;http://www.ikalogic.com/avr-monitor-power-supply-voltage-for-free/&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;or &#34;calibrate ADC voltage&#34;:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://hacking.majenko.co.uk/node/57&#34; rel=&#34;nofollow&#34;&#62;http://hacking.majenko.co.uk/node/57&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;In the same spirit, Maple has 1.2v internal ADC channel.  Here is a simple sketch that reports Vcc (in millivolts) using the Vrefint ADC channel 17.&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;#include &#38;lt;adc.h&#38;gt;

void setup(){
    adc_reg_map *regs = ADC1-&#38;gt;regs;
    regs-&#38;gt;CR2 &#124;= ADC_CR2_TSEREFE;  // enable VREFINT and temp sensor
    regs-&#38;gt;SMPR1 =  ADC_SMPR1_SMP17;  //  sample rate for VREFINT ADC channel
}

void loop() {
    int mv;

    mv = 1200 * 4096 / adc_read(ADC1, 17);  // ADC sample to millivolts
    SerialUSB.println(mv,DEC);
    delay(3000);
}&#60;/code&#62;&#60;/pre&#62;</description>
		</item>

	</channel>
</rss>
