Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

pic16f877 a/d converter

Status
Not open for further replies.

shehry

New Member
hi,

my pic16f877 a/d is acting strangely. i was using Vdd and Vss as voltage references. and was using the 8-bit mode. the output goes on somewhat like this:

0 0 0 0 0 0 0 ....i keep on increasing the pot till i begin to get 23 24 21 23 24. but thats all i get even if i further increase it. either 0's or 23's. no numbers in between

the pot is otherwise ok, but whenever i use it with my pic877, i get this stupid output. i was using the example code that comes with the pcwh compiler

could anyone help?
 
its a pic c compiler but i was thinking that it might be more of a problem with the hardware rather than software. its just that the voltage levels drain out when i connect the pot to the pic. i even tried a voltage buffer before it, but it didnt work.
 
If you think it's a hardware problem, you can try replacing the PIC. It's hard to tell from here if you don't show any schematics.

IMO, it think its more likely a software problem even if you don't show any code. Have you correctly set the data justification for the A/D conversion result?

either 0's or 23's. no numbers in between

What do you mean? You're getting 21's in addition to 24's.
 
well this is the code if you need it
Code:
////  This program will show how to use the built in PIC PWM.        ////
////  The program takes an analog input and uses the digital         ////
////  value to set the duty cycle.  The frequency is set by          ////
////  the user over the RS-232.                                      ////


#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=12000000)
#use rs232(baud=9600, xmit=PIN_B0)  // Jumpers: 8 to 11, 7 to 12

void main() {
   char selection;
   byte value;


   selection = '1';

   setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM


   switch(selection) {
     case '1' : setup_timer_2(T2_DIV_BY_1, 127, 1);
                break;
     case '2' : setup_timer_2(T2_DIV_BY_4, 127, 1);
                break;
     case '3' : setup_timer_2(T2_DIV_BY_16, 127, 1);
                break;
   }


  setup_port_a(ALL_ANALOG);
  setup_adc(adc_clock_internal);
  set_adc_channel( 0 );

  delay_ms(500);
  while( TRUE ) {
    value=read_adc();

    printf("%d ",value);

    set_pwm1_duty(value);         
  }

}

its actually an example code for pwm actually. However, in the example it used a parameter BRGH10K in the #use rs232 command as

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, BRGH1OK)

i had no idea what BRGH10K did so i removed it and i couldnt find help on it anywhere.

The corresponding hardware has nothing special to it either. just an analog input at A0, and that's all.

But let's just say that the pot is set to 2.5 V when not connected to the pic. the 2.5V drops to 0V when you connect the pot to the pic, and it stays at 0V almost always until you increase the voltage to maximum. then the adc begins to generate 23's as a result coupled with an odd 21 or 24.[/code]
 
shehry said:
i had no idea what BRGH10K did so i removed it and i couldnt find help on it anywhere.

How can you hope to use a micro-controller when you don't bother reading the datasheet for it?. This is a classic example of why it's ESSENTIAL to have a reasonable understanding of assembler in order to use a high level language!.

The corresponding hardware has nothing special to it either. just an analog input at A0, and that's all.

But let's just say that the pot is set to 2.5 V when not connected to the pic. the 2.5V drops to 0V when you connect the pot to the pic, and it stays at 0V almost always until you increase the voltage to maximum. then the adc begins to generate 23's as a result coupled with an odd 21 or 24

Again, your lack of assembler is probably responsible?, you don't apear to be setting the pins as inputs anywhere? - it's anyones guess what the compiler produces with "setup_port_a(ALL_ANALOG); ".
 
i had no idea what BRGH10K did so i removed it and i couldnt find help on it anywhere.

BRGH is a bit setting that switches the baud rate generator to high speed sampling. It's found in the PIC16F87X datasheet. You ought to read that until you are fully familiar with it.

The trouble with programming in C with these controllers is that you have to know exactly what these functions do before using them. The ff. functions are cases in point:

Code:
  setup_port_a(ALL_ANALOG); 
  setup_adc(adc_clock_internal);

You and I don't know what they do exactly. These compilers supply source code for the functions calls. They are sometimes written in assembly language. As many here have mentioned, some familiarity with PIC assembly language is required.

But let's just say that the pot is set to 2.5 V when not connected to the pic. the 2.5V drops to 0V when you connect the pot to the pic, and it stays at 0V almost always until you increase the voltage to maximum.

It sounds like the port pin is setup as output instead of input. Again it goes back to what the above functions do exactly. Do they set the TRISA register correctly?
 
i thought you didnt have to use tris in case you were using standard i/o. but you did have to use it in case you used fast_io.

anyways, the problem is over now. all did was to change the analog channel...and it worked :S

thanks to all of you for helping.
 
Status
Not open for further replies.

Latest threads

Back
Top