Microcontroller › AVR › Need Help – Decimal Point not Getting Displayed
- This topic has 1 reply, 1 voice, and was last updated 12 years, 10 months ago by Rahul Tiwari.
-
AuthorPosts
-
January 20, 2012 at 6:16 pm #1726Rahul TiwariParticipant
Resp Members,
I am working on Atmega16. On a local dev board, I am trying to read the ADC Channel 0, convert it into voltage level and then display it on the LCD Continuously.
Well I havent developed the complete code myself. I have written the LCD PART but not the ADC part. I didnt understand it fully but managed to make it work for me.
Here is the code:
#include<avr/io.h>
#include<util/delay.h>#define LCD_DATA PORTC // LCD data port
#define ctrl PORTA
#define en PA2 // enable signal
#define rw PA1 // read/write signal
#define rs PA3 // register select signal
void LCD_cmd(unsigned char cmd);
void init_LCD(void);
void LCD_write(unsigned char data);
void LCD_string1(const char *s);
char s;#define ADC_ENABLE ADCSRA |= (1<<ADEN)
#define ADC_DISABLE ADCSRA &= 0x7F
#define ADC_START_CONVERSION ADCSRA |= (1<<ADSC)void ADC_init(void);
int ADC_read(void);
float ADC_calculateTemp(int);
unsigned char* updateTempDisplay(float);
unsigned char* temporary(void);
void ADC_displayValue(void);
void port_init(void);
void init_devices(void);unsigned char valueDisplay[]=”: . VOLT”; //11 char global string for
//voltage value displayfloat Vref = 5.00; // Reference voltage Vref of ADC
//
void port_init(void)
{
DDRC=0xff; // making LCD_DATA port as output port
DDRA=0x0E; // making signal as out put
DDRB=0xFF;
DDRD=0x03;
}void init_devices(void)
{
port_init();
init_LCD();
ADC_init();
}
void ADC_init(void) // Initialization of ADC
{
ADMUX=(1<<REFS0); // AVcc with external capacitor at AREF
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
// Enable ADC and set Prescaler division factor as 128
}
/*
unsigned int ADC_read(unsigned char ch)
{
ch= ch & 0b00000111; // channel must be b/w 0 to 7
ADMUX |= ch; // selecting channel
ADCSRA|=(1<<ADSC); // start conversion
while(!(ADCSRA & (1<<ADIF))); // waiting for ADIF, conversion complete
ADCSRA|=(1<<ADIF); // clearing of ADIF, it is done by writing 1 to it
return (ADC);
} *//********************************************************************
* Purpose : Do a Analog to Digital Conversion
* Paramtr : none
* return : integer voltage value
********************************************************************/int ADC_read(void)
{
char i;
int ADC_temp, ADCH_temp;
int ADC_var = 0;ADC_ENABLE;
ADC_START_CONVERSION; //do a dummy readout first
while(!(ADCSRA & 0x10)); // wait for conversion done, ADIF flag active
ADCSRA|=(1<<ADIF);for(i=0;i<8;i++) // do the ADC conversion 8 times for better accuracy
{
ADC_START_CONVERSION;
while(!(ADCSRA & 0x10)); // wait for conversion done, ADIF flag set
ADCSRA|=(1<<ADIF);ADC_temp = ADCL; // read out ADCL register
ADCH_temp = ADCH; // read out ADCH register
ADC_temp +=(ADCH_temp << ;
ADC_var += ADC_temp; // accumulate result (8 samples)
//for later averaging
}ADC_var = ADC_var >> 3; // average the 8 samples
ADC_DISABLE;
return ADC_var;
}/**************************************************************************
* Purpose : To calculate temp in degre celsius
* Paramtr : Integer value of voltage, received from ADC read
* Return : float voltage value
***************************************************************************/float ADC_calculateValue(int inputValue)
{
float actualValue;actualValue=(inputValue * Vref/1024.0); //calculates the voltage present
return actualValue;
}/***************************************************************************
* Purpose : To update the tempDisplay string based on the latest temp read
* Paramtr : Float value of voltage
* Return : String pointer pointing to the updated valueDisplay
***************************************************************************/
unsigned char* updateDisplay(float actualValue)
{
int temp;
unsigned char c;
temp=(int)(actualValue*100.0); //to include decimal point for displayif((actualValue*100.0 – temp) >= 0.5) temp=temp+1;
valueDisplay[6] = ((unsigned char)(temp%10)) | 0x30;
temp=temp/10;
valueDisplay[5] = ((unsigned char)(temp%10)) | 0x30;
temp=temp/10;
valueDisplay[3] = ((unsigned char)(temp%10)) | 0x30;
temp=temp/10;return valueDisplay;
}
/**************************************************************************
* Purpose : To display value of voltage applied to selected ADC channel on
the LCD
* Paramtr : None
* Return : None
***************************************************************************/void ADC_displayValue(void)
{
int value;
float value1;value = ADC_read();
value1 = ADC_calculateValue(value);
updateDisplay(value1);
LCD_string1(valueDisplay); //voltage display on 2nd row,
//4th column
}void init_LCD(void)
{
LCD_cmd(0x38); // initialization of 16X2 LCD in 8bit mode
_delay_ms(1);
LCD_cmd(0x01); // clear LCD
_delay_ms(1);
LCD_cmd(0x0F); // cursor ON
_delay_ms(1);
LCD_cmd(0x80); // —8 go to first line and –0 is for 0th position
_delay_ms(1);
return;
}
void LCD_cmd(unsigned char cmd)
{
LCD_DATA=cmd;
ctrl =(0<<rs)|(0<<rw)|(1<<en); // RS and RW as LOW and EN as HIGH
_delay_ms(1);
ctrl =(0<<rs)|(0<<rw)|(0<<en); // RS, RW , LOW and EN as LOW
_delay_ms(50);
return;
}
void LCD_write(unsigned char data)
{
LCD_DATA= data;
ctrl = (1<<rs)|(0<<rw)|(1<<en); // RW as LOW and RS, EN as HIGH
_delay_ms(1);
ctrl = (1<<rs)|(0<<rw)|(0<<en); // EN and RW as LOW and RS HIGH
_delay_ms(50); // delay to get things executed
return ;
}/* void LCD_string1(unsigned char string1[])
{
for (i=0;i<x;i++)
{
LCD_write(string1);
_delay_ms(1);
}
}
*/void LCD_string1(const char *s)
/* print string on lcd (no auto linefeed) */
{
register char c;while ( (c = *s++) ) {
LCD_write(c);
_delay_ms(10);
}}
void main(void)
{
init_devices();
LCD_cmd(0x80);
LCD_string1(“ADC Testing…”);
LCD_cmd(0xC0);
while(1)
{
ADMUX &= 0xe0;
ADMUX |= 0; //select ADC channel
ADC_displayValue();
_delay_ms(5000);
LCD_cmd(0xC0);
}
}Well the actual problem is that the Decimal Point is not getting displayed. Its blank at that spot, 0xC3 but it should be a Decimal/Full Stop Point over there. I have put it in tha Array but still not showing. I tried to put decimal elsewhere in the ADC Testing string and its showing but in the 2nd row its not showing
Need HELP!!!
January 20, 2012 at 6:22 pm #7060Rahul TiwariParticipantAhhh, I got my error
Well I had misplaced it actually
-
AuthorPosts
- You must be logged in to reply to this topic.