///////////////////////////////////////////////////////////////////////// //// //// //// 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. //// ///////////////////////////////////////////////////////////////////////// #include "main.h" #include #include #define TS0 0xA1 // 1ms@Timer0 #define LOOP 1 #define maxch 5 #define maxcnt 100 // 100ms #define pwmmin 10 // PWM MIN #define pwmmax 100 // PWM MAX // Pin setup // 0:input // 1:output #define LED_PWR PIN_C2 #define LED_1 PIN_E0 #define LED_2 PIN_E1 #define LED_3 PIN_E2 #define LED_4 PIN_C0 #define LED_5 PIN_C1 #define ADC_1 0 #define ADC_2 1 #define ADC_3 2 #define ADC_4 3 #define ADC_5 4 #define MT1_A PIN_B7 #define MT1_B PIN_B6 #define MT2_A PIN_B5 #define MT2_B PIN_B4 #define MT3_A PIN_B0 #define MT3_B PIN_D7 #define MT4_A PIN_C7 #define MT4_B PIN_C6 #define MT5_A PIN_D3 #define MT5_B PIN_D2 #rom int 0xf00000={15,03,15,0} // parameter static unsigned int ui_flg_1ms; static unsigned int ui_flg_100ms; static unsigned int ui_cnt; static unsigned int ui_adc[maxch]; static float f_pwm[maxch]; void set_pwm(void) { int i; // set pwm for(i=0;i pwmmax) f_pwm[i] = pwmmax; } } #INT_TIMER0 void isr0(void) { set_timer0(TS0); // flg ui_cnt++; if(ui_cnt >= maxcnt) { ui_cnt = 0; // get A/D set_adc_channel(ADC_1); delay_us(100); ui_adc[ADC_1] = (read_adc(ADC_START_AND_READ)); set_adc_channel(ADC_2); delay_us(100); ui_adc[ADC_2] = (read_adc(ADC_START_AND_READ)); set_adc_channel(ADC_3); delay_us(100); ui_adc[ADC_3] = (read_adc(ADC_START_AND_READ)); set_adc_channel(ADC_4); delay_us(100); ui_adc[ADC_4] = (read_adc(ADC_START_AND_READ)); set_adc_channel(ADC_5); delay_us(100); ui_adc[ADC_5] = (read_adc(ADC_START_AND_READ)); // set PWM set_pwm(); ui_flg_100ms = 1; } ui_flg_1ms = 1; } void init(void) { int i; // init parameter setup_adc_ports(AN0_TO_AN5 | VSS_VDD); setup_adc(ADC_CLOCK_DIV_32); //setup_psp(PSP_DISABLED); //setup_spi(FALSE); //setup_wdt(WDT_OFF); // Pin setup set_tris_a(0xFF); set_tris_b(0x00); set_tris_c(0x00); set_tris_d(0x00); set_tris_e(0x00); port_b_pullups(FALSE); // Timer0 starts. setup_timer_0(RTCC_INTERNAL | RTCC_DIV_128 | RTCC_8_BIT); set_timer0(TS0); //setup_comparator(NC_NC_NC_NC); //setup_vref(FALSE); //setup_oscillator(False); usb_cdc_init(); usb_init(); // output //output_b(0x00); //output_c(0x00); //output_d(0x00); //output_e(0x00); output_high(LED_PWR); output_high(LED_1); output_high(LED_2); output_high(LED_3); output_high(LED_4); output_high(LED_5); // motor drive output_low(MT1_B); output_low(MT2_B); output_low(MT3_B); output_low(MT4_B); output_low(MT5_B); // count ui_cnt = 0; ui_flg_1ms = 0; ui_flg_100ms = 0; for(i=0;i f_pwm[i]) { // for PWM OFF switch(i) { case 0:output_low(LED_1);output_low(MT1_A); break; case 1:output_low(LED_2);output_low(MT2_A); break; case 2:output_low(LED_3);output_low(MT3_A); break; case 3:output_low(LED_4);output_low(MT4_A); break; case 4:output_low(LED_5);output_low(MT5_A); break; } } else { // for PWM ON switch(i) { case 0:output_high(LED_1);output_high(MT1_A); break; case 1:output_high(LED_2);output_high(MT2_A); break; case 2:output_high(LED_3);output_high(MT3_A); break; case 3:output_high(LED_4);output_high(MT4_A); break; case 4:output_high(LED_5);output_high(MT5_A); break; } } } } if (usb_enumerated()) { if(ui_flg_100ms) { ui_flg_100ms = 0; printf(usb_cdc_putc,"%2.0f,%2.0f,%2.0f,%2.0f,%2.0f\r\n",f_pwm[0],f_pwm[1],f_pwm[2],f_pwm[3],f_pwm[4]); } } } while (LOOP); }