|
|
View previous topic :: View next topic |
Author |
Message |
rubber
Joined: 28 May 2009 Posts: 1
|
yet another adc problem... |
Posted: Thu May 28, 2009 2:03 pm |
|
|
Hi!
I'm trying to read a voltage divider made by a 22k potentiometer.
pin1 of the pot goes to vcc, pin3 to vss and center pin on the pic16f877a.
I wrote this code, but seems like there's something wrong because it's not working.
Code: |
#include <16F877A.h>
#device ADC=8
#include <math.h>
#include <stdlib.h>
#include <string.h>
#fuses XT, NOWDT, PUT, NOPROTECT, NOBROWNOUT, NOLVP, WRT_50%, NOCPD
#use delay(clock=11059200)
#use rs232 (baud= 9600,xmit=PIN_B6, rcv=PIN_B7, INVERT)
int16 posicao;
void init(void)
{
setup_adc_ports( AN0 );
setup_adc(ADC_CLOCK_INTERNAL);
}
int16 converte_adc()
{
int16 adc;
delay_us(10);
set_adc_channel( 0 );
adc = read_adc();
return adc;
}
void main(void)
{
printf("ola!!!!");
init();
printf("ola!!!!");
while(1){
//posicao=converte_adc();
posicao=(float)converte_adc();
posicao=((posicao/255.0)*5.0);
delay_us(100);
printf("\n posicao %ld\n\n\r",posicao);
}
}
|
Any ideas?
Thanks in advance.
RJ |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu May 28, 2009 2:25 pm |
|
|
Quote: | #fuses XT, NOWDT, PUT, NOPROTECT, NOBROWNOUT, NOLVP, WRT_50%, NOCPD
#use delay(clock=11059200) |
Look in the oscillator section of the PIC data sheet. XT mode is only
recommended for crystals of 4 MHz, maximum. It doesn't have the
power to drive a faster crystal. Use HS mode.
Also, there's no reason to use write protection during development of
an A/D program. |
|
|
|
|
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
|