Microcontroller › AVR › to display analog value of potentiometr on lcd using atmega 32 › Hi Frnd,Try this
Hi Frnd,
Try this code:
//***********************************************************************
//*********** PROGRAM FOR DISPLAYING ADC VALUE ON LCD ************
//***********************************************************************
//Controller: ATmega8535 (Crystal: 8 Mhz)
//Compiler: CodeVisionAVR
//Author: Amrith Thakur
//********************************************************
#include <mega8535.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <delay.h>
void port_init(void)
{
DDRB = 0x0F;
PORTB = 0x00;
DDRC = 0xFF;
PORTC = 0x00;
DDRD = 0xFF;
PORTD = 0x00;
}
// ************* LCD.h : Header file *************
void LCD_init(void);
void LCD_WriteData (unsigned char Data);
void LCD_DisplayString_F(char row, char column, const unsigned char *string);
void LCD_Cursor(char row, char column);
void LCD_WriteCommand (unsigned char Command);
void Display_PinStatus();
void LCD_DisplayFloat (char row, char column ,int value,char fp);
int Get_Voltage_fp2(char channel);
void Init_UART();
void USART_Transmit( unsigned char data);
void Send_String(char flash *str);
void Send_floatvalue(int value, char dp);
void Init_ADC();
unsigned int Read_ADC_Channel(unsigned char adc_pin);
#define ENABLE_LCD PORTB |= 0x04
#define DISABLE_LCD PORTB &= ~0x04
#define SET_LCD_DATA PORTB |= 0x01
#define SET_LCD_CMD PORTB &= ~0x01
#define pushButton1_PRESSED !(PINB & 0x80)
#define pushButton1_OPEN (PINB & 0x80)
#define Enable_ADC(x) (x) ? (ADCSRA |= 0x80) : (ADCSRA &= (~0x80))
#define Start_ADC_Convertion(x) (x) ? (ADCSRA |= 0x40) :(ADCSRA &= (~0x40))
#define ADC_CHANNEL_0 0x00
#define ADC_CHANNEL_1 0x01
#define ADC_CHANNEL_2 0x02
#define G_Calib 46.900
//call this routine to initialize all peripherals
void init_devices(void)
{
port_init();
LCD_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
}
//******************* MAIN FUNCTION *******************
void LCD_init(void)
{
delay_ms(100); // wait for 100ms
LCD_WriteCommand (0x38); // 8 data lines
//LCD_WriteCommand (0x06); // cursor setting
LCD_WriteCommand (0x0f); // display ON
LCD_WriteCommand (0x01); // clear LCD memory
delay_ms (10); // 10ms delay after clearing LCD
}
// **********************************************
// *** Write a command instruction to the LCD ***
// **********************************************
void LCD_WriteCommand (unsigned char Command)
{
SET_LCD_CMD; // Set LCD in command mode
PORTC = Command; // Load data to port
ENABLE_LCD; // Write data to LCD
delay_ms(1);
DISABLE_LCD; // Disable LCD
delay_ms(1); // wait for 1ms
}
// *****************************************
// *** Write one byte of data to the LCD ***
// *****************************************
void LCD_WriteData (unsigned char Data)
{
SET_LCD_DATA; // Set LCD in data mode
PORTC = Data; // Load data to port
ENABLE_LCD; // Write data to LCD
delay_ms(1);
DISABLE_LCD; // Disable LCD
delay_ms(1); // wait for 1ms
}
// ************************************************************
// Display a string at the specified row and column, from FLASH
//*************************************************************
void LCD_DisplayString_F (char row, char column ,const unsigned char *string)
{
LCD_Cursor (row, column);
while (*string)
LCD_WriteData(*string++);
}
// ***************************************************
// *** Position the LCD cursor at “row”, “column”. ***
// ***************************************************
void LCD_Cursor (char row, char column)
{
switch (row)
{
case 1: LCD_WriteCommand (0x80 + column – 1); break;
case 2: LCD_WriteCommand (0xc0 + column – 1); break;
default: break;
}
}
void LCD_DisplayFloat (char row, char column ,int value,char fp)
{
int i=0;
unsigned char buffer[10];
LCD_Cursor (row, column);
if(value < 0)
LCD_WriteData(‘-‘);
value = abs(value);
do
{
buffer[i++] = (value % 10)+0x30;
value /= 10;
if(i==fp)
{
buffer[i++] = ‘.’;
if(value == 0)
buffer[i++] = ‘0’;
}
}while(value);
while(i>=0)
LCD_WriteData(buffer);
}
int Get_Voltage_fp2(char channel)
{
float Calib = G_Calib;
unsigned long ADC_Value;
unsigned int voltage = 0;
//read Adc channel
ADC_Value = Read_ADC_Channel(channel);
voltage = ((float)ADC_Value/Calib)*100;
return voltage;
}
void Init_UART()
{
UCSRA = 0x00;
UCSRB = 0x98;
UCSRC = 0x06;
UBRRH = 0x00;
UBRRL = 0x33;
SREG = 0x80;
}
void USART_Transmit( unsigned char data )
{
UDR = data;
while((UCSRA & 0x40) == 0);
UCSRA = 0;
delay_ms(10);
}
void Send_String(char flash *str)
{
while(*str != ‘