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

ADC for 16F767

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



Joined: 11 Sep 2006
Posts: 27

View user's profile Send private message

ADC for 16F767
PostPosted: Mon Sep 11, 2006 7:16 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Sep 11, 2006 10:23 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Sep 11, 2006 10:27 pm     Reply with quote

ok, I'll post it once i get back home, thanx for u reply.....
Ttelmah
Guest







PostPosted: Tue Sep 12, 2006 2:19 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 12, 2006 6:43 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 12, 2006 12:40 pm     Reply with quote

Several of the A/D functions don't work for the 16F767 in your version
of the compiler. In fact, items 3 to 7 in the following post, apply to
your case:
http://www.ccsinfo.com/forum/viewtopic.php?t=28032
wakaka



Joined: 11 Sep 2006
Posts: 27

View user's profile Send private message

PostPosted: Tue Sep 12, 2006 6:49 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 12, 2006 8:31 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 12, 2006 8:44 pm     Reply with quote

For a simulator, I use MPLAB SIM, which comes with MPLAB.
It simulates serial port output, if you use the hardware serial port pins.
You can download MPLAB here:
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en019469&part=SW007002
You need a high speed connection for this, such as ADSL.

------------

With regard to your Vref problem, I suspect that you're using a trimpot
with a very high resistance. What is the resistance value stamped on
the trimpot ?
wakaka



Joined: 11 Sep 2006
Posts: 27

View user's profile Send private message

PostPosted: Wed Sep 13, 2006 12:28 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Sep 13, 2006 1:09 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Sep 13, 2006 5:04 am     Reply with quote

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
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