View previous topic :: View next topic |
Author |
Message |
wakaka
Joined: 11 Sep 2006 Posts: 27
|
ADC for 16F767 |
Posted: Mon Sep 11, 2006 7:16 pm |
|
|
Hi guys, need help. This is my code and it is not workign at all....anything wrong??
Code: |
#include <16f767.h>
#device adc=8
#fuses HS,NOWDT,NOBROWNOUT,NOPROTECT,NOPUT,NODEBUG
#use delay(clock=20000000)
#byte porta=5
#byte portb=6
void main()
{
int value = 0;
set_tris_a(1);
set_tris_b(0);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(ANALOG_AN0_to_AN4 | VSS_VDD);
/* Wanna use pin_A0 as input, vref = vdd */
set_adc_channel(0);
delay_us(10);
while(TRUE) {
value = read_adc();
portb = value;
}
} |
For adc=8, in this case, 0V = 0, 5V = 255 rite?
wat if i wan 0V = 0, 3V = 255?
1V = 0, 4V = 255?
thankx.......
Last edited by wakaka on Tue Sep 12, 2006 6:42 am; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Sep 11, 2006 10:23 pm |
|
|
Post your version of the compiler. It's a 4-digit number, such as
3.191, 3.203, or 3.249, etc. You can find it at the top of the .LST file,
which will be in your project directory. |
|
|
wakaka
Joined: 11 Sep 2006 Posts: 27
|
|
Posted: Mon Sep 11, 2006 10:27 pm |
|
|
ok, I'll post it once i get back home, thanx for u reply..... |
|
|
Ttelmah Guest
|
|
Posted: Tue Sep 12, 2006 2:19 am |
|
|
On the part of the question 'how do I get a different analog full scale range', there are two basic possible answers. The first is to have a reference voltage at the required full scale range, and use the external Vref. The second is to use the higher accuracy available from the ADC (adc=10), and then scale the result. So:
Code: |
int16 adcval;
adcval=read_adc();
adcval=(adcval*5)/12;
if (adcval>255) adcval=255;
|
This gives adcval=0 to 255, for 0 to 3v, using a 5v reference.
Obviously the last line is only needed if you want to ensure that adcval, cannot go over 255 (making it easy to transfer into an 8bit value).
Best Wishes |
|
|
wakaka
Joined: 11 Sep 2006 Posts: 27
|
|
Posted: Tue Sep 12, 2006 6:43 am |
|
|
Hi, I've checked the version of my compiler. THis is wat is stated in the LST file.
CCS PCM C Compiler, Version 3.181, 16465
did i do smth wrong on the code?? Regarding the code, plz refer to the 1st post, thanx.....plz help......... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
wakaka
Joined: 11 Sep 2006 Posts: 27
|
|
Posted: Tue Sep 12, 2006 6:49 pm |
|
|
really??aw......no wonder i cant get anything for tat....so, can i ask wat version r u using now? Or wat version of the CCS should hv the least bug or no bug??
btw, do u hv any recommendation on any PIC IDE simulator software? something like http://www.oshonsoft.com/pic.html ?
Better its free.....thanx...... |
|
|
wakaka
Joined: 11 Sep 2006 Posts: 27
|
|
Posted: Tue Sep 12, 2006 8:31 pm |
|
|
Code: | #include <16F876A.h>
#device adc=8
#use delay (clock=20000000)
#fuses HS,NOPUT,NOWDT,NOCPD,NOBROWNOUT,NOPROTECT,NODEBUG,NOLVP
#byte porta=5
#byte portb=6
void main()
{
int value=0;
set_tris_a(1);
set_tris_b(0);
SETUP_ADC_PORTS(RA0_ANALOG_RA3_RA2_REF);
/*
Only Pin A0 is analog pin. Both Vref(+) and Vref(-) are used
*/
SETUP_ADC(adc_clock_internal);
set_adc_channel( 0 );
delay_us(10);
portb = 255;
delay_ms(1000);
portb = 0;
while(1){
value = read_adc();
portb = value;
}
} |
I use 16F876A with the above code and it works!!
But one thing that is weird. I tune a variable resistor to 3.0V, then connect it to PIC Vref(+), then power up the PIC. WHen i measure the Vref(+) again, it shows 0V. I need to further tuned it until it reaches 3.0V. And the range for the Pot becoms smaller as it lifts to 5.0V fast, and it tends to be more sensitive than usual??i wonder y........ |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
wakaka
Joined: 11 Sep 2006 Posts: 27
|
|
Posted: Wed Sep 13, 2006 12:28 am |
|
|
i think so....as I'm at the office, i cant tell u now. But can i know wats the suitable resistance for a Pot? or should i say wat is the max resistance of the pot being used? Is it 10k?......thanx |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 13, 2006 1:09 am |
|
|
According the 16F877 data sheet the Vref current can be as high as 1 ma.
That's quite a lot. The following Microchip appnote (AN546) suggests
several Vref driver circuits.
http://ww1.microchip.com/downloads/en/AppNotes/00546e.pdf
If you want to use a trimpot, it might be best to use an opamp,
configured as a voltage follower, to buffer the signal. See Figure 12
on page 9. They show a Zener as the Vref source, but you could
use a trimpot instead. The LM358 (or LM358A) is a single-supply
opamp, but it's not rail-to-rail. You won't get much more than +3.5v
out. According to the 16F877 data sheet, the minimum Vref voltage
is +2.5v for a +5v Vdd. So I think a Rail-to-Rail opamp is needed. |
|
|
wakaka
Joined: 11 Sep 2006 Posts: 27
|
|
Posted: Wed Sep 13, 2006 5:04 am |
|
|
I'd tried 5k and 10k pot, but same outcome....think i really need to try the zener diode method for the Vref( as i dunno wats the rail-to-rail)..... thanx |
|
|
|