void USB(void){
   for(;;){
      if(usb_enumerated()){   //True si el USB ha sido enumerado.      
         if(usb_kbhit(1)){    //(endpoint=1 EP1)= TRUE si el EP1 tiene datos 
                              //en su buffer de recepcion.
            
            //-- (endpoint,ptr,max)=Reads up to max bytes from
            //-- the specified endpoint buffer and saves it to the pointer ptr
            //-- Returns the number of bytes saved to ptr
            usb_get_packet(1,RecibirByte,20);
                                             
            //-- revisa en orden logico el contenido de 
            //-- RecibirByte[0],[1],[2],[3],[4]....
            //-- *2, OSA usa el doble de pulsos
            numPulsosX     = (RecibirByte[0]*256+RecibirByte[1])*2;  
            numPulsosY     = (RecibirByte[2]*256+RecibirByte[3])*2;
            numPulsosZ     = (RecibirByte[4]*256+RecibirByte[5])*2;
            numPulsosE     = (RecibirByte[6]*256+RecibirByte[7])*2;
            periodoPulsosX = (RecibirByte[8]*256+RecibirByte[9]);
            periodoPulsosY = (RecibirByte[10]*256+RecibirByte[11]);
            periodoPulsosZ = RecibirByte[12];
            periodoPulsosE = RecibirByte[13];
            dirMotorX      = RecibirByte[14];
            dirMotorY      = RecibirByte[15];
            dirMotorZ      = RecibirByte[16];
            dirMotorE      = RecibirByte[17];
            GlaoGlc        = RecibirByte[18];
            valorR         = RecibirByte[19];  
         }         
         //-- reviso en orden logico EnviarByte[0],[1] y envio por usb               
         ADC();    //esta funcion contiene los valores de EnviarByte[0;1]
         if( (numPulsosX || numPulsosY || numPulsosZ || numPulsosE) != 0) {EnviarByte[2]=1; output_high(PIN_B6);}
         else {EnviarByte[2]=0; output_low(PIN_B6);}
         
         //-- (endpoint,data,len,tgl)=Places the packet of data
         //-- into the specified endpoint buffer.
         //-- Returns TRUE if success, FALSE if the buffer 
         //-- is still full with the last packet.
         usb_put_packet(1,EnviarByte,3,USB_DTS_TOGGLE);                                                                     
      }      
      OS_Delay(10); 
      OS_Yield();
   }
}
void main() {
   
   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);
   set_tris_b(0x00);                
   output_b(0x00);
   LED_ON(LEDR);
   LED_OFF(LEDV); 
   usb_init();                      
   usb_task();   //Monitorea el estado de la coneccion conectandose y desconectandose automaticamente
   usb_wait_for_enumeration();  //espera infinitamente hasta que el dispositivo fue enumerado    
   LED_ON(LEDV);
   LED_OFF(LEDR); 
   
   while (TRUE) 
   {
      usb_task();
      if (usb_enumerated()) 
      {
        
            Salida[0]=read_adc();
            usb_put_packet(1, Salida, 1, USB_DTS_TOGGLE);

         if (usb_kbhit(1)) 
         {
         
           usb_get_packet(1, Entrada, 1);
           if (Entrada[0]==1)
            {      
             LED_ON(PIN_B0);
              
            }

            else 
            LED_OFF(PIN_B0);

         }
         
      }
   }
   }
