|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Using port B as analog input |
Posted: Mon Dec 08, 2008 3:44 pm |
|
|
Hi, I am using a PIC16F767. I'm trying to read analog input from a potentiometer into Pin 25 which is RB4/AN11. I'll provide my code. When I print the value at this pin I get values close to 100, but they just stay there as I turn the potentiometer. When I set the analog channel to AN1 it works beautifully. Does anyone know how to get access to the analog inputs, which share pins with port B?
Header file:
Code: | #include <16F767.h>
#device ICD=TRUE
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES RC //Resistor/Capacitor Osc with CLKOUT
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES MCLR //Master Clear pin enabled
#FUSES DEBUG //Debug mode for use with ICD
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES BORSEN //See Datasheet
#use delay(clock=500000)
#use rs232(debugger)
//#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) |
Actually code
Code: | #include "\\engin\rvelez1\celeborn\class09\rvelez1\E72\Final Project\ServoControl\CommandViaPOT.h"
int16 getPWM(int16 pulseWidth)
{
return (int16)( ((long)pulseWidth*500000) / ((long)16*1000000) );
}
void main() {
int POTreading;
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_16,155,1);
setup_ccp1(CCP_PWM);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_oscillator(OSC_500KHZ|OSC_INTRC);
setup_adc_ports(ALL_ANALOG|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0);
set_adc_channel(10);
//maxWidth=2700;
while(1)
{
POTreading=read_adc();
printf("Here is the POT %d.\n",POTreading);
delay_ms(300);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 08, 2008 3:49 pm |
|
|
Quote: | #include <16F767.h>
#FUSES RC //Resistor/Capacitor Osc with CLKOUT
#use delay(clock=500000) |
Is this real ? Do you really have a resistor and capacitor connected to
the Osc1 pin on the PIC ?
If you don't, I suspect that you really want to use the INTRC_IO fuse,
and set the #use delay() for 8 MHz.
Also, is this a hardware project, or is it Proteus ? |
|
|
robyvelez
Joined: 08 Dec 2008 Posts: 2
|
|
Posted: Tue Jan 20, 2009 10:37 am |
|
|
I've since changed those settings. My header file looks like this:
Code: | #include <16F767.h>
#device ICD=TRUE
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES RC //Resistor/Capacitor Osc with CLKOUT
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES MCLR //Master Clear pin enabled
#FUSES DEBUG //Debug mode for use with ICD
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES BORSEN //See Datasheet
#use delay(clock=8000000)
//#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use rs232(debugger)
|
And my code looks this.
Code: | #include "\\engin\rvelez1\celeborn\class09\rvelez1\E72\Final Project\TestBoard\testAnalog.h"
void main() {
setup_adc_ports(ALL_ANALOG|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DISABLED,0,1);
setup_vref(FALSE);
setup_oscillator(OSC_8MHZ|OSC_INTRC);
set_adc_channel(12);
while(1)
{
printf("Reading %Lu.",read_adc());
}
}
|
I'm still having the same problem. I can't read any analog ports that are also port b. I just get junk and random readings.
Also something weird when I place an input on the pin for AN12 i get nothing, but when I place the input on the pin for AN4 it reads it just fine. Same thing happens for AN11 and AN3, AN10 and AN2, AN9 and AN1, and AN8 and AN0. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 20, 2009 2:35 pm |
|
|
Try a simple test program like this (get rid of most of your code) and see
if it works. If it doesn't work, then post:
1. Your compiler version.
2. A description of the circuit connected to AN12 (pin B0).
Here's the test program:
Code: | #include <16F767.h>
#device adc=10
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//========================
void main()
{
int16 adc_value;
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_16);
set_adc_channel(12);
delay_us(20);
while(1)
{
adc_value = read_adc();
printf("%LU ", adc_value);
delay_ms(500);
}
} |
|
|
|
robyvelez
Joined: 08 Dec 2008 Posts: 2
|
FIXED! (I think/hope) |
Posted: Tue Jan 20, 2009 8:13 pm |
|
|
Thanks a lot for the reply. I tried the sample code you presented, but that still didn't work, and then I got to thinking about the compiler version.
I tried using a computer with a more updated version of the compiler. I'm an engineering student at a college and the computer I've claimed to use for my project had an old version of PIC C. Using a new version of the compiler the program seems to works fine. I'm going to ask ITS to update the version of PIC C on this computer and try it tomorrow, but I think using a more up to date compiler will do it. If not I'm sure you'll hear back from me again. |
|
|
Guest
|
|
Posted: Sat Jan 24, 2009 7:39 am |
|
|
Hi
Remove the line setup_spi(false); this it not right.
Its the same as setup_spi(0) which is:
#define SPI_CLK_DIV_4 0
:-) |
|
|
|
|
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
|