Full Version : LCD & ADC Newbie Example Code (GCC)
avr >>BEGINNERS & BUTTERFLIES >>LCD & ADC Newbie Example Code (GCC)


AVR_Admin- 04-16-2006
LCD and ADC Example for newbieThe code includes:

1. LCD routine
2. Analog-to-Digital(ADC) example

This project is to demonstrate how to use external LCD display and ADC function on ATmega32.

The measure voltage across photoresistor is read and converted into digital value then shows on the LCD display.

CODE

/*
Line diagram :
mega32            LCD Module
PD0        ->     D4
PD1        ->     D5
PD2        ->     D6
PD3        ->     D7
PD4        ->     RS
PD5        ->     R/W
PD6        ->     EN

DDRAM Address :
0x80 to 0x8F  =  1st Line
0xC0 to 0xCF  =  2nd Line

*/
#include <stdio.h>
#include <string.h>
#include <avr\io.h>
#include <avr\delay.h>

#define Plcd PORTD
#define RS 0x10
#define RW 0x20
#define EN 0x40
/* Function Prototype*/
void Toggle_Enable(void);
void Set_RW(void);
void Clear_RW(void);
void Set_RS(void);
void Clear_RS(void);
void LCD_init (void);
void LCD_Send(char c, unsigned char _DC); //_DC : 1 for Data, else for Command
void LCD_Send_String(unsigned char *s);


CODE
//Update Sep 30, 2005 23:50

#include <lcdlib.h>

void Toggle_Enable(void)
{
     Plcd |= EN;
     _delay_ms(1);
     Plcd &= ~EN;
}

void Set_RW(void)
{
     Plcd |= RW;
}

void Clear_RW(void)
{      
     Plcd &= ~RW;
}

void Set_RS(void)
{      
     Plcd |= RS;
}

void Clear_RS(void)
{      
     Plcd &= ~RS;
}

void LCD_init (void)
{
     unsigned char i;
     unsigned char init_const[12] ={     0x03,0x03,0x03,
                                         0x02,
                                         0x02,0x0C,
                                         0x00,0x0C,
                                         0x00,0x01,
                                         0x00,0x06};
     Clear_RS();
     Clear_RW();
     for(i=0;i<12;i++)
     {
           Plcd=init_const[i];
           Toggle_Enable();
           _delay_ms(40);      
     }
     LCD_Send(0x0F,0);      
     LCD_Send(0x01,0);
}

// _DC=1 for Data
// _DC=0 for Command
void LCD_Send(char c, unsigned char _DC)
{
     unsigned char bufferH, bufferL;

     bufferH = c >> 4;
     bufferH = bufferH & 0x0F;      
     bufferL = c & 0x0F;
     if (_DC == 1)
     {      
           Plcd = bufferH;
           Set_RS();
           Clear_RW();
           Toggle_Enable();
           _delay_ms(1);      
       
           Plcd = bufferL;
           Set_RS();
           Clear_RW();  
           Toggle_Enable();
            _delay_ms(1);
     }
     else
     {
           Plcd = bufferH;
           Clear_RS();
           Clear_RW();
           Toggle_Enable();
           _delay_ms(1);      
       
           Plcd = bufferL;
           Clear_RS();
           Clear_RW();  
           Toggle_Enable();
            _delay_ms(1);      
     }
}

void LCD_Send_String(unsigned char *s)
{     unsigned char i;
for( i=0; i < strlen(s);i++)
     {
           LCD_Send( s[i], 1);
     }
}



CODE
#include <avr\io.h>
#include <avr\delay.h>
#include <lcdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int j;

void adc_init(void)
{
     ADMUX = 0b01000000; //0x40
     ADCSRA |= 0x80;
}
int readadchan(char n)
{
//read ch n of internal 10 bit a/d

     ADMUX = n;
     ADMUX |= 0x40;
     ADCSRA |= 0x44;

     while((ADCSRA & 0x40) !=0){}; //wait for conv complete
     //while((ADCSRA & ADIF) ==0);

     return ADC;
}

int main(void)
{
     DDRA = 0x00;
     DDRD = 0xFF;
     PORTD = 0x00;
     LCD_init();
     
     unsigned char *k1;
     unsigned char *k2;
     unsigned char *m1;
     unsigned char *m2;
     k1="";
     k2="";
     
     LCD_Send_String("Read1 : ");
     LCD_Send(0xC0,0);
     LCD_Send_String("Read0 : ");
     
     int a0;
     int a1;
     int s;
     adc_init();
     
     while(1)
     {                
           a1 = readadchan(0x01);
           a0 = readadchan(0x00);

           m1 = itoa(a1,k1,10);
         
           LCD_Send(0x88,0);
           LCD_Send_String(k1);      
           LCD_Send(0xC8,0);
           m1 = itoa(a0,k1,10);
           LCD_Send_String(k1);      
           _delay_ms(100000);
     }
}



Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.