int main(void){ unsigned long timer; #define longDelay(x) timer=x; while(timer--) CLKDIV = 0x0000; // Set PLL prescaler (1:1) init(); //setup the crystal, pins, hold the FPGA in reset usbbufflush(); //setup the USB byte buffer USBDeviceInit();//setup usb while(1){ USBDeviceTasks(); if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) continue; usbbufservice();//load any USB data into byte buffer //send data from the FPGA receive buffer to USB //if((mUSBUSARTIsTxTrfReady()) && (uartincnt > 0)){ // putUSBUSART(&buf[0], uartincnt); //} CDCTxService(); }//end while }//end main
void main(void) { unsigned char c,x,y,led; unsigned long l; // Default all pins to digital ADCON1 |= 0x0F; // Disable USB external transceiver to allow RB1/RB2 UCFGbits.UTRDIS = 0; // set pin to output TRISBbits.TRISB0 = 0; TRISBbits.TRISB1 = 0; TRISBbits.TRISB2 = 0; TRISBbits.TRISB3 = 0; LED0 = 1; LED1 = LED2 = LED3 = 0; // TODO: Remove magic with macro usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); usb_start(); initCDC(); usbbufflush(); do { if (!TestUsbInterruptEnabled()) USBDeviceTasks(); switch(usb_device_state){ case DETACHED_STATE: LED1 = 0; LED2 = 0; LED3 = 0; case ATTACHED_STATE: LED1 = 0; LED2 = 0; LED3 = 1; case POWERED_STATE: LED1 = 0; LED2 = 1; LED3 = 0; case DEFAULT_STATE: LED1 = 0; LED2 = 1; LED3 = 1; case ADR_PENDING_STATE: LED1 = 1; LED2 = 0; LED3 = 0; case ADDRESS_STATE: LED1 = 1; LED2 = 0; LED3 = 1; case CONFIGURED_STATE: LED1 = 1; LED2 = 1; LED3 = 0; } } while (usb_device_state < CONFIGURED_STATE); while(1); #if 0 // Initiate Osc with 8MHz (only relevant if you use the internal oscillator) //OSCCON = 0x70; initCDC(); // TODO: Remove magic with macro usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); usb_start(); #if defined (USB_INTERRUPTS) //EnableUsbInterrupt(USB_TRN + USB_SOF + USB_UERR + USB_URST); EnableUsbInterrupt(USB_STALL + USB_IDLE + USB_TRN + USB_ACTIV + USB_SOF + USB_UERR + USB_URST); EnableUsbInterrupts(); #endif usbbufflush(); //flush USB input buffer system led = UCFGbits.UTRDIS; LED0=0; LED1=0; LED2=0; LED3=0; while(1){ LED1 = LED1 ? 0 : 1; for(l = 0; l < 10; ++l); } LED0=1; LED1=1; LED2=1; LED3=1; delay_ms(500); #if 0 while(1) { LED0 = 1; delay_ms(250); LED0 = 0; delay_ms(250); } #endif LED0 = 0; LED1 = 0; LED2 = 0; LED3 = 0; delay_ms(500); while(1); do { LED2=1; LED3 = 0; #ifndef USB_INTERRUPTS //service USB tasks //Guaranteed one pass in polling mode //even when usb_device_state == CONFIGURED_STATE if (!TestUsbInterruptEnabled()) { USBDeviceTasks(); LED3 = 1; } #endif if ((usb_device_state < DEFAULT_STATE)) { // JTR2 no suspendControl available yet || (USBSuspendControl==1) ){ LED0=1; LED1=0; } else if (usb_device_state < CONFIGURED_STATE) { LED0=1; LED1=1; }else if((usb_device_state == CONFIGURED_STATE)) { LED0=0; LED1=1; } LED2=0; delay_ms(1000); } while (usb_device_state < CONFIGURED_STATE); LED0 = 0; LED1 = 0; LED2 = 1; #if 1 while(1) { LED0 = led; led = led ? 0 : 1; #ifndef USB_INTERRUPTS //service USB tasks //Guaranteed one pass in polling mode //even when usb_device_state == CONFIGURED_STATE if (!TestUsbInterruptEnabled()) USBDeviceTasks(); #endif usbbufservice(); //service USB buffer system if (usbbufgetbyte(&c) == 1) { WaitInReady(); cdc_In_buffer[0] = c; //answer OK putUnsignedCharArrayUsbUsart(cdc_In_buffer, 1); } } #endif // Disable USB external transceiver to allow RB1/RB2 UCFGbits.UTRDIS = 0; // set pin to output TRISBbits.TRISB0 = 0; TRISBbits.TRISB1 = 0; TRISBbits.TRISB2 = 0; // start a loop blinking the led while(1) { LED0 = 1; delay_ms(250); LED0 = 0; delay_ms(250); } #endif }
void main(void){ unsigned char i,cmd, param[9]; //unsigned char t[]={"Hello World"}; init(); //setup the crystal, pins usbbufflush(); //setup the USB byte buffer delayMS(10); HD44780_Reset();//setup the LCD HD44780_Init(); USBDeviceInit();//setup usb while(1){ USBDeviceTasks(); if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) continue; usbbufservice();//load any USB data into byte buffer cmd=waitforbyte();//wait for a byte from USB if(cmd!=MATRIX_ORBITAL_COMMAND){//assume text, if 254 then enter command mode LCD_WriteChar(cmd); //not a command, just write it to the display }else{//previous byte was 254, now get actual command switch(waitforbyte()){//switch on the command case BACKLIGHT_ON: //1 parameter (minutes 00=forever) param[0]=waitforbyte(); LCD_Backlight(1);//turn it on, we ignore the parameter break; case BACKLIGHT_OFF: LCD_Backlight(0);//backlight off break; case CLEAR: LCD_Clear(); break; case HOME: LCD_Home(); break; case POSITION: //2 parameters (col, row) param[0]=waitforbyte(); param[1]=waitforbyte(); cmd=( ((param[1]-1)*20) + param[0] ); //convert to 20x4 layout (used defined lines, add rows...) LCD_CursorPosition(cmd); break; case UNDERLINE_CURSER_ON: LCD_UnderlineCursor(1); break; case UNDERLINE_CURSER_OFF: LCD_UnderlineCursor(0); break; case BLOCK_CURSER_ON: LCD_BlinkCursor(1); break; case BLOCK_CURSER_OFF: LCD_BlinkCursor(0); break; case BACKLIGHT_BRIGHTNESS://1 parameter (brightness) param[0]=waitforbyte(); break; case CUSTOM_CHARACTER: //9 parameters (character #, 8 byte bitmap) LCD_WriteCGRAM(waitforbyte());//write character address for(i=1; i<9; i++){ LCD_WriteRAM(waitforbyte()); //send 8 bitmap bytes } break; default: //error break; } } CDCTxService(); } }//end main
void main(void){ u8 i,cmd, param[9],c; u16 temp; // TODO to be removed later on long l; signed int x, y, z; signed char x8, y8, z8; static unsigned char ledtrig; initCDC(); // JTR this function has been highly modified It no longer sets up CDC endpoints. init(); //setup the crystal, pins mma_init(); usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); // TODO: Remove magic with macro usb_start(); usbbufflush(); //flush USB input buffer system i = mma_read( MMA_I2CAD ); //if((i&~0b10000000)!=0x1d)while(1); mma_write( MMA_I2CAD, 0x1d | MMA_I2CDIS_bit ); // disable I2C i = mma_read( MMA_CTL1 ); mma_write( MMA_CTL1, i |= MMA_DFBW_bit ); // high output data rate mma_write( MMA_MCTL, 0b00000101 ); // 2g range; measurement mode //mma_calibrate_offset( 0, 0, 0 ); //LEDs //PWM on cathode TRISAbits.TRISA0=0; LATAbits.LATA0=1; //enable cathode //setup LEDs LATB=0; TRISB=0; ledtrig=1; //only shut LED off once // Never ending loop services each task in small increments while (1) { do { if (!TestUsbInterruptEnabled()) //JTR3 added USBDeviceTasks(); ////service USB tasks Guaranteed one pass in polling mode even when usb_device_state == CONFIGURED_STATE if ((usb_device_state < DEFAULT_STATE)) { // JTR2 no suspendControl available yet || (USBSuspendControl==1) ){ //LedOff(); } else if (usb_device_state < CONFIGURED_STATE) { //LedOn(); }else if((ledtrig==1) && (usb_device_state == CONFIGURED_STATE)){ //LedOff(); ledtrig=0; } } while (usb_device_state < CONFIGURED_STATE); usbbufservice();//load any USB data into byte buffer if(usbbufgetbyte(&inByte) == 1){ switch(inByte){//switch on the command case 0x00: WaitInReady(); //it's always ready, but this could be done better param[0]='B'; param[1]='B'; param[2]='I'; param[3]='O'; param[4]='1'; putUnsignedCharArrayUsbUsart(param,5); break; case 0x01: mma_get_average8( 4, &x8, &y8, &z8 ); param[0]=x8; param[1]=y8; param[2]=z8; WaitInReady(); //it's always ready, but this could be done better putUnsignedCharArrayUsbUsart(param,3); break; default: //error break; } }//if data }//while //#endif }//end main