Status
Not open for further replies.

#### Cantafford

##### Member
Hey,

I'm reading the value of a POT with the ADC of ATMega32 and displaying it on an LCD.
I have initialised the ADC to 10 bit result left justified. I convert the result to a string then try to display it on a LCD.

I have one problem. When the pot is at it's highest value I get 1023 displayed on my LCD. All good. But when I move it downwards my values don't coincide with the position of the pot. I get weird values like 6035 or 9055 that shouldn't be there at all no matter the position of the pot. Please help me correct this.

This is the code:
Code:
/*
* AVRGCC1.c
*
* Created: 1/10/2017 10:10:28 AM
*  Author: Paul
*/

#include <avr/io.h>
#include <stdlib.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "lcd.h"

void InitLCD();

char Buffer[4];

int main(void)
{
InitLCD();

ADMUX  |= 1<<REFS0; // using AVCC pin as reference
sei();                // enable global interrupts

while(1)
{
}
}

void InitLCD()
{
LCDInit(LS_NONE|LS_NONE);
LCDClear();
_delay_ms(1500);
LCDClear();
}

{
//uint16_t tenBitValue = ADC; // put the ADC value into the tenBitValue variable

LCDWriteStringXY(0,0,Buffer);
}

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Looks to me like you are writing a three digit number and a two digit number over a four digit number..

You need to use sprintf() rather than itoa() with sprintf() you can do this

and then it will print right justified and it will look correct..

#### wkrug

##### Active Member
Another problem:
You should never put an output or an math routine into an interrupt.
While this interrupt runs no other interrupt can be computed. That will give You ugly overrun problems.
It's better to store the ADC Result into a variable ( like adc_value ), set a marker ( variable e.g. NEWADC ) and make the Output in a function in the main loop.
Like:
char main (void)
{...
...
while(1)
{...
...
if ( NEWADC > 0 )
{
}
...
}
....
return(0);
}

{