00001 /********************************************************************* 00002 * 00003 * Microchip USB C18 Firmware Version 1.0 00004 * 00005 ********************************************************************* 00006 * FileName: user.c 00007 * Dependencies: See INCLUDES section below 00008 * Processor: PIC18 00009 * Compiler: C18 2.30.01+ 00010 * Company: Microchip Technology, Inc. 00011 * 00012 * Software License Agreement 00013 * 00014 * The software supplied herewith by Microchip Technology Incorporated 00015 * (the “Company”) for its PICmicro® Microcontroller is intended and 00016 * supplied to you, the Company’s customer, for use solely and 00017 * exclusively on Microchip PICmicro Microcontroller products. The 00018 * software is owned by the Company and/or its supplier, and is 00019 * protected under applicable copyright laws. All rights are reserved. 00020 * Any use in violation of the foregoing restrictions may subject the 00021 * user to criminal sanctions under applicable laws, as well as to 00022 * civil liability for the breach of the terms and conditions of this 00023 * license. 00024 * 00025 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES, 00026 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 00027 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00028 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, 00029 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 00030 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. 00031 * 00032 * Author Date Comment 00033 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00034 * Rawin Rojvanit 11/19/04 Original. 00035 ********************************************************************/ 00036 00037 /****************************************************************************** 00038 * CDC RS-232 Emulation Tutorial Instructions: 00039 ****************************************************************************** 00040 * Refer to Application Note AN956 for explanation of the CDC class. 00041 * 00042 * First take a look at Exercise_Example() and study how functions are called. 00043 * 00044 * There are five exercises, each one has a solution in the CDC\user\solutions. 00045 * Scroll down and look for Exercise_01,_02,_03,_04, and _05. 00046 * Instructions on what to do is inside each function. 00047 * 00048 *****************************************************************************/ 00049 00050 /** I N C L U D E S **********************************************************/ 00051 #include <p18cxxx.h> 00052 #include <usart.h> 00053 #include "system\typedefs.h" 00054 00055 #include "system\usb\usb.h" 00056 00057 #include "io_cfg.h" // I/O pin mapping 00058 #include "user\user.h" 00059 #include "user\temperature.h" 00060 00061 /** V A R I A B L E S ********************************************************/ 00062 #pragma udata 00063 byte old_sw2,old_sw3; 00064 00065 char input_buffer[64]; 00066 //char output_buffer[64]; 00067 00068 //rom char welcome[]={"PIC18F2550 Full-Speed USB - CDC RS-232 Emulation Demo\r\n\r\n"}; 00069 //rom char ansi_clrscr[]={"\x1b[2J"}; // ANSI Clear Screen Command 00070 00071 /** P R I V A T E P R O T O T Y P E S ***************************************/ 00072 void InitializeUSART(void); 00073 void BlinkUSBStatus(void); 00074 BOOL Switch2IsPressed(void); 00075 BOOL Switch3IsPressed(void); 00076 00077 00078 /** D E C L A R A T I O N S **************************************************/ 00079 #pragma code 00080 void UserInit(void) 00081 { 00082 mInitAllLEDs(); 00083 // mInitAllSwitches(); 00084 // old_sw2 = sw2; 00085 // old_sw3 = sw3; 00086 00087 // InitTempSensor(); 00088 00089 // InitializeUSART(); 00090 00091 }//end UserInit 00092 00093 //void InitializeUSART(void) 00094 //{ 00095 // TRISCbits.TRISC7=1; // RX 00096 // TRISCbits.TRISC6=0; // TX 00097 // SPBRG = 0x71; 00098 // SPBRGH = 0x02; // 0x0271 for 48MHz -> 19200 baud 00099 // TXSTA = 0x24; // TX enable BRGH=1 00100 // RCSTA = 0x90; // continuous RX 00101 // BAUDCON = 0x08; // BRG16 = 1 00102 //}//end InitializeUSART 00103 00104 /****************************************************************************** 00105 * Function: void ProcessIO(void) 00106 * 00107 * PreCondition: None 00108 * 00109 * Input: None 00110 * 00111 * Output: None 00112 * 00113 * Side Effects: None 00114 * 00115 * Overview: This function is a place holder for other user routines. 00116 * It is a mixture of both USB and non-USB tasks. 00117 * 00118 * Note: None 00119 *****************************************************************************/ 00120 void ProcessIO(void) 00121 { 00122 BlinkUSBStatus(); 00123 // User Application USB tasks 00124 if((usb_device_state < CONFIGURED_STATE)||(UCONbits.SUSPND==1)) return; 00125 00126 00127 if(getsUSBUSART(input_buffer,1)) 00128 { 00129 if(mUSBUSARTIsTxTrfReady()) 00130 { 00131 input_buffer[0]++; 00132 mUSBUSARTTxRam((byte *)input_buffer,1); 00133 } 00134 } 00135 00136 }//end ProcessIO 00137 00138 /****************************************************************************** 00139 * Function: void BlinkUSBStatus(void) 00140 * 00141 * PreCondition: None 00142 * 00143 * Input: None 00144 * 00145 * Output: None 00146 * 00147 * Side Effects: None 00148 * 00149 * Overview: BlinkUSBStatus turns on and off LEDs corresponding to 00150 * the USB device state. 00151 * 00152 * Note: mLED macros can be found in io_cfg.h 00153 * usb_device_state is declared in usbmmap.c and is modified 00154 * in usbdrv.c, usbctrltrf.c, and usb9.c 00155 *****************************************************************************/ 00156 void BlinkUSBStatus(void) 00157 { 00158 static word led_count=0; 00159 00160 if(led_count == 0)led_count = 10000U; 00161 led_count--; 00162 00163 #define mLED_Both_Off() {mLED_1_Off();mLED_2_Off();} 00164 #define mLED_Both_On() {mLED_1_On();mLED_2_On();} 00165 #define mLED_Only_1_On() {mLED_1_On();mLED_2_Off();} 00166 #define mLED_Only_2_On() {mLED_1_Off();mLED_2_On();} 00167 00168 if(UCONbits.SUSPND == 1) 00169 { 00170 if(led_count==0) 00171 { 00172 mLED_1_Toggle(); 00173 mLED_2 = mLED_1; // Both blink at the same time 00174 }//end if 00175 } 00176 else 00177 { 00178 if(usb_device_state == DETACHED_STATE) 00179 { 00180 mLED_Both_Off(); 00181 } 00182 else if(usb_device_state == ATTACHED_STATE) 00183 { 00184 mLED_Both_On(); 00185 } 00186 else if(usb_device_state == POWERED_STATE) 00187 { 00188 mLED_Only_1_On(); 00189 } 00190 else if(usb_device_state == DEFAULT_STATE) 00191 { 00192 mLED_Only_2_On(); 00193 } 00194 else if(usb_device_state == ADDRESS_STATE) 00195 { 00196 if(led_count == 0) 00197 { 00198 mLED_1_Toggle(); 00199 mLED_2_Off(); 00200 }//end if 00201 } 00202 else if(usb_device_state == CONFIGURED_STATE) 00203 { 00204 if(led_count==0) 00205 { 00206 mLED_1_Toggle(); 00207 mLED_2 = !mLED_1; // Alternate blink 00208 }//end if 00209 }//end if(...) 00210 }//end if(UCONbits.SUSPND...) 00211 00212 }//end BlinkUSBStatus 00213 00214 /** EOF user.c ***************************************************************/