<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>How to read Potentiometer and display conversion result</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/39582/how-to-read-potentiometer-and-display-conversion-result</link><description> 
I would to ask how to read the potentiometer(Keil MCBSTR9U) input
periodically with a timer interrupt? And also to display the
conversion result on the LCD display. Please help. I don&amp;#39;t know how
to get started. 

 
#include &amp;lt;91x_lib.h&amp;gt;
#include &amp;quot;LCD</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: How to read Potentiometer and display conversion result</title><link>https://community.arm.com/thread/140148?ContentTypeID=1</link><pubDate>Sun, 29 Jan 2012 16:12:23 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:5de3e7bc-d4c0-4962-b911-d3af9aaa4b9f</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
If you have a number between 0 and 9, you can make it into ASCII
by adding &amp;#39;0&amp;#39;.&lt;/p&gt;

&lt;p&gt;
So if you have a number between 0 and 1023 you could do:&lt;/p&gt;

&lt;pre&gt;
char1 = &amp;#39;0&amp;#39; + (num / 1000);
char2 = &amp;#39;0&amp;#39; + ((num / 100) % 10);
char3 = &amp;#39;0&amp;#39; + ((num / 10) % 10);
char4 = &amp;#39;0&amp;#39; + (num % 10);
buf[0] = char1;
buf[1] = char2;
buf[2] = char3;
buf[3] = char4;
buf[4] = &amp;#39;\0&amp;#39;;
lcd_print(buf);
&lt;/pre&gt;

&lt;p&gt;
Or you could use:&lt;/p&gt;

&lt;pre&gt;
sprintf(buf,&amp;quot;%u&amp;quot;,num);
lcd_print(buf);
&lt;/pre&gt;

&lt;p&gt;
There are so many different ways you could select. Google or
experiement instead of seeing problems.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read Potentiometer and display conversion result</title><link>https://community.arm.com/thread/130944?ContentTypeID=1</link><pubDate>Sun, 29 Jan 2012 09:13:46 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:dc797f25-5fc6-4668-8bc2-cf423ab8c542</guid><dc:creator>Winnie Tan</dc:creator><description>&lt;p&gt;&lt;p&gt;
What i am trying to ask is how to display correctly the AD
conversion value to the LCD? As from what i understand, the printing
on the LCD is done using char variable encoded in ASCII character. As
ADC_value is given as integer value, we would need to convert them
into ASCII value, correct me if i am wrong. So the maximum value to
be display is 1023. So I am thinking i might need to conver to ASCII
before displaying.&lt;/p&gt;

&lt;p&gt;
by having the main loop see flag that there are a new value to
print. Then call lcd_print() to emit it.&lt;br /&gt;
-&amp;gt; Would appreciate if you could tell me more how to do this?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read Potentiometer and display conversion result</title><link>https://community.arm.com/thread/127336?ContentTypeID=1</link><pubDate>Sun, 29 Jan 2012 08:36:41 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ca3baa3b-0f47-4827-b485-d8a34a676b8f</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
As I did mention in a previous post - by having the main loop see
flag that there are a new value to print. Then call lcd_print() to
emit it. An optimization would be to ignore display update if the new
ADC value is the same as what was previously printed.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read Potentiometer and display conversion result</title><link>https://community.arm.com/thread/116689?ContentTypeID=1</link><pubDate>Sun, 29 Jan 2012 05:36:59 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:b3f73719-17a1-4637-993e-fadf41b3bc30</guid><dc:creator>Winnie Tan</dc:creator><description>&lt;p&gt;&lt;p&gt;
Just wondering how is the display work? For example when we are
done with the AD conversion, what next is to display it to the LCD.
How can it be done??&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read Potentiometer and display conversion result</title><link>https://community.arm.com/thread/105676?ContentTypeID=1</link><pubDate>Sun, 29 Jan 2012 04:40:39 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:46a903ee-4583-44e7-86d0-1d435a093c9b</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
Note that it&amp;#39;s very hard to read this code:&lt;/p&gt;

