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

PIC16F77A - ADC resolution

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



Joined: 06 May 2025
Posts: 1

View user's profile Send private message

PIC16F77A - ADC resolution
PostPosted: Tue May 06, 2025 9:57 pm     Reply with quote

I'm working on the ADC module of PIC16F877A. The schematic is very simple, including a voltage divider connected to RA0: https://uomustansiriyah.edu.iq/media/lectures/5/5_2020_06_14!01_18_34_AM.pdf

What I do not understand is the resolution of the module. The ADC module has 10-bit resolution as read in the datasheet. I was guided that I should put #device ADC = 10 in the code. The real value would be read_adc() * 5/1023 . Everything is fine. My code is as follows:

Code:

#include <16f877a.h>
#include <def_877a.h>
#DEVICE ADC=10 //return 10 bit
#fuses HS, NOWDT, NOPROTECT, NOLVP, NODEBUG, NOBROWNOUT, NOCPD, NOWRT
#use delay(clock=8MHz)

#define LCD_RS_PIN PIN_A2
#define LCD_RW_PIN PIN_A3
#define LCD_ENABLE_PIN PIN_A5
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#define LCD_DATA0 PIN_D0
#define LCD_DATA1 PIN_D1
#define LCD_DATA2 PIN_D2
#define LCD_DATA3 PIN_D3

#include <lcd.c>

float voltage;

void main() {
    setup_adc_ports(AN0);
    setup_adc(ADC_CLOCK_DIV_2);
    lcd_init();

    while(true) {
        set_adc_channel(0);
        delay_us(10); 
        voltage = (read_adc()*5.0)/1023;

        lcd_gotoxy(1, 1);
        lcd_putc("DHCN");   
        lcd_gotoxy(1, 2);
        printf(lcd_putc, "%1.2fV", voltage);
    }
}

But when I do not put the directive #device ADC = 10, the voltage must be divided by 65535 to get the correct voltage. OR, I can use ADC = 16 instead (?).

I'd highly appreciate it if someone can explain this. Thank you very much.
temtronic



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

View user's profile Send private message

PostPosted: Wed May 07, 2025 5:28 am     Reply with quote

pretty sure it's the printf format.

"%1.2fV"

to me 1.2 is bad, '1' is total number of digits, '2' is number of digits after decimal point

try '4.2' and see what happens.

The manual isn't very clear about this,2 or 3 more example would be nice.
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