#include<string.h>
#include<avr/io.h>
#define adc_vol(a) (a/1023.0)*5 // for adc voltage conversion
char str[10] ; // for string
void setup(){
delay(55);
Serial.begin(9600);
DDRB|=(1<<DDB3)|(1<<DDB4); // enable and rw pin
DDRD = 0xF0; // pins 4to 7
lcd_initialize();
setCursor(0,0);
lcdWrite(1,"VOL:");
}
void loop()
{
int adc1= read(0);
double v1= adc_vol(adc1);
setCursor(0,1);
String a=String(v1,6);
strcpy(str, a.c_str()); //to convert String to char array
lcdWrite(1, str);
delay(100);
}
/*ADC reading*/
int read(int pin)
{
ADMUX=0x00;
ADMUX |= (1 << REFS0)|pin; //setting channel
ADCSRA=0x00;
ADCSRA |= (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
ADCSRA |= (1 << ADEN); //to enable ADC
ADCSRA |= (1 << ADSC); //to start ADC conversion
while(ADCSRA & (1<<ADSC));
int adc= (ADCL|(ADCH<<8));
return adc;
}
/*LCD Command writting*/
void lcdCmdWrite (int select,char data)
{
if (select==1) //0- command mode 1- data mode
PORTB|=(1<<PORTB4);
else
PORTB&=~(1<<PORTB4);
PORTD |= (data& 0xF0); //data assigning MSB
/*enabling */
PORTB|=(1<<PORTB3);
delay(15);
PORTB&=~(1<<PORTB3);
delay(15);
PORTD=0x00; //resetting data pins
PORTD |= (data& 0x0F)<<4; //next Data assigning LSB
/*enabling */
PORTB|=(1<<PORTB3);
delay(15); //delay as per datasheet
PORTB&=~(1<<PORTB3);
delay(15);
PORTD=0x00; //resetting
}
void lcdWrite (int select, char data_l[16])
{
char data;
int len=strlen(data_l);
for(int i=0;i<len;i++)
{
if(i==16) // next line code
lcdCmdWrite(0,0xC0);
data=data_l[i]; //Passing string as character for cmd write
lcdCmdWrite(1,data);
}
}
/*initalize lcd*/
void lcd_initialize()
{
lcdCmdWrite(0,0x02); //return home
lcdCmdWrite(0,0x28); //4 bit mode
lcdCmdWrite(0,0x0C); //display on, cursor off
lcdCmdWrite(0,0x06); // cursor increment
lcdCmdWrite(0,0x01); //clear display
}
/* setting cursor*/
void setCursor(int col, int row)
{
lcdCmdWrite(0, 0x80 | (col + ((row==0)?0x00:0x40)));
}