'---------------Title-------------- ' File......16F877A_adc3.pbp ' Started....1/7/08 ' Microcontroller used: Microchip Technology PIC16F877A ' microchip.com ' PicBasic Pro Code: micro-Engineering Labs, Inc. ' melabs.com '--------Program Desciption-------- ' The program uses one of the analog-to-digital ' converters,(AN4), to measure the voltage ' on the center pin of a potentiometer (an analog signal). ' It then converts the analog voltage into an 10-bit ' digital value and displays the result as a ' voltage (0-5V) on an LCD. '------------Schematic------------- ' See schematic at: ' http://www.cornerstonerobotics.org/schematics/pic16f877a_adc1.pdf '----------Related Lesson---------- ' adc3.pbp (the 16F88 program) is used in ' the lesson Resistive Sensors at: ' http://www.cornerstonerobotics.org/curriculum/lessons_year2/erii23_resistive_sensors.pdf '-------------Revisions------------ ' 1/17/09: Change microcontroller from 16F88 to 16F877A '---------PIC Connections---------- ' 16F877A Pin Wiring ' --------- ---------- ' RB4 LCD pin 11(DB4) ' RB5 LCD pin 12(DB5) ' RB6 LCD pin 13(DB6) ' RB7 LCD pin 14(DB7) ' RA0 Center Lead of Potentiometer ' RA4 LCD Register Select(RS) ' RB3 LCD Enable(E) '----------LCD Connections--------- ' LCD Pin Wiring ' --------- ---------- ' 1 Ground(Vss) ' 2 + 5v(Vdd) ' 3 Center of 20K Pot(Contrast) ' 4 RA4(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 RB4(DB4) ' 12 RB5(DB5) ' 13 RB6(DB6) ' 14 RB7(DB7) '---------Constants/Defines-------- ' To free up AN0 (Pin RA0) for an analog input, the ' default four LCD Data Bits must be removed from RA0 - RA3. ' This is relocated to the upper 4 bits RB4 - RB7 in PORTB ' using the LCD DEFINE statements below. All other ' default LCD pins and functions are left unchanged. ' For details see: ' http://www.cornerstonerobotics.org/curriculum/lessons_year2/erii16_lcd3_pot_command_and_lcd_defines.pdf ' or ' Look around page 97 in the PicBasic Pro Compiler Manual. ' The PicBasic Pro Compiler Manual is on line at: ' http://www.microengineeringlabs.com/resources/index.htm#Manuals define LCD_DREG PORTB ' PORTB - Data bit Port define LCD_DBIT 4 ' Set starting Data Bit to bit 4 DEFine ADC_BITS 10 ' Sets the number of bits in ' the result to 10 '------------Variables------------- x var WORD ' WORD for potentiometer input temp_int var word ' Word for integer temp_fract var word ' Word for fraction '----------Initialization---------- 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. This changes the LCD ' values to 0 - 1023. ' The ADCON1 Register is Register 11-2: ADCON1 Register, ' look around page 128 in the 16F877A datasheet. ' For Microchip PIC DATASHEETS, see: ' http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=2046 ' Select 8-bit PIC Microcontrollers, then the device from the ' drop down menu. Now download the 16F877A Datasheet. '-------------Main Code------------ pause 1000 ' Pause to allow LCD to setup start: adcin 0, x ' Read analog voltage on AN0(RA0) ' and stores it as x. lcdout $FE,1,"POT =", DEC x ' Clears LCD screen, displays ' "POT =" and the 10-bit value of x x = x * 49/10 temp_int = x/1000 temp_fract = x//1000 lcdout $FE,$c0,"Voltage = ",DEC temp_int, ".", DEC3 temp_fract pause 500 ' Pause 1/2 second goto start ' Go to loop label end