#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdlib.h>
#define lcd_DPRT PORTD //LCD DATA PORT
#define lcd_DDDR DDRD //LCD DATA DDR
#define lcd_DPIN PIND //LCD DATA PIN
#define lcd_CPRT PORTB //LCD COMMANDS PORT
#define lcd_CDDR DDRB //LCD COMMANDS DDR
#define lcd_CPIN PINB //LCD COMMANDS PIN
#define lcd_RS 4 //LCD RS
#define lcd_EN 5 //LCD EN
#define lcd_POT 1 //LCD Potentiometer
#define pot_DDRD DDRC //LCD DATA DDR
float x = 0;
char Vol[] = (" Voltage:");
char dot[] = (".");
void lcdCommand( unsigned char cmnd ) {
lcd_DPRT = cmnd & 0xF0; //send high nibble to D4-D7
lcd_CPRT &= ~ (1 << lcd_RS); //RS = 0 for command
lcd_CPRT |= (1 << lcd_EN); //EN = 1 for H-to-L pulse
_delay_us(1); //make EN pulse wider
lcd_CPRT &= ~ (1 << lcd_EN); //EN = 0 for H-to-L pulse
_delay_us(100); //wait
lcd_DPRT = cmnd << 4; //send low nibble to D4-D7
lcd_CPRT |= (1 << lcd_EN); //EN = 1 for H-to-L pulse
_delay_us(1); //make EN pulse wider
lcd_CPRT &= ~ (1 << lcd_EN); //EN = 0 for H-to-L pulse
_delay_us(100); //wait
}
void lcdData( unsigned char data ) {
lcd_DPRT = data & 0xF0; //send high nibble to D4-D7
lcd_CPRT |= (1 << lcd_RS); //RS = 1 for data
lcd_CPRT |= (1 << lcd_EN); //EN = 1 for H-to-L pulse
_delay_us(1); //make EN pulse wider
lcd_CPRT &= ~ (1 << lcd_EN); //EN = 0 for H-to-L pulse
lcd_DPRT = data << 4; //send low nibble to D4-D7
lcd_CPRT |= (1 << lcd_EN); //EN = 1 for H-to-L pulse
_delay_us(1); //make EN pulse wider
lcd_CPRT &= ~ (1 << lcd_EN); //EN = 0 for H-to-L pulse
_delay_us(100); //wait
}
void lcd_init() {
lcd_DDDR = 0xFF;
lcd_CDDR = 0xFF;
lcd_CPRT &= ~(1 << lcd_EN); //lcd_EN = 0
lcdCommand(0x33); //send $33 for init.
lcdCommand(0x32); //send $32 for init.
lcdCommand(0x28); //init. LCD 2 line,5×7 matrix
lcdCommand(0x0e); //display on, cursor on
lcdCommand(0x01); //clear LCD
_delay_us(2000);
lcdCommand(0x06); //shift cursor right
}
void lcd_gotoxy(unsigned char x, unsigned char y) {
unsigned char firstCharAdr[] = {0x80, 0xC0, 0x94, 0xD4};
lcdCommand(firstCharAdr[y - 1] + x - 1);
_delay_us(100);
}
void lcd_print(char * str ) {
unsigned char i = 0;
while (str[i] != 0)
{
lcdData(str[i]);
i++ ;
}
}
int main(void)
{
lcd_init();
float Number;
pot_DDRD |= (1 << lcd_POT); //Potentiometer in
ADCSRA = 0x8F; //enable and interrupt select ck/128
ADMUX = 0x44; //5V Vref - input right-justified data
ADCSRA |= (1 << ADSC); //start conversion once
while (1) { //stay here forever
ADCSRA |= (1 << ADSC); //start conversion once
while ((ADCSRA & (1 << ADIF)) == 0); //wait for conversion to finish
Number = ADC;
lcd_gotoxy(1, 1);
lcd_print(Vol);
x = Number / 1024;
x = x * 5;
char voltage_str[] = "";
//Enteros
lcd_gotoxy(1, 2);
sprintf(voltage_str, "%d", (int)x);
lcd_print(voltage_str);
//Decimales
lcd_print(dot);
sprintf(voltage_str, "%d", ((int)(x * 100) % 100) );
lcd_print(voltage_str);
lcd_gotoxy(5, 2);
_delay_ms(100);
}
return 0;
}