'--------Title-------- ' File......signed_num1.pbp ' Started....4/25/08 ' Microcontroller used: Microchip Technology PIC16F88 ' microchip.com ' PBPro Code, micro-Engineering Labs, Inc. ' melabs.com '--------Program Desciption-------- ' This program displays signed numbers in ' PicBasic Pro in an output range of values -50.0 to +50.0. ' The input is a 10-bit digital range of 0 to 1023. '------------Connections----------- ' 16F88 Pin Wiring ' --------- ---------- ' RA0 LCD pin 11(DB4) ' RA1 LCD pin 12(DB5) ' RA2 LCD pin 13(DB6) ' RA3 LCD pin 14(DB7) ' RA4 Resistive Input ' RB3 LCD Enable(E) ' RB4 LCD Register Select(RS) ' See schematic for the usual connections '---------LCD Connections--------- ' LCD Pin Wiring ' --------- ---------- ' 1 Ground(Vss) ' 2 + 5v(Vdd) ' 3 Center of 20K Pot(Contrast) ' 4 RB4(Register Select,RS) ' 5 Ground(Read/Write,R/W) ' 6 RB3(Enable) ' 7 No Connection(DB0) ' 8 No Connection(DB1) ' 9 No Connection(DB2) ' 10 No Connection(DB3) ' 11 RA0(DB4) ' 12 RA1(DB5) ' 13 RA2(DB6) ' 14 RA3(DB7) '---------Constants/Defines------- ' To free up AN4 (Pin RA4) for an analog input, the ' default LCD Register Select (RS) function must be ' removed from RA4. This is relocated to PORTB.4 ' using the LCD DEFINE statements below. All other ' default LCD pins and functions are left unchanged. ' See Curriculum Year 2, Lesson LCD3, POT Command and ' LCD DEFINES on this web site for more details. define LCD_RSREG PORTB ' PORTB - RS port define LCD_RSBIT 4 ' Bit 4 - RS bit DEFine ADC_BITS 10 ' Sets the number of bits in ' the result to 10 '---------Variables--------- x var word ' BYTE for potentiometer input temp_int var word ' WORD for temporary interger, temp_int temp_fract var word ' WORD for temporary fraction, temp_fract '---------Initialization-------- ANSEL = %00010000 ' Leaves AN4 in analog mode, but ' changes other analog bits to digital. ' See table below. ' Analog Bit Analog or Digital PIC16F88 Pin ' ------------ ------------------ -------------- ' AN0 Digital RA0 ' AN1 Digital RA1 ' AN2 Digital RA2 ' AN3 Digital RA3 ' AN4 Analog RA4 ' AN5 Digital RB6 ' AN6 Digital RB7 ADCON1 = %10000000 ' Right justifies 10-bit value of x ' in 16-bit WORD. Adds "0" in the ' 6 Most Significant bits of the Word, ' shifting the 10-bit value of x to ' the right. OSCCON = $60 ' Sets the internal oscillator in the ' 16F88 to 4 MHz '--------Main Code-------- loop: pause 1000 ' 1 second pause to allow LCD to setup adcin 4, x ' Read analog voltage on AN4 and ' convert to 10-bit digital value ' and store as x. lcdout $FE,1,dec x ' On first line, display 10-bit ' value of x x = x * 44/45 ' Begin converting 10-bit input range, ' (0 - 1023), to LCD output range '(-50.0 - 50.0). ' Output Range/Input Range = 100/1023 ' = 0.09775. Must use whole number ' numerator for calculation. Numerator ' must be less than 65 since ' 65 * 1023 > 65535, the limit for WORD ' variable. Found that 44/45 = 0.9777 ' approximates the significand of 0.09775. ' The shift in the decimal point is done ' in the next formula. temp_int = x/10 - 50 ' Get integer portion. Divide by 10 to ' shift decimal point from 0.9777 to ' 0.09777. Subtract 50 to shift 0 - 100 ' output range to -50.0 - 50.0. temp_fract = x//10 ' Get the remainder portion lcdout $FE,$c0,sDEC temp_int, ".", DEC1 temp_fract ' On the second LCD line, display the integer ' portion of x, temp_int, as a signed decimal ' (SDEC) and the remainder portion of x, ' temp_fract, as a decimal. pause 250 ' Pause 250 ms goto loop ' Jump to loop label end