View previous topic :: View next topic |
Author |
Message |
josepic
Joined: 18 Feb 2020 Posts: 5
|
voltage divider for pic 16f88 |
Posted: Tue Feb 18, 2020 10:03 am |
|
|
Hello friends best regards.
I have made the following code for pic 16f88, it is a voltmeter from 0 to 51Vdc, but I need to make a voltage divider to visualize the voltage on the 16x2 lcd and that this voltage is the same as the voltmeter of the main source.
I hope you can help me thanks.
Code: |
#include <16f88.h>
#device adc=10//Resolucion del ADC
#use delay (clock=4M)
#fuses NOWDT,INTRC,NOMCLR,NOLVP,CPD,WRT,PROTECT
#define LCD_ENABLE_PIN PIN_B2
#define LCD_RS_PIN PIN_B0
#define LCD_RW_PIN PIN_B1
#define LCD_DATA_4 PIN_B4
#define LCD_DATA_5 PIN_B5
#define LCD_DATA_6 PIN_B6
#define LCD_DATA_7 PIN_B7
#include <lcd.c>
///////////////////////////////////////////////////
//Programa Principal//
//////////////////////////////////////////////////
void main()
{
unsigned long valor=0;//Declaracion de variables
float voltaje; //Variable que contendra el resultado
///////////////////////////////////////////////////////////////////////////////////////
//Se habilita el A/D y se declara el PORT a usar//
///////////////////////////////////////////////////////////////////////////////////////
setup_adc( ADC_CLOCK_INTERNAL );
setup_adc_ports(sAN0);
set_adc_channel(0);
//////////////////////////////////////////////
//Se inicia la LCD//
//////////////////////////////////////////////
lcd_init();
lcd_gotoxy(1,1);
lcd_putc("Iniciando.....");
delay_ms(2000);
lcd_putc("\f" ) ;
lcd_gotoxy(4,1);
lcd_putc("VOLTIMETRO");
do//Bucle
{
delay_ms(100);
valor = read_adc();
voltaje = (float)valor*0.004882812*10.44 ;
lcd_gotoxy(5,2);
printf(lcd_putc,"%g V",voltaje);
}while(true);
}
|
jose |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Tue Feb 18, 2020 10:17 am |
|
|
I'd try a 9K---1K voltage divider, using 1% resistors. Be sure to test BEFORE attaching to PIC though.
Also your selection of adc_clock_internal is wrong. See Table 12-1 of the datasheet, NOTE 2. |
|
|
josepic
Joined: 18 Feb 2020 Posts: 5
|
|
Posted: Tue Feb 18, 2020 10:42 am |
|
|
temtronic wrote: | I'd try a 9K---1K voltage divider, using 1% resistors. Be sure to test BEFORE attaching to PIC though.
also your selection of adc_clock_internal is wrong. See Table 12-1 of the datasheet, NOTE 2. |
Tell me why the adc_clock_internal instruction is incorrect?
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Tue Feb 18, 2020 10:45 am |
|
|
Please See and read Table 12-1 of the datasheet, NOTE 2. |
|
|
josepic
Joined: 18 Feb 2020 Posts: 5
|
|
Posted: Tue Feb 18, 2020 10:49 am |
|
|
temtronic wrote: | Please See and read Table 12-1 of the datasheet, NOTE 2. |
On which datasheet page?
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Tue Feb 18, 2020 11:18 am |
|
|
Page 118 of my copy of the datasheet. It's chapter 12, ADC, section 12-2, selecting the ADC clock. |
|
|
josepic
Joined: 18 Feb 2020 Posts: 5
|
|
Posted: Tue Feb 18, 2020 12:08 pm |
|
|
temtronic wrote: | Page 118 of my copy of the datasheet. It's chapter 12, ADC, section 12-2, selecting the ADC clock. |
I just read page 118 and Note 2, I go back and insist if the instruction had an error, would the code not compile me or not?
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Tue Feb 18, 2020 4:58 pm |
|
|
Please post your program so we can see what you did. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Wed Feb 19, 2020 3:27 am |
|
|
For your clock frequency, the correct ADC setup line is:
setup_adc( ADC_CLOCK_DIV_8 );
This should not give an error.
The code will compile with the wrong setting. The compiler only checks
for C syntax errors, not chip functional errors. You can select a wrong
clock, or perform output to a pin wired as an input, and the compiler
will not complain. These faults need you to find them....
The internal ADC clock, will actually 'work', but the ADC accuracy will
be badly degraded. |
|
|
josepic
Joined: 18 Feb 2020 Posts: 5
|
|
Posted: Wed Feb 19, 2020 8:48 pm |
|
|
temtronic wrote: | Please post your program so we can see what you did. |
I have published the code and I have corrected the correct instruction, I have also solved the voltage divider.
Thanks for the help
|
|
|
|