&lt;pre&gt;
ADC-&amp;gt;CR &amp;amp;= 0xFFFE;                    /* Clear STR bit (Start Conversion)   */
ADC-&amp;gt;CR &amp;amp;= 0x7FFF;                    /* Clear End of Conversion flag       */
&lt;/pre&gt;

&lt;p&gt;
It&amp;#39;s much better to write:&lt;/p&gt;

&lt;pre&gt;
ADC-&amp;gt;CR &amp;amp;= ~0x0001;                    /* Clear STR bit (Start Conversion)   */
ADC-&amp;gt;CR &amp;amp;= ~0x8000;                    /* Clear End of Conversion flag       */
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
Now, the specific bit cleared is visible. And another thing is that
the ~ operation will be performed to full integer width. Wahat if you
use 0xFFFE to a 32-bit wide register? Then you would also clear the
16 high bits without it being visible to the reader.&lt;/p&gt;

&lt;p&gt;
Next thing is that it&amp;#39;s a good idea to created named constants for
your flags.&lt;/p&gt;

&lt;pre&gt;
enum {
    ADC_CR_STR = 0x0001u,
};
ADC-&amp;gt;CR &amp;amp;= ~ADC_CR_STR;
&lt;/pre&gt;

&lt;p&gt;
Now a reader can see that you are clearing the STR bit of the
register, without having to read a comment.&lt;/p&gt;

&lt;p&gt;
I normally create include files with these additional flags for
the different processors I&amp;#39;m using so I would include:&lt;/p&gt;

&lt;pre&gt;
#include &amp;quot;lpc17xx_adc.h&amp;quot;
#include &amp;quot;lpc17xx_uart.h&amp;quot;
...
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
for my projects that are using a processor in the NXP LPC17xx
family.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read Potentiometer and display conversion result</title><link>https://community.arm.com/thread/79945?ContentTypeID=1</link><pubDate>Sun, 29 Jan 2012 04:08:36 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:b52c3353-603a-4ff0-a69c-7c5510d2a584</guid><dc:creator>Winnie Tan</dc:creator><description>&lt;p&gt;&lt;p&gt;
Hello Per Westermark, thank you for the reply.&lt;/p&gt;

&lt;p&gt;
As I am very new to this, so pardon if i have mentioned anything
&amp;quot;ridiculous&amp;quot;.&lt;/p&gt;

&lt;p&gt;
Woudlnt&amp;#39; you simply have the timer interrupt start an ADC
conversion and then end?&lt;br /&gt;
-&amp;gt;&lt;/p&gt;

&lt;pre&gt;
__irq void TIM3_IRQ_Handler (void) {    /* TIM3 timer interrupt routine       */

  if (!AD_in_progress)  {               /* If conversion not in progress      */
    AD_in_progress = 1;                 /* Flag that AD conversion is started */
    ADC-&amp;gt;CR |= 0x0403;                  /* Set STR bit (Start Conversion)     */
                                        /* on Channel 0, with interrupt       */
                                        /* generation enabled                 */
  }
  TIM3-&amp;gt;SR &amp;amp;= ~0x2000;                  /* Clear Timer Overflow interrupt flag*/

  VIC0-&amp;gt;VAR = 0;                        /* Acknowledge Interrupt              */
  VIC1-&amp;gt;VAR = 0;
}
&lt;/pre&gt;

&lt;p&gt;
And the ADC interrupt pick up the ADC conversion result and then
end?&lt;br /&gt;
-&amp;gt;&lt;/p&gt;

&lt;pre&gt;

