|
|
View previous topic :: View next topic |
Author |
Message |
Alexandra
Joined: 27 May 2009 Posts: 1
|
Error reading AD0 on a Pic18f4550 |
Posted: Wed May 27, 2009 8:08 am |
|
|
Hi, I hope someone can help me with this program:
Using the PicUSB code of J1M, I am using the AD0 channel of PIC18F4550 to read a voltage range of 0-5V and then send this value through the USB port on the PC, for low voltage values less than 2.5 V got correct readings from 0 to 127, but for values above the reading remains at this value (127) even when I change the input voltage up to 3.7V with a reading of 205, then on reading is again correct, someone could tell me what I'm doing wrong ...
The potentiometer is not the problem that I have checked it with a multimeter and it is OK
I have checked this using the hyperterminal and a labview and the reading is the some
Code: | #include <18F4550.h>
#DEVICE ADC=8
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48MHZ)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#define USB_HID_DEVICE FALSE //deshabilitamos el uso de las directivas HID
#define USB_EP1_TX_ENABLE USB_ENABLE_BULK //turn on EP1(EndPoint1) for IN bulk/interrupt transfers
#define USB_EP1_RX_ENABLE USB_ENABLE_BULK //turn on EP1(EndPoint1) for OUT bulk/interrupt transfers
#define USB_EP1_TX_SIZE 1 //size to allocate for the tx endpoint 1 buffer
#define USB_EP1_RX_SIZE 3 //size to allocate for the rx endpoint 1 buffer
#include <pic18_usb.h> //Microchip PIC18Fxx5x Hardware layer for CCS's PIC USB driver
#include <PicUSB.h> //Configuración del USB y los descriptores para este dispositivo
#include <usb.c> //handles usb setup tokens and get descriptor reports
#define modo recibe[0]
#define param1 recibe[1]
#define param2 recibe[2]
#define resultado envia[0]
void main(void)
{
unsigned char recibe[3]; //declaramos variables
unsigned char adc[1];
unsigned int contador;
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_2);
set_adc_channel(0);
delay_us(10);
usb_init(); //inicializamos el USB
usb_task(); //habilita periferico usb e interrupciones
usb_wait_for_enumeration(); //esperamos hasta que el PoteUSB sea configurado por el host
while (TRUE)
{
if(usb_enumerated()) //si el PoteUSB está configurado
{
while(TRUE)
{
adc[0] = read_adc();
delay_us(10);
usb_put_packet(1, adc, 1, USB_DTS_TOGGLE);
printf("El valor de AD0 es: %2u\n\r",adc[0]);
delay_ms(500);
}
}
}
} |
|
|
|
Ttelmah Guest
|
|
Posted: Wed May 27, 2009 9:02 am |
|
|
There is a problem with operating the ADC at 48MHz, it is above the maximum supported by this component. Technically, you have to use the sleep mode, select the RC clock, and momentarily stop the processor while you take the reading, to get full accuracy.
The 'best' solution, is to switch to running the CPU at 24Mhz (CPUDIV2).
Even at the slower clock rate, the ADC, needs to be using ADC_CLOCK_DIV_32
At the moment, you are trying to run a component expecting a clock not above 1.4MHz (or 700KHz, if this is an LF device), with a clock at 24MHz....
Best Wishes |
|
|
|
|
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
|