#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#define LCD_Port PORTD
#define LCD_DPin DDRD
#define RS PD2
#define EN PD3
void LCD_Action( unsigned char cmnd );
void LCD_Init (void);
void LCD_Clear();
void LCD_Print (const char *str);
void LCD_Pos (char row, char pos, char *str);
float x = 0;
unsigned char xchar;
void LCD_Init (void)
{
LCD_DPin = 0xFF; //Control LCD Pins (D4-D7)
_delay_ms(15); //Wait before LCD activation
LCD_Action(0x02); //4-Bit Control
LCD_Action(0x28); //Control Matrix @ 4-Bit
LCD_Action(0x0c); //Disable Cursor
LCD_Action(0x06); //Move Cursor
LCD_Action(0x01); //Clean LCD
_delay_ms(2);
}
void LCD_Action( unsigned char cmnd )
{
LCD_Port = (LCD_Port & 0x0F) | (cmnd & 0xF0);
LCD_Port &= ~ (1<<RS);
LCD_Port |= (1<<EN);
_delay_us(1);
LCD_Port &= ~ (1<<EN);
_delay_us(200);
LCD_Port = (LCD_Port & 0x0F) | (cmnd << 4);
LCD_Port |= (1<<EN);
_delay_us(1);
LCD_Port &= ~ (1<<EN);
_delay_ms(2);
}
void LCD_Clear()
{
LCD_Action (0x01); //Clear LCD
_delay_ms(2); //Wait to clean LCD
LCD_Action (0x80); //Move to Position Line 1, Position 1
}
void LCD_Print (const char *str)
{
int i;
for(i=0; str[i]!=0; i++)
{
LCD_Port = (LCD_Port & 0x0F) | (str[i] & 0xF0);
LCD_Port |= (1<<RS);
LCD_Port|= (1<<EN);
_delay_us(1);
LCD_Port &= ~ (1<<EN);
_delay_us(200);
LCD_Port = (LCD_Port & 0x0F) | (str[i] << 4);
LCD_Port |= (1<<EN);
_delay_us(1);
LCD_Port &= ~ (1<<EN);
_delay_ms(2);
}
}
int main()
{
float n;
LCD_Clear();
LCD_Init();
LCD_Print(" Voltaje: "); //Begin writing at Line 1, Position 1
DDRC = 0; //make Port C an input for ADC input
ADCSRA= 0x8F; //enable and interrupt select ck/128
ADMUX= 0x40; //2.56V Vref and ADC0 single-ended
//input right-justified data
ADCSRA|=(1<<ADSC); //start conversion
while (1){
ADCSRA|=(1<<ADSC); //start conversion
while((ADCSRA&(1<<ADIF))==0);//wait for conversion to finish
//LCD_Clear();
n = ADC&1023;
LCD_Action (0x80);
LCD_Print(" Voltaje: ");
x =n*0.00488;
char string[]="";
//LCD_Action (0xC0); //Jump next line
sprintf(string, "%d", (int)x); //Entero
LCD_Print(string);
sprintf(string, ".");
LCD_Print(string);
sprintf(string, "%d", ((int)(x*100)%100)); //Decimal
LCD_Print(string);
LCD_Print(" V ");
}
}