7/22/2019 Este Es Un Ejemplo Para Leer Constantemente Con LabVIEW Un Potenciometro Conectado
1/2
// Este es un ejemplo para leer constantemente con LabVIEW unpotenciometro conectado// en la entrada AN0 del PIC. Esta basado en el ejemplo de J1M Pic_USB// disponible en www.hobbypic.com (muchas gracias J1M)// Proximamente el proyecto estara disponible en www.microing.com.ar
#include#DEVICE ADC=8
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN
#use delay(clock=48000000)#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#define USB_HID_DEVICE FALSE //deshabilitamos el usode 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 forthe tx endpoint 1 buffer#define USB_EP1_RX_SIZE 3 //size to allocate forthe rx endpoint 1 buffer
#include //Microchip PIC18Fxx5x Hardware layer forCCS's PIC USB driver
#include //Configuracin del USB y losdescriptores para este dispositivo#include //handles usb setup tokens and getdescriptor reports
#include
#define LEDV PIN_C0#define LEDR PIN_C1#define LED_ON output_high#define LED_OFF output_low
#define modo recibe[0]#define param1 recibe[1]#define param2 recibe[2]#define resultado envia[0]
void main(void){
int8 recibe[3]; //declaramos variablesint8 adc[1];int1 salir;
setup_adc_ports(AN0|VSS_VDD);setup_adc(ADC_CLOCK_DIV_2);set_adc_channel(0);delay_us(10);
usb_init(); //inicializamos el USBusb_task(); //habilita periferico usb e
interrupcionesusb_wait_for_enumeration(); //esperamos hasta que el PoteUSB
sea configurado por el host
7/22/2019 Este Es Un Ejemplo Para Leer Constantemente Con LabVIEW Un Potenciometro Conectado
2/2
while(TRUE){
if(usb_enumerated()) //si el PoteUSB est configurado
{output_high(PIN_C0); // encendemos LED para indicar que
el disp esta enumerado
salto:
if(usb_kbhit(1)) //si el endpoint de salidacontiene datos del host
{usb_get_packet(1, recibe,3);//leemos el paquete de
tamao 3bytes del EP1 y lo guardamos en recibe
if(modo ==0) // Modo_Pote
{
adc[0]= read_adc();delay_us(10);
if(usb_put_packet(1, adc,1, USB_DTS_TOGGLE))
//enviamos el paquete de tamao 1byte del EP1 al PC{ output_toggle(PIN_C0);}
delay_ms(100);goto salto;
}if(modo ==1)// Modo_Salir
{goto salto;}
}
}}
}