///////////////////////////////////////////////////////////////////////// //// //// //// 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 #define HED 0xAB // Header def:0xAB #define EOF1 0x0D // EOF1 #define EOF2 0x0A // EOF2 #rom int 0xf00000={1,2,3,4} static long dat[3]= {0,0,0}; static unsigned long duty = 0x1234; void get_votage(unsigned char); void show_tlm (unsigned char,unsigned char,unsigned char); void main() { unsigned char head; unsigned char cmd; unsigned char dat1; unsigned char dat2; unsigned char tmp; setup_adc(ADC_CLOCK_DIV_64); setup_adc_ports(AN0_TO_AN2_ANALOG|VSS_VDD); setup_timer_2(T2_DIV_BY_16,0xFF,1); setup_ccp1(CCP_PWM); setup_ccp2(CCP_PWM); usb_cdc_init(); usb_init(); enable_interrupts(INT_RDA); enable_interrupts(GLOBAL); while(!usb_cdc_connected()) {} do { usb_task(); if (usb_enumerated()) { if (usb_cdc_kbhit()){ // Check command header head = usb_cdc_getc(); if (head != HED) continue; // Receive command & data cmd = usb_cdc_getc(); dat1 = usb_cdc_getc(); dat2 = usb_cdc_getc(); // Analysis command switch(cmd) { case 0x00: break; case 0x01: show_tlm(cmd,0x12,0x34); break; case 0x02: show_tlm(cmd,dat1,dat2); break; case 0x03: tmp = duty; show_tlm(cmd,duty >> 8,tmp & 0x00FF); break; case 0xA0: get_votage(0); tmp = dat[0]; show_tlm(cmd,dat[0] >> 8,tmp & 0x00FF); break; case 0xA1: get_votage(1); tmp = dat[1]; show_tlm(cmd,dat[1] >> 8,tmp & 0x00FF); break; case 0xA2: get_votage(2); tmp = dat[2]; show_tlm(cmd,dat[2] >> 8,tmp & 0x00FF); break; case 0xD0: duty = dat1; duty = ((duty << 8) | dat2 & 0x00FF); break; // 0% case 0xD1: set_pwm1_duty(duty); break; // pwm1 case 0xD2: set_pwm2_duty(duty); break; // pwm2 case 0xD3: duty = 0x1234; break; }; if (duty > 0x03FF) duty = 0x03FF; head = 0x00; } } } while (TRUE); } void get_votage(unsigned char chn) { set_adc_channel(chn); dat[chn] = read_adc(ADC_START_AND_READ); delay_ms(100); } void show_tlm(unsigned char cmd,unsigned char d1,unsigned char d2) { usb_cdc_putc(cmd); usb_cdc_putc(d1); usb_cdc_putc(d2); usb_cdc_putc(EOF1); usb_cdc_putc(EOF2); }