' USB CDC DEMO sample program for PIC18F4550 CDC serial port emulation ' Compilation of this program requires that specific support files be ' available in the source directory. You may also need to modify the ' file USBDESC.ASM so that the proper descriptor files are included. For ' detailed information, see the file PBP\USB18\USB.TXT. 'NOTES: ' When first connected to a Windows PC, you may be asked for the USB driver. ' if so, use the PBPCDC.INF file when asked for the driver. 'After the device is connected to a PC an additional serial com port will be available 'Open any terminal application such as HyperTerm and select the new com port. 'Enter the values 0 to 5 to see the demo results. Values displayed are 8 bit in size '0 = Hello World! message '1 = ADC Chan 1 value '2 = ADC Chan 2 value '3 = ADC Chan 3 value '4 = ADC Chan 3 value '5 = Displays a value that is incremented each time 'Any other key will display the 'Enter 0-5' message ' buffer Var Byte[16] cnt Var Byte sw var byte i var byte dat var byte x var byte y var byte j var byte k var byte ch var byte adval var byte 'ADC values 8 bit LED Var PORTC.0 'If an LED is connected to V+ via a resistor Define OSC 48 ' Define LCD registers and bits Define LCD_DREG PORTB Define LCD_DBIT 4 Define LCD_RSREG PORTE Define LCD_RSBIT 2 Define LCD_EREG PORTE Define LCD_EBIT 0 DEFINE LCD_BITS 4 'LCD bus size 4 or 8 DEFINE LCD_LINES 4 'Number lines on LCD DEFINE LCD_COMMANDUS 4000 'Command delay time in us DEFINE LCD_DATAUS 200 'Data delay time in us 'ADC parameters DEFINE ADC_BITS 8 'Number of bits in ADCIN result DEFINE ADC_CLOCK 3 'ADC clock source (rc = 3) DEFINE ADC_SAMPLEUS 50 'ADC sampling time in microseconds 'CMCON = 7 TRISA = %11111111 ' Set PORTA to all input ADCON1 = %00001010 ' Set PORTA analog and right justify result, PORTE digital low porte.1 'LCD RW to low since it is write mode only pause 500 k=0 '***** MAIN PROGRAM ****** START: LCDOUT $FE,1, "USB Ready" 'If LCD is connected 'Initialize USBInit Low LED ' LED off ' Wait for USB input idleloop: USBService ' Must service USB regularly cnt = 16 ' Specify input buffer size USBIn 3, buffer, cnt, idleloop sw = buffer[0] ' Message received Toggle LED select case sw case "0" for i = 0 to 14:lookup i,["Hello World!",13,10,0],dat:buffer[i]=dat :next i lcdout $FE,$C0,"Hello World!" case "1" 'Read and send ADC values ADCIN 1, adval : pause 50 : ch = "1" lcdout $FE,$C0," ADC Ch 1 " lcdout $FE,$94," " lcdout $FE,$94,dec adval gosub adc2buff case "2" 'Read and send ADC values ADCIN 2, adval : pause 50 : ch = "2" lcdout $FE,$C0," ADC Ch 2 " lcdout $FE,$94," " lcdout $FE,$94,dec adval gosub adc2buff case "3" 'Read and send ADC values ADCIN 3, adval : pause 50 : ch = "3" lcdout $FE,$C0," ADC Ch 3 " lcdout $FE,$94," " lcdout $FE,$94,dec adval gosub adc2buff case "4" 'Read and send ADC values ADCIN 4, adval : pause 50 : ch = "4" lcdout $FE,$C0," ADC Ch 4 " lcdout $FE,$94," " lcdout $FE,$94,dec adval gosub adc2buff case "5" k=k+1 lcdout $FE,$D4," " lcdout $FE,$D4,"Value=",dec k for i = 0 to 6:lookup i,["Value ="],dat:buffer[i]=dat:next i buffer[7]=(k dig 2) + $30 buffer[8]=(k dig 1) + $30 buffer[9]=(k dig 0) + $30 buffer[10]=13 buffer[11]=10 buffer[12]=0 case else for i = 0 to 13:lookup i,["Enter 0-5",13,10,0],dat:buffer[i]=dat :next i end select outloop: USBService ' Must service USB regularly USBOut 3, buffer, 15, outloop Goto idleloop ' Wait for next buffer '************************************************* 'Subroutines Adc2Buff: buffer[0]="A" buffer[1]="d" buffer[2]="c" buffer[3]= ch buffer[4]="=" buffer[5]=(adval dig 2)+$30 buffer[6]=(adval dig 1)+$30 buffer[7]=(adval dig 0)+$30 buffer[8]=13 buffer[9]=10 buffer[10]=0 return