unsigned char usbcdc_rd_ready() { if (ep2_o.STAT & UOWN) return 0; if (rx_idx >= ep2_o.CNT) { usbcdc_read(); return 0; } return 1; }
char usbcdc_getchar() { char c; while (!usbcdc_rd_ready()); c = cdc_rx_buffer[rx_idx++]; if (rx_idx>=ep2_o.CNT) { usbcdc_read(); } return c; }
void main(void) { OSCCON = 0x70; initIO(); stepperInit(MOTOR_X); stepperInit(MOTOR_Y); stepperInit(MOTOR_Z); stepperInit(MOTOR_4); TRISCbits.TRISC6 = 0; // Timer 0 interrupt rate = ((48 000 000 MHz / 4) / 4) / 256 = 11 718.7 Hz // rxtimeout = 255/11718.75 = 21.7 msec // Note Timer 2 would allow faster rates if the interrupt processing can be optimized T0CONbits.T0CS = 0; // Internal instruction clock as source for timer 0 T0CONbits.PSA = 0; // Assign prescaler to Timer 0. PSA=1 would be equivalent prescaler 1:1 T0CONbits.T08BIT = 1; // 8 bit T0CONbits.T0PS2 = 0; // Set up prescaler to 1:4 = 001 T0CONbits.T0PS1 = 0; T0CONbits.T0PS0 = 1; // se above T0CONbits.TMR0ON = 1; T0CONbits.T0SE = 1; // high to low (no effect when timer clock coming from instruction clock...) usbcdc_init(); INTCON2bits.TMR0IP = 1; // Make Timer0 high priority INTCON = 0; // Clear interrupt flag bits. INTCONbits.TMR0IE = 1; // Enable Timer 0 interrupts TMR0L = 0; TMR0H = 0; stepperSetMode(MOTOR_X, 0xF, 0, 0, 0, 0); stepperSetMode(MOTOR_Y, 0xF, 0, 0, 0, 0); stepperSetMode(MOTOR_Z, 0xF, 0, 0, 0, 0); stepperSetMode(MOTOR_4, 0xF, 0, 0, 0, 0); INTCONbits.PEIE = 1; INTCONbits.GIE = 1; LED_PIN = 0; while (1) { if (usbcdc_device_state != CONFIGURED) { if (!rx_timeout) { rx_timeout = 255; if (!blink--) { blink = 23; LED_PIN = !LED_PIN; } } } else { if (getMessage()) { LED_PIN=0; processMessage(); //LED_PIN = 1; blink = 12; } else { if (!blink--) { blink = 12; LED_PIN = !LED_PIN; while (UIRbits.TRNIF == 1) UIRbits.TRNIF = 0; usbcdc_read(); } } } } }