View previous topic :: View next topic |
Author |
Message |
Ben Dinsdale
Joined: 10 Aug 2007 Posts: 6 Location: East Yorkshire, UK
|
PIC16F913 ADC issues (CCS version: 3.219) |
Posted: Mon Oct 29, 2007 8:50 am |
|
|
Hello, I'm having bother getting the ADC to work the circuit is very simple i'm using AN0 (pin 2) which is connected across a 1 Ohm resistor as shunt. I'm getting 0.5V at the PIN, but ca't get a reading from the ADC. which I'm monitoring using MPLAB ICD2. via the watch window.
I'm wondering if I'm missing somthing?
in the .h file I have added #device ADC=10
the ADC is set up as follows:
setup_port_a(sAN0 | VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
then I try to get the value using:
value = read_ADC();
where value is declared as a long
any help or suggestions will be greatly received
Thanks, Regards, Ben |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 29, 2007 12:10 pm |
|
|
I don't have a 16F913, but I do have a 16F917, which is a very similar
chip. I was able to make it work.
I used a PicDem2-Plus board with the 16F917 plugged into it. I installed
your version of the compiler. I'm using an ICD2 with MPLAB vs. 7.41.
I used my voltmeter to adjust the trimpot for pin AN0 so it puts 0.5 volts
on that pin.
I used the test program shown below. In the Debugger menu, I went
to the "Select Tool" menu and picked "MPLAB ICD2". Then I compiled
the program and selected "Connect" in the Debugger menu. Then I
opened a Watch Window and set it up to watch the 'value' symbol.
Also, I did a right-click on the 'value' line and selected 'Properties' and
set it up to display the value in decimal (The default is Hex).
Initially the Watch Window says "out of scope". I put a breakpoint on
the delay_cycles(1) line in the source code. This is depicted with a little
red dot with a "B" in the middle of it, in MPLAB. Then I went to the
debugger window and selected "Run". It ran. It stopped on the
breakpoint and displayed this:
Code: |
Address Value
0026 103
|
A value of 103 is about 10% of the ADC range (when running in 10-bit
mode), and 10% of 5v is 0.5v, which is correct. It's what you expect
to see.
Code: |
#include <16F917.H>
#device adc=10
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//======================================
void main()
{
int16 value;
setup_port_a(sAN0 | VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
while(1)
{
value = read_ADC();
delay_cycles(1);
}
while(1);
} |
|
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Mon Oct 29, 2007 2:47 pm |
|
|
Can you please explain the delay_cycles(1) in your program. I use delay_ms(20) before asigning the adc value to a variable. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 29, 2007 3:20 pm |
|
|
Actually, it should be more like 20 us.
I really should have written the program like this:
Code: |
setup_port_a(sAN0 | VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
delay_us(20); // Initial acquisition delay
while(1)
{
value = read_ADC();
delay_cycles(1);
}
|
The purpose of the delay_cycles(1) is just to provide a NOP instruction
for the debugger to do a breakpoint on. |
|
|
Ben Dinsdale
Joined: 10 Aug 2007 Posts: 6 Location: East Yorkshire, UK
|
|
Posted: Tue Oct 30, 2007 4:30 am |
|
|
Thanks for the input guys,
I have solved the problem, I was having after alot of head scratching and going through the disassemby listing, and stacks. For some reason the read_ADC() wasn't polling the on bit ANCON0. So the value wasn't returned so I did the polling of ADCON0 manualy. its not ideal but it worked.
Thanks again guys
Regards Ben |
|
|
|