Thursday, September 23, 2004

Analog input with the PIC

I'm working on the analog input with the PIC controller right now. This is interesting - thinking of all the applications that use this type of control, potentiometers are in stero volume and tuning knobs, in flex sensors and in measuring temperature.

I hooked up a potentiometer to my PIC at RA0 which is the second pin on the top left of the chip - just under the 10K resistor. There is also a serial cable connected to the computer to display the value of the potentiometer or any variable resistor. Displaying from 0 to 1023 you can use this variable to control other elements on the board.




Later on I replaced the potentiometer with a photocell and had to add a 10k resistor with it to keep the current smooth. still not sure what that means. The resistor and a leg of the photocell was plugged into RA0 and the second leg of the photocell was plugged into ground.



The code I was working on was a distance location system. If nothing obstructed the light then the green LED stayed on. If a shadow or hand came into view of the photocell the yellow light would turn on (green off). If the shadow/hand got to close the red light turns on, and if impact occurs the red LED blinks.




here is the code.

'------------------------------------------------------------
' PicBasic Pro program to display result of
' 10-bit A/D conversion through serial at 9600 baud
'
' Connect analog input to channel-0 (RA0)

' Define ADCIN parameters
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS

ADCvar VAR WORD ' Create variable to store result

TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and right justify result
Pause 500 ' Wait .5 second

main:
ADCIN 0, ADCvar ' Read channel 0 to adval
serout2 PORTC.6, 16468, [DEC ADCvar, 13, 10] ' print it to serial out,

'determine the analog input and control the LEDs
SELECT CASE ADCvar
CASE IS > 820 'blink red led
high portd.6
pause 250
low portd.6
pause 250
low portd.5
low portd.4
CASE IS > 680 'turn on red led
high portd.6
low portd.5
low portd.4
CASE IS > 575 'turn on yellow led
low portd.6
high portd.5
low portd.4
CASE else ' keep green led on
low portd.6
low portd.5
high portd.4

END SELECT

'Button Code here
if portb.0 = 1 then ' if the switch is closed on pin RB0
low portd.0 ' set pin RD1 low
else
high portd.0 ' set RD1 high
SEROUT2 portc.6, 16468, ["Hello World!", 13, 10]
endif
GoTo main
'-------------------------------------------------------------------

The difficulties today came from the BASIC programming language. I ended up finding the error. After using IF THEN statements I decided to test out the SELECT CASE statement and use it instead. I forgot to remove the THEN statement and the compiler caught it. After referencing the manual I found my error. Compile then Programmer then test and hoorah!

It's simple enough. I'm going to look for a simple device to attach but will keep the board as is until class. Also bought some more jameco products so I'll have more to play with, the flex sensor should come next week.

0 Comments:

Post a Comment

<< Home