![]() | ||||||
|
PicBasic Experiments With The PIC16F877
| ||||||
|
This first application shows how to use the PIC16F877 Port A analog inputs. The analog values are displayed on the LCD. If you entered this page from another link, click HERE for the LCD connection schematic so you can follow along with these experiments. Figure 1 shows the schematic used for this experiment. Port A.0, A.1 and A.3 are used in analog-input mode. The readings taken from each port-pin are then displayed on the LCD.
Here is the PicBasic code:
' Picbasic Pro program to read pots on 16F877 ADC
' Define LCD pins
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1
TRISA = %00001011
' Allocate variables
x var byte
y var byte
z var byte
ADCON1 = 4 ' Set PortA 0, 1, 3 to A/D inputs
Low PORTE.2 ' LCD R/W line low (W)
Pause 100 ' Wait for LCD to start
Goto mainloop ' Skip subroutines
' Subroutine to read a/d converter
getad:
PAUSEUS 50 ' Wait for A/D channel acquisition time
ADCON0.2 = 1 ' Start conversion
WHILE ADCON0.2 ' Wait for it to complete
WEND
Return
' Subroutine to get pot x value
getx:
ADCON0 = $41 ' Set A/D to Fosc/8, Channel 0, On
Gosub getad
x = ADRESH
Return
' Subroutine to get pot y value
gety:
ADCON0 = $49 ' Set A/D to Fosc/8, Channel 1, On
Gosub getad
y = ADRESH
Return
' Subroutine to get pot z value
getz:
ADCON0 = $59 ' Set A/D to Fosc/8, Channel 3, On
Gosub getad
z = ADRESH
Return
mainloop:
Gosub getx ' Get x value
Gosub gety ' Get y value
Gosub getz ' Get z value
Lcdout $fe, 1, "x=", #x, " y=", #y, " z=", #z ' Send to LCD
Pause 100 ' Do it about 10 times a second
Goto mainloop ' Do it forever
End
| ||||||
|
Copyright © 1999-2008 Reynolds Electronics |
||||||
|
|