View previous topic :: View next topic |
Author |
Message |
Got-Got
Joined: 26 Feb 2012 Posts: 17
|
ADC Problem |
Posted: Sun Feb 26, 2012 3:16 pm |
|
|
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
|
What's wrong? |
Posted: Sun Feb 26, 2012 3:28 pm |
|
|
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
|
|
Posted: Sun Feb 26, 2012 4:35 pm |
|
|
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
|
Analogue problem |
Posted: Sun Feb 26, 2012 6:19 pm |
|
|
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
|
|
Posted: Sun Feb 26, 2012 7:13 pm |
|
|
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
|
|
Posted: Mon Feb 27, 2012 4:05 am |
|
|
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
|
|
Posted: Mon Feb 27, 2012 11:40 am |
|
|
So, are you up and running or not?
Mike |
|
|
Got-Got
Joined: 26 Feb 2012 Posts: 17
|
|
Posted: Mon Feb 27, 2012 1:33 pm |
|
|
No it still doesn't work at all. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Got-Got
Joined: 26 Feb 2012 Posts: 17
|
|
Posted: Mon Feb 27, 2012 2:39 pm |
|
|
my version of CCS compiler is : Version 4.068 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 27, 2012 3:02 pm |
|
|
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
|
|
Posted: Mon Feb 27, 2012 3:33 pm |
|
|
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
|
|
Posted: Mon Feb 27, 2012 3:40 pm |
|
|
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
|
|
Posted: Mon Feb 27, 2012 3:55 pm |
|
|
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
|
|
Posted: Mon Feb 27, 2012 4:05 pm |
|
|
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. |
|
|
|