//--------------------------------------------------------------------- // RS232.c // 9/24/07 // Converts from USB to RS232. One USB input to three RS232 outputs. // 11/9/07 // Add capability to receive replies from devices. If no reply received // after sending USB command, it will return the string "No Reply" // // lt222@cornell.edu //--------------------------------------------------------------------- #include <18F4455.h> #include #include #fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN, MCLR #use delay(clock=48000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream=,bits=8) ///////////////////////////////////////////////////////////////////////////// //In usb.c/h - tells the CCS PIC USB firmware to include HID handling code. #DEFINE USB_HID_DEVICE TRUE //the following defines needed for the CCS USB PIC driver to enable the TX endpoint 1 // and allocate buffer space on the peripheral #define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT //turn on EP1 for IN bulk/interrupt transfers #define USB_EP1_TX_SIZE 25 //allocate 25 bytes in the hardware for transmission //the following defines needed for the CCS USB PIC driver to enable the RX endpoint 1 // and allocate buffer space on the peripheral #define USB_EP1_RX_ENABLE USB_ENABLE_INTERRUPT //turn on EP1 for OUT bulk/interrupt transfers #define USB_EP1_RX_SIZE 25 //allocate 25 bytes in the hardware for reception #include //Microchip 18Fxx5x hardware layer for usb.c #include //USB Configuration and Device descriptors for this USB device #include //handles usb setup tokens and get descriptor reports // Messages - USB interface #define PRINTSTR 0x04 //Global Variables char command[26]; char *reply; char *p; /////////// Initialization code ///////////////////////////// // // Sets ports as inputs or outputs and to the correct state // Note in set tris - 1 means input, 0 means output void SystemInit(void) { setup_adc(ADC_CLOCK_INTERNAL); setup_psp(PSP_DISABLED); setup_spi(FALSE); setup_wdt(WDT_OFF); setup_timer_0(RTCC_INTERNAL); setup_timer_1(T1_DISABLED); setup_timer_2(T2_DISABLED,0,1); setup_comparator(NC_NC_NC_NC); setup_vref(FALSE); setup_oscillator(False); SET_TRIS_A(0b10001010); // Port A = analog data in from PMT ID signal SET_TRIS_B(0b00000000); // Port B = all outputs SET_TRIS_C(0b11000000); // Port C = SET_TRIS_D(0b00000000); // Port D = all outputs SET_TRIS_E(0b00001000); // Port E = all outputs except for reset } void out1(void) { #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream=,bits=8) printf("\n\r %s ", (p+1)); } void out2(void) { #use rs232(baud=9600, xmit=PIN_A0, rcv=PIN_A1, stream=,bits=8) printf("\n\r %s ", (p+1)); } void out3(void) { #use rs232(baud=9600, xmit=PIN_A2, rcv=PIN_A3, stream=,bits=8) printf("\n\r %s ", (p+1)); } void main(void) { usb_init(); SystemInit(); p=command; while (TRUE) { if (usb_enumerated()) { if (usb_kbhit(1)) { usb_gets(1, command, 25, 100); switch (command[0]){ case 0x31: out1(); break; case 0x32: out2(); break; case 0x33: out3(); break; } if (kbhit()){ gets(reply); } else { reply = "No Reply"; } usb_puts(1, reply, 25, 100); } } } }