Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Pic 16f877A (analog digital converter)

Status
Not open for further replies.

ryujinaga

Newbie level 3
Joined
Apr 6, 2012
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
malaysia
Activity points
1,310
hello

i try to read ADC at pin AN0 from a potentiometer and display the digital measurement.the max output voltage of the potentiometer is 5V and to get the digital measurement=vin/5x1023...but then at my lcd show measurement 255 instead 1023. from datasheet, the pic is 10 bit. please explain to me why this happen???

the coding


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <16F877A.h>                // type of micricontroller
#fuses HS,NOWDT,NOLVP,NOPROTECT    // high speed oscillator, no watch dog timer
#use delay (clock=20M)             //20MHz crystal
#include <lcd.c>
 
 
 
     #define LCD_ENABLE_PIN  PIN_D0                                    ////set lcd port
     #define LCD_RS_PIN      PIN_D1                                    ////
     #define LCD_RW_PIN      PIN_D2                                    ////
     #define LCD_DATA4       PIN_D4                                    ////
     #define LCD_DATA5       PIN_D5                                    ////
     #define LCD_DATA6       PIN_D6                                    ////
     #define LCD_DATA7       PIN_D7 
float ALL;
 
void main()
{
  set_tris_b(0x00);    //set port b as output
  set_tris_C(0x00);    //set port c as output
  output_b(0x00);      //reset port b
  output_c(0x00);      //reset port c
  
  setup_adc(ADC_CLOCK_INTERNAL); // set internal clck for ADC
  setup_adc_ports(ALL_ANALOG);          //set s analog input
  
  lcd_init();
  while (true)
  {
    
    delay_ms(200);
    set_adc_channel(0);
    ALL=read_adc();
    lcd_gotoxy(1,1);
    printf(lcd_putc,"\fVDIG=%0.1f",(float)ALL);
    
   
   
   
  
   
  }
 
}


best regard
 
Last edited by a moderator:

You haven't said which compiler you are using but as a guess, the problem is the "read_adc()" function doesn't return a floating point number.
Check your documentation to confirm this.

Brian.
 
hello

i try to read ADC at pin AN0 from a potentiometer and display the digital measurement.the max output voltage of the potentiometer is 5V and to get the digital measurement=vin/5x1023...but then at my lcd show measurement 255 instead 1023. from datasheet, the pic is 10 bit. please explain to me why this happen???

the coding


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <16F877A.h>                // type of micricontroller
#fuses HS,NOWDT,NOLVP,NOPROTECT    // high speed oscillator, no watch dog timer
#use delay (clock=20M)             //20MHz crystal
#include <lcd.c>
 
 
 
     #define LCD_ENABLE_PIN  PIN_D0                                    ////set lcd port
     #define LCD_RS_PIN      PIN_D1                                    ////
     #define LCD_RW_PIN      PIN_D2                                    ////
     #define LCD_DATA4       PIN_D4                                    ////
     #define LCD_DATA5       PIN_D5                                    ////
     #define LCD_DATA6       PIN_D6                                    ////
     #define LCD_DATA7       PIN_D7 
float ALL;
 
void main()
{
  set_tris_b(0x00);    //set port b as output
  set_tris_C(0x00);    //set port c as output
  output_b(0x00);      //reset port b
  output_c(0x00);      //reset port c
  
  setup_adc(ADC_CLOCK_INTERNAL); // set internal clck for ADC
  setup_adc_ports(ALL_ANALOG);          //set s analog input
  
  lcd_init();
  while (true)
  {
    
    delay_ms(200);
    set_adc_channel(0);
    ALL=read_adc();
    lcd_gotoxy(1,1);
    printf(lcd_putc,"\fVDIG=%0.1f",(float)ALL);
    
   
   
   
  
   
  }
 
}


best regard

It appears that you are using CCS C compiler.

Example code

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#DEVICE ADC=10
...
long value;
...
setup_adc(ADC_CLOCK_INTERNAL); //enables the a/d module 
                                                //and sets the clock to internal adc clock
setup_adc_ports(ALL_ANALOG);      //sets all the adc pins to analog
set_adc_channel(0);                       //the next read_adc call will read channel 0
delay_us(10);                               //a small delay is required after setting the channel //and before read
value=read_adc();                         //starts the conversion and reads the result
                                                  //and store it in value
read_adc(ADC_START_ONLY);            //only starts the conversion
value=read_adc(ADC_READ_ONLY);     //reads the result of the last conversion and store it in value.Assuming the device have 
                                                    //a 10bit ADC module, value willrange between 0-3FF. 
                                                    //If #DEVICE ADC=8 had been usedinstead the result will yield 0-FF. 
                                                     //  If #DEVICE ADC=16 hadbeen used instead the result will yield 0-FFC0

 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top