/*
* Analog Voltmeter
*
* Created: 12/1/2023 1:29:09 PM
* Author : VGU1
*/
#include <stdio.h>
#define F_CPU 8000000UL
#include <avr/io.h> /* Include AVR std. library file */
#include <util/delay.h> /* Include inbuilt defined Delay header file */
#define RS PB3 /* Define Register Select (data reg./command reg.) signal pin */
#define EN PB2 /* Define Enable signal pin */
#define lcd_port PORTB
#define lcd_ddr DDRB
#define analog_port PORTA
#define analog_ddr DDRA
float volt;
void ADC_Init(){
PORTA = 0x00; /* Make ADC port as input */
ADCSRA |= (1<<ADEN)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS1); /* Enable ADC, with freq/128 */
// ADMUX |= (1<<REFS0); /* Vref: Avcc, ADC channel: 0 */
}
int ADC_Read(char channel)
{
ADMUX |= (channel & 0x07); /* set input channel to read */
ADCSRA |= (1<<ADSC); /* Start ADC conversion */
while (!(ADCSRA & (1<<ADIF))); /* Wait until end of conversion by polling ADC interrupt flag */
ADCSRA |= (1<<ADIF); /* Clear interrupt flag */
_delay_ms(1); /* Wait a little bit */
return ADCW; /* Return ADC word */
}
void lcd_sending_command( unsigned char cmnd )
{
lcd_port = (cmnd & 0xF0); /* sending upper nibble */
lcd_port &= ~ (1<<RS); /* RS=0, command reg. */
PORTB |= (1<<EN); /* Enable pulse */
_delay_us(1);
lcd_port &= ~ (1<<EN);
lcd_port = (cmnd << 4); /* sending lower nibble */
lcd_port |= (1<<EN);
_delay_us(1);
lcd_port &= ~ (1<<EN);
_delay_ms(2);
}
void lcd_sending_data( unsigned char data )
{
lcd_port = (data & 0xF0); /* sending upper nibble */
lcd_port |= (1<<RS); /* RS=1, data reg. */
lcd_port |= (1<<EN);
_delay_us(1);
lcd_port &= ~ (1<<EN);
lcd_port = (data << 4); /* sending lower nibble */
lcd_port |= (1<<RS);
lcd_port |= (1<<EN);
_delay_us(1);
lcd_port &= ~ (1<<EN);
_delay_ms(2);
}
void lcd_initialization (void) /* LCD Initialize function */
{
lcd_ddr = 0xFF; /* Make LCD command port direction as o/p */
_delay_ms(20); /* LCD Power ON delay always >15ms */
lcd_sending_command(0x33); /* Ensures display is in 8-bit mode */
lcd_sending_command(0x32); /* send for 4 bit initialization of LCD */
lcd_sending_command(0x28); /* Use 2 line and initialize 5*7 matrix in (4-bit mode)*/
lcd_sending_command(0x0f); /* Display on, cursor on and blink cursor*/
lcd_sending_command(0x01); /* Clear display screen*/
lcd_sending_command(0x06); /* Clear display screen*/
lcd_sending_command(0x02); /* Clear display screen*/
_delay_ms(2);
}
void lcd_display_string (char *str) /* Send string to LCD function */
{
int i;
for(i=0;str[i]!=0;i++) /* Send each char of string till the NULL */
{
lcd_sending_data (str[i]);
}
}
void lcd_string_float(float LCD_number, int LCD_line, int LCD_cursor_position)
{
char buffer_LCD[13];
dtostre(LCD_number, buffer_LCD, 5, 4);
lcd_display_string(buffer_LCD);
}
int main()
{
char Voltage[10];
ADC_Init(); /* initialize ADC*/
int i;
lcd_initialization(); /* Initialization of LCD*/
lcd_display_string("DC Voltage");
lcd_sending_command(0xc0); /* Go to 2nd line*/
while (1) {
volt = (float)ADC_Read(3)*3.3/1024;
// volt = (volt/10.00);
lcd_sending_command(0xc0);
lcd_sending_command(0x0C);
lcd_string_float(volt,2,1 );/* send string data for printing */
lcd_display_string(" V");
_delay_ms(10);
}
}