Conseguí arreglar en parte el código, ahora funciona todo menos el USB. El ordenador lo reconoce pero ahora falta que se comunique con mi programa en labview. Alguien controla un poco de labview que me pueda ayudar con el USB?
Os dejo el código que funciona:
#include <18F2550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#define USB_HID_DEVICE FALSE //disabled 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 1 //size to allocate for the rx endpoint 1 buffer
#use standard_io(B)
#define use_portB_lcd TRUE
#include <lcd.c>
#include <pic18_usb.h> //Microchip PIC18Fxx5x Hardware layer for CCS's PIC USB driver
#include "pic18_WinUSB.h" //Descriptors and USB configuration
#include <usb.c> //handles usb setup tokens and get descriptor reports
#define led1 PIN_C0
#define led2 PIN_C1
#define led3 PIN_C2
#define ledG PIN_A4
#define ledY PIN_A5
#define pin_on output_high
#define pin_off output_low
#define ACTUADORv PIN_C7
#define ACTUADORsa PIN_C6
char varG=0,varS=0,varM=0,i=0,t=0;
int16 crono=0;
//int8 iBuff[1]; //Buffer a utilizar/1byte
//int8 oBuff[1]; //Buffer a utilizar/1byte
#INT_TIMER0
void TIMER0_isr(void){
set_timer0(65235); //reset TMR0
if ((varG==1)&(input(pin_A0)==1))
{
if(t>9){
t=0;
crono++;
if(crono>10000){ //CRONO HASTA 10 SEGUNDOS
crono=0; //SI -> crono=0
}
}
else{ //(*) NO -> incrementa crono
t++;
}
}
}
void main(void)
{
set_tris_a(0x0F);
set_tris_c(0x78);
delay_cycles(5);
pin_off(led1); //Apagar y encender LEDs
pin_off(led2);
pin_off(led3);
pin_on(ledG);
pin_off(ledY);
pin_off(ACTUADORv);
pin_off(ACTUADORsa);
disable_interrupts(GLOBAL);
usb_init();
usb_task();
usb_wait_for_enumeration();
enable_interrupts(INT_TIMER0); //interrupcion TIMER0 activada
setup_counters(RTCC_INTERNAL,RTCC_DIV_4); //configuracion interrupcion TMR0
set_timer0(65235); //carga TMR0
enable_interrupts(GLOBAL); //activadas interrupciones
lcd_init(); // Inicia LCD
delay_ms(100); // Retardo
printf(lcd_putc, "AEDA chrono sys.\n"); // Saca texto
printf(lcd_putc, " Wait please... "); // Saca texto
delay_ms(1000); // Retardo
lcd_putc("\f"); // Limpia pantalla
lcd_putc("Reaction Time:\n"); // Saca texto
lcd_putc(" Ready to go! "); // Saca texto
while (TRUE)
{
if(usb_enumerated()) //Conexión USB establecida
{
pin_on(ledY);
if((varS==0)&(varM==0)&(input(pin_A2)==1)) // envia rutina 1 al actuador
{
delay_ms(100);
varM=1;
lcd_gotoxy(0,2); // Acomoda cursor LCD
lcd_putc(" ON YOUR MARKS");
pin_on(led1);
i=10;
while(!i==0)
{
i--;
pin_on(ACTUADORv);
pin_on(ACTUADORsa);
delay_ms(500);
pin_off(ACTUADORv);
pin_off(ACTUADORsa);
delay_ms(500);
}
// aux=0;
}
if((varM==1)&(varS==0)&(input(pin_A2)==1)) //envia rutina 2 al actuador
{
delay_ms(100);
varS=1;
lcd_gotoxy(0,2); // Acomoda cursor LCD
lcd_putc(" SET");
pin_on(led2);
pin_on(ACTUADORv);
pin_on(ACTUADORsa);
delay_ms(1000);
pin_off(ACTUADORsa);
}
if((varS==1)&(input(pin_A3)==1)) //envia rutina 3 y activa crono
{
delay_ms(100);
varG=1;
lcd_gotoxy(0,2); // Acomoda cursor LCD
lcd_putc(" GO");
pin_on(led3);
pin_off(ACTUADORv);
pin_on(ACTUADORsa);
delay_ms(300);
pin_off(ACTUADORsa);
while(input(pin_A0)==1){}
// oBuff[0] = crono;
// usb_put_packet(1, oBuff, 1, USB_DTS_TOGGLE); // Send 1 byte packet to EP 1
}
if((varG==1)&(input(pin_A0)==0)) // muestra crono LCD
{
lcd_gotoxy(11,2); // Acomoda cursor LCD
printf(lcd_putc,"%5LU",crono); // Imprime el conteo por la lcd
delay_ms(20);
}
if(input(pin_A1)==1) //reinicia sistema
{
varM=0;
varS=0;
varG=0;
i=0;
crono=0;
// reset=0;
// aux=0;
// start=0;
lcd_putc("Reaction Time:\n");
lcd_putc(" Ready to go! ");
pin_off(led1);
pin_off(led2);
pin_off(led3);
pin_off(ACTUADORv);
pin_on(ACTUADORsa);
delay_ms(400);
pin_off(ACTUADORsa);
delay_ms(150);
pin_on(ACTUADORsa);
delay_ms(200);
pin_off(ACTUADORsa);
}
if (usb_kbhit(1)) //Verificar recepción de datos
{
pin_off(ledY);
//usb_get_packet(1, iBuff, 1);
//if (iBuff[0] == 1) reset=1;
//if (iBuff[0] == 2) start=1;
//if (iBuff[0] == 3) aux=1;
}
}
}
}