jean12
Advanced Member level 2
Hey,I would like to implement a digital voltmeter with PIC16F877A and an 16*2 LCDbut the conversion is not succeeding,the voltmeter has to measure 0-30VDC but I am not getting good values.
Could you please help see the codes here below:
For voltage divider I used 750ohm and 3.9Kohm as resistors for reducing the voltage up to approximately 5V,in parallel to the 750ohm I placed a zener diode of 5.1V,unfortunately when calculating
Vout(max)=750*30/(3900+750)=4.838Volts.
Please help to handle!!
Could you please help see the codes here below:
PHP:
#include <16f877A.h> //Header declaration for pic used
#DEVICE ADC=10 //definition of ADC bits for conversation purpose
//Fuses definition for the PIC
#fuses HS //definition of the quartz in use High Frequency =4MHZ
#fuses NOWDT //Disable internal Timer watchdog
#fuses NOLVP //Disable programmation at low voltage
#fuses NODEBUG //disable test of codes at runtime
#fuses NOCPD //Avoid protection of codes
#use delay(clock=4M) //Set the values of Quartz to use
//Definition of pins for connecting the LCD:Light Crystal Display
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#define LCD_RW_PIN PIN_D3
#define LCD_ENABLE_PIN PIN_D1
#define LCD_RS_PIN PIN_D2
#include <lcd.c>
float volt1,volt2; //Variable declaration
void main() //Initialization of the main function
{
set_tris_d(0x00); //definition of ports
lcd_init(); //Initialization of the LCD to be used
delay_us(5); //set of a delay of 5uS for ADC to start operating
setup_adc_ports(AN0); //Set the pin where input is connected
Setup_adc(ADC_CLOCK_INTERNAL); //set the clock used by the internal ADC Module
set_adc_channel(0); //set the channel from which the ADC will be reading the data from
setup_vref(FALSE); //use of 5V of the power supply as a reference voltage
while(TRUE) //define the endless loop for measuring the voltage
{
delay_us(10); //set a simple delay for the ADC to start reading
read_adc(ADC_START_ONLY);//Start reading the ADC value
delay_us(2); //set a simple delay for the ADC to start reading
volt1=read_adc();//start reading and store the value in variable volt1
volt2=(volt1*4.88)/1023; //conversion to decimal number
if(volt2==0)//Condition for displaying the text before priniting out on the LCD
{
delay_ms(20);
lcd_gotoxy(2,1);
printf(lcd_putc,"vin is needed!!"); //printing the text on the LCD
}
else//in case the input is connected the value has to be printed out here below
lcd_gotoxy(1,1);
printf(lcd_putc,"\connected vin is:");
delay_ms(600);
lcd_gotoxy(2,2);
printf(lcd_putc,"vin=%f V",volt2);
delay_us(200);//delay for reading and displaying the value on the screen(LCD)
}
}
Vout(max)=750*30/(3900+750)=4.838Volts.
Please help to handle!!