Beispiel #3
0
void main() {
	// mod, added to patch config bits
	patch_config();

	output_high(LEDR);
	output_low(LEDG);
   
	usb_init();
   
	setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4);
	set_timer0(0x8ad0);

	enable_interrupts(GLOBAL);
	enable_interrupts(INT_TIMER0);

	while(1) {
	
	#if defined (BOARD_AVRUSB12_32)
		  //Mod here we check pin status to see if we must reset device
	  if(!input(PIN_B7)) {
					delay_ms(25);	//debounce
					if(!input(PIN_B7)) {	//its a press
						output_bit(LEDG,1);					
						delay_ms(500);	//for long press detection, 
						if(!input(PIN_B7)) {	//its still a press after 1 sec
							PINRST_BTL();			//reset device
						}						
						output_bit(LEDG,0);			
					}					
		}
	#endif
		
		usb_task();
		usb_isr();

		if(DelayCount) continue;

		if(Connect) {
			if(UADDR != HubAddress) {
				usb_set_address(HubAddress);
			}

			DevicePort = Connect;
			port_status[Connect - 1] = PORT_FULL;
			port_change[Connect - 1] = C_PORT_CONN;

			TxBuf[0] = 1 << Connect;
			if(Force0DTS)
				usb_put_packet(1, TxBuf, 1, 0);
			else
				usb_put_packet(1, TxBuf, 1, USB_DTS_TOGGLE);
			Connect = 0;
			Force0DTS = 0;
		}

		if(Reset) {
			TxBuf[0] = 1 << Reset;
			usb_put_packet(1, TxBuf, 1, USB_DTS_TOGGLE);
			Reset = 0;
		}

		if(Disconnect) {
			if(UADDR != HubAddress)
				usb_set_address(HubAddress);
			DevicePort = Disconnect;
			port_status[Disconnect - 1] = PORT_EMPTY;
			port_change[Disconnect - 1] = C_PORT_CONN;
			TxBuf[0] = 1 << Disconnect;
			usb_put_packet(1, TxBuf, 1, USB_DTS_TOGGLE);
			Disconnecting = Disconnect;
			Disconnect = 0;
		}

		if(WaitJig) {
			if(WaitJig == 1) {
				if(usb_kbhit(2)) {
					unsigned char c;
					Chirp();
					c = usb_get_packet(2, TxBuf, 8);
					nJigs++;
					EP_BDxST_I(1) = 0x40;   //Clear IN endpoint
					if(nJigs == 8) {
						nJigs = 0;
						WaitJig = 2;
						Delay10ms(50);
					}
				}
			}
			else {
				int n = 0;
				for(n = 0; n < 8; ++n) {
					TxBuf[n] = jig_response[8 * nJigs + n];
				}
				if(usb_put_packet(1, TxBuf, 8, nJigs == 0 ? 0 : USB_DTS_TOGGLE)) {
					Delay10ms(1);
					nJigs++;
					Chirp();
					if(nJigs == 8) {
						nJigs = 0;
						WaitJig = 0;
						Delay10ms(15);
						Disconnect = 3;
					}
				}
			}
		}

		if(Address != -1) {
			delay_ms(1);
			usb_set_address(Address);
			Address = -1;
		}
	}
}
void main(void) {

   int8 recibe[3];                  //declaramos variables
   int8 envia[1];

   setup_adc_ports( ALL_ANALOG );		//Configuramos el puerto a como analogo
   setup_adc( ADC_CLOCK_INTERNAL );
   set_adc_channel( 0 );  

   LED_OFF(LEDV);                   //encendemos led rojo
   LED_ON(LEDR);

   usb_init();                      //inicializamos el USB

   usb_task();                      //habilita periferico usb e interrupciones
   usb_wait_for_enumeration();      //esperamos hasta que el PicUSB sea configurado por el host

  LED_OFF(LEDR);
  LED_ON(LEDV);                    //encendemos led verde
    
  /* while(TRUE){
       envia[0] = (int8)Read_ADC();
       if (envia[0] > 0x7F){
              LED_ON(LEDR);
              LED_ON(LEDV);                    //encendemos led verde
         }else{
              LED_OFF(LEDR);
              LED_OFF(LEDV);  //otra acción
        }
   } 
*/
   while (TRUE)
   {
      if(usb_enumerated())          //si el PicUSB está configurado
      {
         if (usb_kbhit(1))          //si el endpoint de salida contiene datos del host
         {
            usb_get_packet(1, recibe, 3); //cojemos el paquete de tamaño 3bytes del EP1 y almacenamos en recibe

            if (modo == 0) // Modo_Suma
            {
              envia[0] = (int8)Read_ADC();
              usb_put_packet(1, envia, 1, USB_DTS_TOGGLE); //enviamos el paquete de tamaño 1byte del EP1 al PC
				if (envia[0] > 0x7F){
              LED_ON(LEDR);
              LED_ON(LEDV);                    //encendemos led verde
         	}else{
              LED_OFF(LEDR);
              LED_OFF(LEDV);  //otra acción
        }
            }

            if (modo == 1) // Modo_Led
            {
               if (param1 == 0) {LED_OFF(LEDV); LED_OFF(LEDR);} //apagamos los leds
               if (param1 == 1) {LED_ON(LEDV); LED_OFF(LEDR);} //encendemos led verde
               if (param1 == 2) {LED_OFF(LEDV); LED_ON(LEDR);} //encendemos led rojo
            }
         }
      }
   }
}
void main(void) {
     
   int8 recibe[5];                  //declaramos variables
   int8 send2[1];

   LED_OFF(LED_OK);                   //encendemos led rojo
   LED_ON(LED_FAIL);
            
   usb_init();                      //inicializamos el USB  
   
   setup_adc_ports(AN0);         //Configura canais analógico
   setup_adc(ADC_CLOCK_INTERNAL);    //De acordo com relógio interno.
   
   /*SETUP_TIMER_1 (T1_INTERNAL|T1_DIV_BY_2);       //Configurar timer1 para clock iterno/8
   enable_interrupts (INT_TIMER1);                //Habilitar Interrupções
   enable_interrupts (global);*/

   usb_task();                      //habilita periferico usb e interrupciones
   usb_wait_for_enumeration();      //esperamos hasta que el PicUSB sea configurado por el host
   
   enable_interrupts (global);

   LED_OFF(LED_FAIL);                 //desligo o LED vermelho
   LED_ON(LED_OK);                    //acendo o LED verde
   
   move_x('S');
   move_y('S');
         
   while (true)
   {
      if(usb_enumerated())          //si el PicUSB está configurado
      {  
         if (usb_kbhit(1))          //si el endpoint de salida contiene datos del host
         {            
            LED_ON(LED_DATA);
            usb_get_packet(1, recibe, 5); //cojemos el paquete de tamaño 3bytes del EP1 y almacenamos en recibe            
            
            if(modo == 1)
            {               
                  if(dir_x == 1 && value_x == 1){
                     move_x('R');
                  }
                  if(dir_x == 2 && value_x == 1){
                     move_x('L');
                  }                  
                  if(dir_y == 1 && value_y == 1){
                     move_y('F');
                  }
                  if(dir_y == 2 && value_y == 1){
                     move_y('B');
                  }
                  
                  delay_ms(20);               
            }
            
            if (modo == 4)
            {
               status = 1;
               usb_put_packet(1, send2, 1, USB_DTS_TOGGLE); //enviada a informação para o PC com o status
            }    
            LED_OFF(LED_DATA);
         }
      }
   }
}