Jump to content

Displaying ADC value on a 16x2 LCD

Recommended Posts

Hi all,


I have small opamp LDR circuit that provides a voltage 0-3 volts to the ADC on the MSP, the code I am using is below and all works fine.



#include  "msp430g2231.h"
#define     LED0                  BIT0
#define     LED1                  BIT6
unsigned int value=0;
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
__bic_SR_register_on_exit(CPUOFF);        // Return to active mode }
void ConfigureAdc(void)
/* Configure ADC  Channel */
ADC10CTL1 = INCH_3 + ADC10DIV_3 ;         // Channel 3, ADC10CLK/4
ADC10CTL0 = SREF_0 + ADC10SHT_3 + ADC10ON + ADC10IE;  //Vcc & Vss as reference
ADC10AE0 |= BIT3;                         //P1.3 ADC option     }
void main(void)
WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
BCSCTL1 = CALBC1_1MHZ;                    // Set range   DCOCTL = CALDCO_1MHZ;
BCSCTL2 &= ~(DIVS_3);                    // SMCLK = DCO = 1MHz
P1DIR |= LED0 + LED1;
P1SEL |= BIT3;                            //ADC Input pin P1.3
P1OUT &= ~(LED0 + LED1);
__enable_interrupt();                     // Enable interrupts.
__delay_cycles(1000);                   // Wait for ADC Ref to settle
ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
__bis_SR_register(CPUOFF + GIE);        // LPM0 with interrupts enabled
value = ADC10MEM;
if (value<511)
P1OUT &= ~(LED0 + LED1);
P1OUT |= LED0;
P1OUT &= ~(LED0 + LED1);
P1OUT |= LED1;
What I want to do is combine the 2 programs, and remove the parts of the ADC code that flash the LED's.  Then take the unsigned integer value in the ADC code, and then display this on a 16x2 LCD (just the raw data for now will be fine).  I have the code for the LCD and successfully connected the LCD up and can display Hello World, but don't fully understand the LCD code.  LCD code used is from this very helpful website below:
I think I need to change the unsigned integer into a char, or possibly create a new function that mimics the PrintStr function in the LCD code, someone told me I could maybe use the function atoa.  Little out of my depth with the C code now so would really appreciate some help, I am using CCS5.1 at the moment.
Link to post
Share on other sites

When I've done this in the past, I've use a character map and a function to translate an integer to display values.  Basically there's a variable with the LCD code for that particular number in it's indexed location.  For my LCD it's 


const char charMap[10] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};


Then if I have an integer to display (say 341) I do the modulus operator to find the remainder after 10 (1 in this case), send charMap[1] to the display, then divide by 10 and repeat.


Hope this helps

Link to post
Share on other sites

EngIP cheers, this has been suggested to me by a tutor and they also mentioned using the function ITOA, it carries out the divide by 10, and modulus, then basically converts the Integer into a Char as I understand it.  However I am yet to fully understand how to use the ITOA function and implement it, only just started C.


I will persevere, at least I know the direction I am crawling now :-)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...