__irq void ADC_IRQ_Handler (void) {     /* AD converter interrupt routine     */

  AD_last = ADC-&amp;gt;DR0 &amp;amp; 0x03FF;          /* AD value for global usage (10 bit) */

  ADC-&amp;gt;CR &amp;amp;= 0xFFFE;                    /* Clear STR bit (Start Conversion)   */
  ADC-&amp;gt;CR &amp;amp;= 0x7FFF;                    /* Clear End of Conversion flag       */

  AD_value = ADC-&amp;gt;DR0 &amp;amp; 0x3FF;           /* Read the ADC Value            */

  VIC0-&amp;gt;VAR = 0;                        /* Acknowledge Interrupt              */
  VIC1-&amp;gt;VAR = 0;

  AD_in_progress = 0;                   /* Clear flag, as AD conv finished    */
}


&lt;/pre&gt;

&lt;p&gt;
I am just trying to read the AD_conversion value and display it on
LCD. I admit that the code written are way too complicated, any
advice how i can do it??&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to read Potentiometer and display conversion result</title><link>https://community.arm.com/thread/67218?ContentTypeID=1</link><pubDate>Sun, 29 Jan 2012 02:13:38 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:4f4970e2-c48a-40bd-a626-e4c1043f1cdf</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
Woudlnt&amp;#39; you simply have the timer interrupt start an ADC
conversion and then end?&lt;br /&gt;
And the ADC interrupt pick up the ADC conversion result and then
end?&lt;/p&gt;

&lt;p&gt;
The issue here is that you also want to emit data to the LCD. That
is normally not a good thing to do in an inteerrupt because it
normally takes very long time to interact with the LCD. So the ADC
interrupt would then just have to store the ADC value in a buffer and
set a flag that there is a ADC value to emit. And your main loop
would see the flag, pick up the value and display it.&lt;/p&gt;

&lt;p&gt;
By the way: why so complicated code:&lt;/p&gt;

&lt;pre&gt;
if (AD_value &amp;gt;= 0 &amp;amp;&amp;amp; AD_value &amp;lt;= 13 ){
    lcd_print(&amp;quot;0&amp;quot;);
} else if (AD_value &amp;gt;= 14 &amp;amp;&amp;amp; AD_value &amp;lt;= 26) {
    lcd_print(&amp;quot;1&amp;quot;);
} else if (AD_value &amp;gt;= 27 &amp;amp;&amp;amp; AD_value &amp;lt;= 39) {
    lcd_print(&amp;quot;2&amp;quot;);
} else if (AD_value &amp;gt;= 40 &amp;amp;&amp;amp; AD_value &amp;lt;= 52) {
    lcd_print(&amp;quot;3&amp;quot;);
} else if (AD_value &amp;gt;= 53 &amp;amp;&amp;amp; AD_value &amp;lt;= 65)  {
    lcd_print(&amp;quot;4&amp;quot;);
} else if (AD_value &amp;gt;= 66 &amp;amp;&amp;amp; AD_value &amp;lt;= 78) {
    lcd_print(&amp;quot;5&amp;quot;);
}
&lt;/pre&gt;

&lt;p&gt;
Do you expect the ADC to return negative values? Of course some
ADC really can return results in two-complement format or with a sign
bit but the majority of ADC only supports measuring a positive input
signal.&lt;/p&gt;

&lt;p&gt;
Next thing - do you expect your ADC value to be able to have a
value &amp;gt; 26 but &amp;lt; 27? Why else do you have two conditions for
each range? If there isn&amp;#39;t a gap between then, you only need to have
a series of &amp;quot;less than&amp;quot; comparisons to bin the ADC value into
different bins.&lt;/p&gt;

&lt;p&gt;
Next thing - if your bins are always the same size, you don&amp;#39;t need
to do as above. You can perform an integer division by 14. 13 is
smaller than 14, so answer will be zero. 14..27 are less than 2 so
but &amp;gt;= 1 so will return value 1.&lt;/p&gt;

&lt;p&gt;
By the way - you did notice that your first bin is 14 numbers
large, while all other bins are 13 large? A mistake ir really
intended?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>