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 Problem
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Got-Got



Joined: 26 Feb 2012
Posts: 17

View user's profile Send private message

ADC Problem
PostPosted: Sun Feb 26, 2012 3:16 pm     Reply with quote

Hello everyone


i'm new here and new in PIC programming.

I wrote a simple program. Read the resistance value of a potentiometer via the ADC module of a PIC 16F887 and turn on a led if the value returned is less than 100.
I already checked the forum, i correct some mistakes but it still doesn't work.
I use the CCS plugin for MPLAB v8.36, a PIC 16f887, potentiometer on AN0, LED on RD7. Internal RC.

You can see the diagram of the board (a 44pin demo board of Microchip) at the end of this Document

http://ww1.microchip.com/downloads/en/devicedoc/41296a.pdf

Code:


#include <16F887.h>
#device adc=8
#FUSES NOWDT      // no WDT
#FUSES INTRC      // internal oscillator
#FUSES PUT        //power Up Timer
#FUSES NOPROTECT  //no write protec
#FUSES NODEBUG    //no debug ICD
#FUSES NOBROWNOUT //no brownout reset
#FUSES NOLVP      //No low voltage prgming
#FUSES NOCPD      //no eeprom protect
#use delay(internal=8Mhz) //internal oscillator 8 Mhertz


//Potentiometer on AN0
//Led on RD7

void init_io()
{
    // Port A IN
    SET_TRIS_A(0x11);   
   
    // Port B OUT
    SET_TRIS_D(0x00);   // bit 0 = sortie, 1 = entrée

    OUTPUT_B(0x00);
    OUTPUT_C(0x00);
    OUTPUT_D(0x00);
    OUTPUT_E(0x00);
}

void main(){
   int8 temp=0;

   SETUP_ADC_PORTS(sAN0 | VSS_VDD);               
   setup_adc(ADC_CLOCK_INTERNAL); //configuring  ADC
   setup_adc_ports(ALL_ANALOG);   // Ports in ADC

   while(true){ //forever
      set_adc_channel(0);// AN0 reading
      delay_us(20);// 20us delay
      temp=read_adc(); // AN0 reading: 0<=temp<=255
     
       // turn led on if "temp" >100
       
      if(temp > 100){
         output_high(PIN_D7);
         delay_ms(500); // waiting half second
      }
      else{
         output_low(PIN_D7);
      }
   }
}




Bye !
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

What's wrong?
PostPosted: Sun Feb 26, 2012 3:28 pm     Reply with quote

What do you mean "does not work".

There could be loads of things.

It would help if explain what does or does not work, then it's easier to help you.

Mike

P.S. Please quote version No.
Got-Got



Joined: 26 Feb 2012
Posts: 17

View user's profile Send private message

PostPosted: Sun Feb 26, 2012 4:35 pm     Reply with quote

Yes sorry,

I mean that nothing happens, the led don't turn on. I can turn the potentiometer in all the ways, nothing. No problem with the board (i can load and test other programs without any problems)

CCS build the program without any errors or warnings. the program loading in the pic is ok too.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

Analogue problem
PostPosted: Sun Feb 26, 2012 6:19 pm     Reply with quote

I'm not sure that you need the "SET_TRIS_A(0x11)" line (it's a digital I/O setting).

Check the syntax for setup_adc_ports in the 16F877.h file.

You can check in the watch window that registers are being set correctly. Refer to the '877 data sheet for this.

Try sending 'temp' to a PC with printf() via RS232.

Also check AN0 with a voltmeter.

You could move the delay_ms(500) outside the if( temp > 100 ) statement so that it happens whatever temp's value.

Best of luck

Mike
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 26, 2012 7:13 pm     Reply with quote

Can you turn on the LED, just by itself, without all the A/D code, etc. ?
The schematic for your board shows that a jumper JP1 must be installed
to enable the LEDs. Is it installed ?
Got-Got



Joined: 26 Feb 2012
Posts: 17

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 4:05 am     Reply with quote

I checked the syntax, there was a mistake i think but nothing important.

Code:
SETUP_ADC_PORTS(sAN0 | VSS_VDD);
      setup_adc(ADC_CLOCK_INTERNAL); //configuring  ADC       
      setup_adc_ports(ALL_ANALOG);   // Ports in ADC


I first set up AN0 "SETUP_ADC_PORTS(sAN0 | VSS_VDD); "
so no need to do it again here "setup_adc_ports(ALL_ANALOG); // Ports in ADC "

For the use of "SET_TRIS_A(0x11)" I can't be sure but i use it in an other program using ADC and it work well.

For the RS232, can i use it without serial cable ? (i use an USB cable)
Using this instruction :
"#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS)"

and this one just after reading ADC "printf("%u",temp);" ?

And for "PCM programmer", yes i can turn it on. The jumper is installed, the led turn on with others programs.

Thanks for help !
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 11:40 am     Reply with quote

So, are you up and running or not?

Mike
Got-Got



Joined: 26 Feb 2012
Posts: 17

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 1:33 pm     Reply with quote

No it still doesn't work at all.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 1:46 pm     Reply with quote

What is your CCS compiler version ? It's given at the top of the .LST
file, which will be in your project directory. Example of version numbers:
http://www.ccsinfo.com/devices.php?page=versioninfo
Got-Got



Joined: 26 Feb 2012
Posts: 17

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 2:39 pm     Reply with quote

my version of CCS compiler is : Version 4.068
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 3:02 pm     Reply with quote

It worked for me. I copied and pasted your program from your first post
into MPLAB. I installed vs. 4.068. I put a 16F887 DIP-40 package PIC
into my PicDem2-Plus board (older, non-Rohs version board).

That board has LEDs on pins B0 to B3, so I jumpered pin B0 to pin D7
so it would work with your program.

I compiled and loaded the program. I turned the trimpot (on pin A0)
and when the voltage got to 2.0v, the LED turned on. It works.
Got-Got



Joined: 26 Feb 2012
Posts: 17

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 3:33 pm     Reply with quote

It works for me too. i re tested it and guess what... in the programming software, the option "/MCLR" was checked. i unchecked it and it work perfectly...

"Hold Device in Reset :– When checked, the MCLR (VPP) pin is held low
(asserted). When unchecked, the pin is released (tri-stated), allowing an external
pull-up to bring the device out of Reset."

Thanks for your help! Problem Solved !
Got-Got



Joined: 26 Feb 2012
Posts: 17

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 3:40 pm     Reply with quote

Just another question. Some other programs works even when MCLR is checked (even some that use ADC) . why ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 3:55 pm     Reply with quote

What program has this MCLR option ? Are you talking about MPLAB
and its toolbar buttons for "Release from Reset" and "Hold in Reset" ?
I don't see any checkbox there. They're just buttons. Explain what
you mean.
Got-Got



Joined: 26 Feb 2012
Posts: 17

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 4:05 pm     Reply with quote

I create the program with MPLAB and then i load it in the pic with another software named " PICkit 2 programmer"

In "PICkit 2 programmer", there is a MCLR checkbox.

you can see it on this picture

http://gregwoods.co.uk/wp-content/uploads/2012/02/PICKit2Programmer100pc.png

When the box is checked, my program don't work, and when unchecked, it work. But some others program works no matter what MCLR is On or Off.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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