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:
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();
ADCSRA |= 1<<ADPS2; // prescaler
ADMUX |= 1<<ADLAR; // result left justified(we'll use 8 bits)
ADMUX |= 1<<REFS0; // using AVCC pin as reference
ADCSRA |= 1<<ADIE; // enable ADC interrupts
ADCSRA |= 1<<ADEN; // turn the ADC ON
sei(); // enable global interrupts
ADCSRA |= 1<<ADSC; // start FIRST conversion. Setting bit ADSC will start the conversion
while(1)
{
}
}
void InitLCD()
{
LCDInit(LS_NONE|LS_NONE);
LCDClear();
LCDWriteString("ADC Program ");
_delay_ms(1500);
LCDClear();
}
ISR(ADC_vect)
{
//uint16_t tenBitValue = ADC; // put the ADC value into the tenBitValue variable
itoa(ADC, Buffer, 10); // convert ADC result to base 10 number
LCDWriteStringXY(0,0,Buffer);
ADCSRA |= 1<<ADSC; // start NEXT conversion
}