///////////////////////////////////////////////////////////////////////// //// //// //// ex_usb_serial2.c //// //// //// //// A demonstration of the USB CDC API that is provided by CCS. //// //// The USB CDC API that CCS provides will create a virtual UART //// //// port. USB CDC drivers are included with most versions of //// //// Microsoft Windows, and when properly loaded will create a COMx //// //// port from which you can write and read to your PIC device //// //// like any serial device that has a COMx port. //// //// //// //// This example is a conversion of the original EX_INTEE.C to use //// //// the USB CDC API to read and display serial data over USB. //// //// //// //// Only two lines were added to initialize USB: //// //// usb_init() - init USB and enable USB interrupt. //// //// while(!usb_cdc_connected()) - wait until user opens //// //// Hyperterminal before displaying serial data. This line //// //// is not necessary. //// //// //// //// Two other changes were also made to convert to USB. First, //// //// printf will call usb_cdc_putc() to put the character out USB //// //// instead of the normal RS232 stream. Second, gethex() was //// //// replaced with gethex_usb(). All input functions normally found //// //// in input.c have been converted to use the USB CDC API in //// //// usb_cdc.h, and gethex_usb() is one of these converted //// //// functions. //// //// //// //// See usb_cdc.h for API documentation. //// //// //// ///////////////////////////////////////////////////////////////////////// //// //// //// VERSION HISTORY //// //// //// //// July 1st, 2005: Initial Release. //// //// //// ///////////////////////////////////////////////////////////////////////// //// (C) Copyright 1996,2005 Custom Computer Services //// //// This source code may only be used by licensed users of the CCS //// //// C compiler. This source code may only be distributed to other //// //// licensed users of the CCS C compiler. No other use, //// //// reproduction or distribution is permitted without written //// //// permission. Derivative programs created using this software //// //// in object code form are not restricted in any way. //// ///////////////////////////////////////////////////////////////////////// //set to 1 to use a PIC's internal USB Peripheral //set to 0 to use a National USBN960x peripheral #define __USB_PIC_PERIF__ 1 #if !defined(__PCH__) #error USB CDC Library requires PIC18 #endif #if __USB_PIC_PERIF__ #include <18F2550.h> //configure a 20MHz crystal to operate at 48MHz #fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN #device ADC=10 //#fuses USBDIV, PLL1, CPUDIV1, PROTECT, NOCPD, noBROWNOUT,HSPLL,NOWDT,nolvp, VREGEN #use delay(clock=48000000) #else //use the National USBN960x peripheral #include <18F452.h> #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #endif //endif check to see which peripheral to use ///////////////////////////////////////////////////////////////////////////// // // If you are using a USB connection sense pin, define it here. If you are // not using connection sense, comment out this line. Without connection // sense you will not know if the device gets disconnected. // (connection sense should look like this: // 100k // VBUS-----+----/\/\/\/\/\----- (I/O PIN ON PIC) // | // +----/\/\/\/\/\-----GND // 100k // (where VBUS is pin1 of the USB connector) // ///////////////////////////////////////////////////////////////////////////// ///only the 18F4550 development kit has this pin #if __USB_PIC_PERIF__ && defined(__PCH__) #define USB_CON_SENSE_PIN PIN_B2 #endif // Includes all USB code and interrupts, as well as the CDC API #include #rom int 0xf00000={1,2,3,4} #define TS0 0xA1 // 1ms@Timer0 #define dummy 0xBFBF #define EOF1 0xAA #define EOF2 0x0D static unsigned char cd[4] = {0,0,0,0}; static long us_acc[3] = {0x1111,0x2222,0x3333}; static long us_ome[3] = {0x0001,0x0002,0x0003}; static long us_tmp[3] = {0x2354,0x5321,0x9753}; static long us_thK[9] = {0x0101,0x0202,0x0303,0x0404,0x0505,0x0606,0x0707,0x0808,0x0909}; static long Rcnt = 0; void show_mem(); void show_tlm(unsigned char CID,long d1,long d2,long d3); void main() { char c; unsigned char flg = 0x00; setup_ccp1(CCP_PWM); setup_timer_2(T2_DIV_BY_16,0xFF,1); output_float(PIN_B0); ext_int_edge(H_TO_L); usb_cdc_init(); usb_init(); enable_interrupts(INT_EXT); enable_interrupts(GLOBAL); while(!usb_cdc_connected()) {} do { usb_task(); if (usb_enumerated()) { if (usb_cdc_kbhit()){ c = usb_cdc_getc(); if (flg == 2) {cd[1] = c; c = 0; flg = 0;} if (flg == 1) {cd[0] = c; c = 0; flg = 2;} switch(c) { case 0x00: flg = 1; break; case 0x10: show_tlm(c,us_acc[0],us_acc[1],us_acc[2]); c = 0; break; case 0x20: show_tlm(c,us_ome[0],us_ome[1],us_ome[2]); c = 0; break; case 0x40: show_tlm(c,us_tmp[0],us_tmp[1],us_tmp[2]); c = 0; break; case 0x80: show_tlm(c,us_thK[0],dummy,dummy); c = 0; break; case 0x81: show_tlm(c,us_thK[1],dummy,dummy); c = 0; break; case 0x82: show_tlm(c,us_thK[2],dummy,dummy); c = 0; break; case 0x83: show_tlm(c,us_thK[3],dummy,dummy); c = 0; break; case 0x84: show_tlm(c,us_thK[4],dummy,dummy); c = 0; break; case 0x85: show_tlm(c,us_thK[5],dummy,dummy); c = 0; break; case 0x86: show_tlm(c,us_thK[6],dummy,dummy); c = 0; break; case 0x87: show_tlm(c,us_thK[7],dummy,dummy); c = 0; break; case 0x88: show_tlm(c,us_thK[8],dummy,dummy); c = 0; break; case 0x01: show_tlm(c,dummy,dummy,dummy); show_mem(); c = 0; break; case 0x02: show_tlm(c,dummy,dummy,dummy); c = 0; break; case 0x04: show_tlm(c,dummy,dummy,dummy); c = 0; break; case 0x08: show_tlm(c,dummy,dummy,dummy); c = 0; break; }; } } } while (TRUE); } void show_tlm(unsigned char CID,long d1,long d2,long d3) { usb_cdc_putc(CID); usb_cdc_putc(d1 >> 8); usb_cdc_putc(d1 & 0xFF); usb_cdc_putc(d2 >> 8); usb_cdc_putc(d2 & 0xFF); usb_cdc_putc(d3 >> 8); usb_cdc_putc(d3 & 0xFF); usb_cdc_putc(EOF1); usb_cdc_putc(EOF2); } void show_mem() { long i; for(i=1;i<=256;i++) { usb_cdc_putc(i); } usb_cdc_putc(EOF2); } #INT_EXT void ext_isr() { Rcnt++; }