Markdem
Joined: 24 Jun 2005 Posts: 206
|
Problem with HID and Analog |
Posted: Sat Jun 06, 2009 4:26 am |
|
|
HI All, I am trying to make a simple flight yoke. I have the following code
Code: |
#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#DEFINE USB_HID_DEVICE TRUE
//the following defines needed for the CCS USB PIC driver to enable the TX endpoint 1
// and allocate buffer space on the peripheral
#define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT //turn on EP1 for IN bulk/interrupt transfers
#define USB_EP1_TX_SIZE 8 //allocate 8 bytes in the hardware for transmission
//the following defines needed for the CCS USB PIC driver to enable the RX endpoint 1
// and allocate buffer space on the peripheral
#define USB_EP1_RX_ENABLE USB_ENABLE_INTERRUPT //turn on EP1 for OUT bulk/interrupt transfers
#define USB_EP1_RX_SIZE 8 //allocate 8 bytes in the hardware for reception
#define USB_CON_SENSE_PIN PIN_E1
#DEFINE LED PIN_E2
#define ADC_DELAY 20
#include <pic18_usb.h>
#include "usb_desc_yoke.h"
#include <usb.c>
int out_data[50];
int in_data[2];
signed int Rudder; //out_data[0]
signed int X; //out_data[1]
signed int Y; //out_data[2]
int Hat_Buttons; //out_data[3]
int Buttons; //out_data[4]
int i;
void main()
{
setup_adc_ports(AN0_TO_AN2);
setup_adc(ADC_CLOCK_DIV_64);
usb_init_cs();
set_adc_channel(0);
delay_us(ADC_DELAY);
while (TRUE)
{
usb_task();
out_data[0] = read_adc();
if (usb_enumerated())
{
out_data[0]=-111;
out_data[1]=-100;
out_data[2]=100;
out_data[3]=0b01101110;
out_data[4]=0b00111011;
usb_put_packet(1, out_data, 5, USB_DTS_TOGGLE);
}
delay_ms(500);
output_high(LED);
delay_ms(500);
output_low(LED);
}
}
|
Now, when I comment out the read_adc() line, everything works 100%. With the read_adc() line included, the PIC will stop running (the LED stops blinking) after 30-60 seconds. This will happen both when the USB plug is in and out. I just don't get it. I am not even using the adc reading to send to the PC.
I know that the PIC can be very fussy about the voltage on the VUSB pin. Could it be that somehow, when I read the adc value, the voltage on that pin is going below what the PIC needs? I don't have a scope, so I can't check this.
Thanks
Mark |
|