void main(void) { //we use PIN_C2 as an event to determine if we should start the USB CDC //bootloader. if it is not low (button is not pressed) then goto the //application, else if is low (button is pressed) then do the bootloader. output_bit(PIN_C2, 1); if(!input(PIN_C2)) { setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_DIV_16); //Max7219 Initialized initLedDriver(); g_InBootloader = TRUE; usb_cdc_init(); usb_init(); while(!usb_enumerated()); load_program(); } g_InBootloader = FALSE; #ASM goto APPLICATION_START #ENDASM }
void main() { BYTE i, j, address, value; int16 q,q1; float p; q1=0; setup_adc_ports(AN0|VSS_VDD); setup_adc(ADC_CLOCK_INTERNAL); set_adc_channel(0); usb_cdc_init(); usb_init(); // while(!usb_cdc_connected()) {} do { usb_task(); if (usb_enumerated()) { delay_ms(500); q = read_adc(); if (q!=q1){ p = 5.0 * q / 1024.0; printf(usb_cdc_putc,"\r Voltage=%01.2fV", p); } q1=q; } } while (TRUE); }
//------------------------------------------------------------------------------ // Main Function: //------------------------------------------------------------------------------ void main() { //-------------------------------------------------------------------------- // PIC Initialization Section //-------------------------------------------------------------------------- Disable_Interrupts(GLOBAL); // all interrupts OFF Setup_adc(ADC_OFF); // ADC not needed Setup_adc_ports(NO_ANALOGS); // No ADC ports needed Setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256); // Timer 0 set for 42.6us, Setup_timer_1(T1_DISABLED); // Timer 1 Set_Tris_A(TRISA_Enabled); // SPI Disabled Set_Tris_B(TRISB_Disable); // Flash SPI Disabled Set_Tris_C(TRISC_Disable); // Flash SPI Disabled Output_high(FLASH_SELECT); // Slash Select High Output_high(FLASH_CLOCK); // Pulse the clock Output_low(RTC_RST); // Disable RTC Output_High(FPGALoad); // FPGA Upload pin Output_High(FPGAReset); // FPGA reset off Output_High(TestLED); // indication of boot up spi_enabled = False; // ZBC to PIC SPI disabled initially spi_write = False; // ZBC to PIC SPI disabled initially Refresh_RTCSPI(); // Refresh data from RTC into SPI buffer //-------------------------------------------------------------------------- // USB Initialization Section //-------------------------------------------------------------------------- usb_init_cs(); // Initialize the USB Connection if(read_eeprom(BOOT_TYPE) > 0) { // We want boot from FLASH FlashToFPGA(); // If not hooked up to USB then try to init Output_Low(FPGAReset); // FPGA reset off Output_High(FPGAReset); // FPGA reset off Output_Low(TestLED); // Turn off Test LED } else { FGPA_SPI_Init(); // Put PIC in SPI Slave mode } //-------------------------------------------------------------------------- // Main Command Loop: //-------------------------------------------------------------------------- do { // Always do USB Task usb_task(); // so it will detect USB pluging in if(usb_enumerated()) { // Are we plugged into the USB port ? usb_rcvdata_task(); // If so, check for data } if(spi_enabled) { // if ZBC to PIC SPI enabled, then check if(SSP_HAS_DATA()) { Handle_SPI(); // Handle SPI request from ZBC } } // Otherwise... just } while(True); // Continue forever, what else can we do ? }
void main(void) { int i; set_tris_b(0x10000000); output_high(PIN_B0); delay_ms(500); output_low(PIN_B0); fprintf(A, "rs232 init...\n\r"); usb_init_cs(); while(1) { if(input(PIN_B7)) { output_high(PIN_B0); usb_task(); while(input(PIN_B7)) { if (usb_enumerated()) { if (usb_kbhit(1)) { receive_msg(); //usb_get_packet(1, rdata, 1); //fprintf(A, "%u", rdata[0]); //switch(rdata[0]) //{ // case 0x01: output_high(PIN_B0); break; // case 0x02: output_low(PIN_B0); break; //} } } } usb_detach(); output_low(PIN_B0); } } }
void main() { setup_timer_3 (T3_DISABLED|T3_DIV_BY_1); usb_init (); lcd_init (); lcd_putc ('\f'); delay_ms (1); while (TRUE) { lcd_refresh (); usb_task (); if (usb_enumerated ()) { if (usb_kbhit (1)) rxdata_len = usb_get_packet (1, rxdata, sizeof (rxdata)); } delay_ms (100); } }
///////////////////////////////////////////////////////////////////////////// // // usb_debug_task() // // When called periodically, displays debugging information over serial // to display enumeration and connection states. Also lights LED1 based upon // enumeration and status. // ///////////////////////////////////////////////////////////////////////////// void usb_debug_task(void) { static int8 last_connected; static int8 last_enumerated; int8 new_connected; int8 new_enumerated; static int8 last_cdc; int8 new_cdc; new_connected=usb_attached(); new_enumerated=usb_enumerated(); new_cdc=usb_cdc_connected(); if (new_enumerated) LED_ON(LED1); else LED_OFF(LED1); if (new_cdc) LED_ON(LED2); else LED_OFF(LED2); if (usb_cdc_carrier.dte_present) LED_ON(LED3); else LED_OFF(LED3); if (new_connected && !last_connected) printf("USB connected, waiting for enumaration...\r\n\n"); if (!new_connected && last_connected) printf("USB disconnected, waiting for connection...\r\n\n"); if (new_enumerated && !last_enumerated) printf("USB enumerated by PC/HOST\r\n\n"); if (!new_enumerated && last_enumerated) printf("USB unenumerated by PC/HOST, waiting for enumeration...\r\n\n"); if (new_cdc && !last_cdc) { printf("Serial program initiated on USB<->UART COM Port\r\n\n"); printf(usb_cdc_putc, "\r\n\nCCS CDC (Virtual RS232) Example\r\n\n"); } last_connected=new_connected; last_enumerated=new_enumerated; last_cdc=new_cdc; }
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); } } } }
void lcd_refresh () { char* p = buf; int8 i; lcd_gotoxy (1, 1); if (usb_enumerated ()) { lcd_putc ("EN: 1"); sprintf (buf, ", Dat: %d\n", rxdata_len); while (*p) lcd_putc (*(p++)); } else lcd_putc ("EN: 0\n"); /* dump data if any */ for (i = 0; i < rxdata_len; i++) if (rxdata[i]) lcd_putc (rxdata[i]); lcd_putc ('\n'); }
void main () { setup_timer_3 (T3_DISABLED|T3_DIV_BY_1); pwm_init (0); pwm_duty (0, 50); usb_init (); lcd_init (); lcd_putc ('\f'); delay_ms (1); lcd_refresh (); while (TRUE) { usb_task (); if (usb_enumerated ()) { if (usb_kbhit (1)) { rx_msg_len = usb_get_packet (1, &rx_msg, sizeof (rx_msg)); process_usb_data (); } } lcd_refresh (); delay_ms (100); } }
void lcd_refresh () { lcd_gotoxy (1, 1); printf (lcd_putc, "U=%d P=%d\n", usb_enumerated (), pwm_enabled); printf (lcd_putc, "P1=%Ld%% P2=%Ld%%\n", pwm1_duty_percent, pwm2_duty_percent); }
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); } } } }