EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Replies / Hi Frnd,Try this

Hi Frnd,Try this

|

Microcontroller › AVR › to display analog value of potentiometr on lcd using atmega 32 › Hi Frnd,Try this

August 24, 2012 at 11:45 am #8505
Amrith
Participant

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 != ‘’)
    USART_Transmit(*str++);
}

void Send_floatvalue(int value, char dp)
{
    unsigned char buffer[10];
    int i=0;
   
    if(value < 0)
       USART_Transmit(‘-‘);
   
    do                    
    {
        buffer[i++] = (value % 10)+0x30;
        value /= 10;
        if(i==dp)
        {  
            buffer[i++] = ‘.’;
            if(value == 0)
               buffer[i++] = ‘0’;
        }   
    }while(value);
   
    while(i>=1)
        USART_Transmit(buffer);
}
void Init_ADC()
{
    ADMUX = 0xE0;   //Enable Internal Reference voltage
    ADCSRA = 0x86;
}

unsigned int Read_ADC_Channel(unsigned char adc_pin)
{
    unsigned int adcH,adcL;
    while( (ADCSRA &0x40) != 0);  //wait till previous Convertion is Completed
    ADMUX = adc_pin|0xE0;         //select ADC Channel
    Start_ADC_Convertion(1);      //Start ADC Convertion            
    while((ADCSRA & 0x10)==0);    //Wait till Current Covertion is completed
   
    adcL = ADCL;
    adcH = ADCH;
    return ((adcH << 2) | (adcL>>6));
}

/*void Delay_ms(int msec)
{
    int i,j;
   
    for(i=0;i<msec;i++)
        for(j=0;j<1000;j++);
}     */

void Display_Voltages(void)
{
   float Calib = G_Calib;//48.514;
   unsigned long  ADC_Value;
   unsigned int voltage = 0;
  
   //read Adc channel 0
   ADC_Value = Read_ADC_Channel(ADC_CHANNEL_0);
   voltage = ((float)ADC_Value/Calib)*100;
  
   Send_String(“AI1: “);
   Send_floatvalue(voltage,2);
   Send_String(”    “);
  
  
  //read Adc channel 2
   ADC_Value = Read_ADC_Channel(ADC_CHANNEL_1);
   voltage = ((float)ADC_Value/Calib)*100;
   Send_String(“AI2: “);
   Send_floatvalue(voltage,2);
   Send_String(”    “);

  /*  //read Adc channel 2
   ADC_Value = Read_ADC_Channel(ADC_CHANNEL_2);
   voltage = ((float)ADC_Value/Calib)*100;
  
   Send_String(“AI3: “);
   Send_floatvalue(voltage,2);
   Send_String(”    “); */
        
   Send_String(“rn”); 
  
   Display_PinStatus();   
}

void Display_PinStatus()
{
    Send_String(“DI1: “);
   
    if(PINB.6 == 1) 
    {
        USART_Transmit(‘1’);
        Send_String(”    “);
        LCD_DisplayString_F(2,1,”D1:”); 
        LCD_DisplayString_F(2,4,”1″);
      LCD_DisplayString_F(2,5,” “);
    }
    else 
    {
        USART_Transmit(‘0’);
        Send_String(”    “);
        LCD_DisplayString_F(2,1,”D1:”); 
        LCD_DisplayString_F(2,4,”0″);
        LCD_DisplayString_F(2,5,” “);
    }
    
    
     Send_String(“DI2: “);
   
    if(PINB.7 == 1) 
    {
        USART_Transmit(‘1’);
        Send_String(”    “);
        LCD_DisplayString_F(2,10,”D2:”); 
        LCD_DisplayString_F(2,13,”1″);
        LCD_DisplayString_F(2,14,” “);
    }
   
    else 
    {
        USART_Transmit(‘0’);
        Send_String(”    “);
        LCD_DisplayString_F(2,10,”D2:”); 
        LCD_DisplayString_F(2,13,”0″);
        LCD_DisplayString_F(2,14,” “);
    }
     Send_String(“rn”); 
     Send_String(“rn”);
}
void main(void)
{
   // unsigned char Change_Display=0, Change_Display1=1; 
    Init_UART();
    Init_ADC();

   
    DDRA &= (~0x07);  //set pins 0,1,2 as input pins    
    DDRB = 0x0F;
 DDRC = 0xFF;
 //DDRD = 0x00;
 PORTB = 0x00;
    init_devices();
    delay_ms(100);
    LCD_DisplayString_F(1,1,” Welcome To GPS “);
    LCD_DisplayString_F(2,1,”Simulation Tool”);
    delay_ms(5000);  
    LCD_WriteCommand (0x01);
    LCD_DisplayString_F(1,1,”Intializing  “); 
    LCD_DisplayString_F(2,1,”Please Wait…..”);
    delay_ms(5000);
    while(1)
    {  
        LCD_DisplayString_F(1,1,”A1:”);
        LCD_DisplayFloat(1,4,Get_Voltage_fp2(0x00),2);
        LCD_DisplayString_F(1,8,”  “);
        LCD_DisplayString_F(1,10,”A2:”);
        LCD_DisplayFloat(1,13,Get_Voltage_fp2(0x01),2);
        Send_String(“GPS Device Simulation”);
        Send_String(“rn”);
        Display_Voltages();
    }
}

// ********************** END **************************

RSS Recent Posts

  • Do i need a buffer? February 7, 2026
  • ANOTHER OLD PROJECT REDO February 7, 2026
  • wall transformer polarity February 7, 2026
  • Supply vs performance query February 7, 2026
  • BPF February 7, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise