CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

voltage divider for pic 16f88

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
josepic



Joined: 18 Feb 2020
Posts: 5

View user's profile Send private message

voltage divider for pic 16f88
PostPosted: Tue Feb 18, 2020 10:03 am     Reply with quote

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);
  }

Question

jose
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 18, 2020 10:17 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Feb 18, 2020 10:42 am     Reply with quote

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?

Question Question
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 18, 2020 10:45 am     Reply with quote

Please See and read Table 12-1 of the datasheet, NOTE 2.
josepic



Joined: 18 Feb 2020
Posts: 5

View user's profile Send private message

PostPosted: Tue Feb 18, 2020 10:49 am     Reply with quote

temtronic wrote:
Please See and read Table 12-1 of the datasheet, NOTE 2.


On which datasheet page?

Question Question
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 18, 2020 11:18 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Feb 18, 2020 12:08 pm     Reply with quote

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?

Question Question
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 18, 2020 4:58 pm     Reply with quote

Please post your program so we can see what you did.
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Wed Feb 19, 2020 3:27 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 19, 2020 8:48 pm     Reply with quote

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
Wink
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group