///////////////////////////////////////////////////////////////////////// //// //// //// 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> #device adc=8 //configure a 20MHz crystal to operate at 48MHz #fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN //#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={10,0,10,13} //#define THR_ON 2 //#define THR_SLP 0 #define TS0 0xA1 // 1ms@Timer0 #define ADCh 1 #define HED 0xAB // Header def:0xAB #define A1 1.614827292 #define A2 -0.635295662 #define B0 0.005117092 #define B1 0.010234185 #define B2 0.005117092 #define ADDR0 0 #define ADDR1 1 #define ADDR2 2 #define SW_DRIVE PIN_B5 #define TR_DRIVE PIN_A2 #define LED_PWR PIN_C6 #define LED_DRV PIN_C7 static float f_dat; static float f_dat_LPF; static float pre1,pre2; static float f_pre = 0; static float f_diff; static long head; static long period = 0; static long dat[3]= {0,0,0}; static unsigned int ui_flg = 0; static unsigned int ui_dat; float LPF(float inp) { float reg,out; reg = inp + A1*pre1 + A2*pre2; out = B0*reg + B1*pre1 + B2*pre2; pre2 = pre1; pre1 = reg; return out; } #INT_TIMER0 void isr0(void) { set_timer0(TS0); // get A/D set_adc_channel(ADCh); ui_dat = read_adc(); f_dat = 0xFF - ui_dat; f_dat_LPF = LPF(f_dat); ui_flg++; } void main() { setup_adc_ports(AN0_TO_AN1 | VSS_VDD); setup_adc(ADC_CLOCK_DIV_32); set_tris_a(0xFB); set_tris_b(0xFF); set_tris_c(0x00); port_b_pullups(TRUE); usb_cdc_init(); usb_init(); //while(!usb_cdc_connected()) {} // Timer0 starts. setup_timer_0(RTCC_INTERNAL | RTCC_DIV_128 | RTCC_8_BIT); set_timer0(TS0); enable_interrupts(INT_TIMER0); enable_interrupts(GLOBAL); // Read parmeter dat[0] = read_eeprom(ADDR0); dat[1] = read_eeprom(ADDR1); dat[2] = read_eeprom(ADDR2); // Done. output_low (TR_DRIVE); output_low (LED_DRV); output_high(LED_PWR); // Hellow printf(usb_cdc_putc,"Hello foucault\n"); do { usb_task(); if (usb_enumerated()) { if(ui_flg) { printf(usb_cdc_putc,"%02X,%02X,%02X,%02X,%f,%f,%f\n",input_b(),dat[0],dat[1],dat[2],f_dat,f_dat_LPF,f_diff); } // Input by keyboard if (usb_cdc_kbhit()) { // Check command header head = usb_cdc_getc(); if (head != HED) continue; // Receive command & data dat[0] = usb_cdc_getc(); dat[1] = usb_cdc_getc(); dat[2] = usb_cdc_getc(); // Write parmeter. write_eeprom(ADDR0,dat[0]); write_eeprom(ADDR1,dat[1]); write_eeprom(ADDR2,dat[2]); } } // Action of push button if(ui_flg) { ui_flg = 0; // Control by auto f_diff = f_dat_LPF - f_pre; // Drive by Switch if(!input(SW_DRIVE)) { output_high(TR_DRIVE); output_high(LED_DRV); }else{ output_low (TR_DRIVE); output_low (LED_DRV); //if(f_diff > THR_MIN && f_diff < THR_MAX) //if(f_dat_LPF > dat[0] && f_diff < dat[1]) if(f_dat_LPF > dat[0] && f_diff > dat[1]) { if(period < dat[2]) { period++; output_high(TR_DRIVE); output_high(LED_DRV); } }else{ period = 0; output_low (TR_DRIVE); output_low (LED_DRV); } } // Refresh f_pre = f_dat_LPF; } } while (TRUE); }