Anda di halaman 1dari 2

' _______________________________________________________________________

' '
' This shows a way to use an ordenary LED as both LED light '
' and sensor at the same time. '
' '
' You can use this to make an LED in to a "Light with touch sensor" '
' '
' This example is reworked from a site with code for ATMEL mcu. '
' http://www.thebox.myzen.co.uk/Workshop/LED_Sensing.html '
' '
' Feel free to use it if you like it. '
' If you have a better way of doing this, please let me know. '
' (You find me @ the Crownhill forum, Username: seconika '
'
' Take a look at the end of the code to get the hardware part :)
' _______________________________________________________________________'

Device = 18F452 ' Select device


Xtal = 20 ' Set speed
PortB_Pullups = Off ' Disable Pullup on portB
' Set Serial output.
Hserial_Baud = 38400 ' Set baud rate to 9600
Hserial_RCSTA = %10010000 ' Enable serial port and continuous rece
ive
Hserial_TXSTA = %00100100 ' Enable transmit and asynchronous mode
Hserial_Clear = On ' Enable Error clearing
on received characters
'Set ADC
Adin_Res 10 ' Set the resolu
tion to 10
Adin_Tad FRC ' Choose the RC osc for
ADC samples
Adin_Stime 80 ' Allow 80us for charge
time
DelayMS 100 ' Wait for PICmicro to s
tabilise
TRISA.0 = 1 ' Setup bit-0 of
PortA as an input
ADCON1 = %10000010 ' Set PORTA analog and r
ight justify result

Dim Raw1 As Byte ' get some variables to save data in.
Dim Raw2 As Byte ' get some variables to save data in.
Dim LightVal As Byte ' get some variables to save data in.
Low PORTA ' Set PORTA to logic zero.
DelayMS 100
Main:
While 1 = 1 ' Create an endless loop
'1) Turn LED on.
Low PORTA.0 ' Set PORTA.0 To logic zero.
High PORTB.7 ' Set PORTB.7 To logic one.
DelayMS 100 ' Now the Led can shine for a while :)
'2) Set LED in measurement mode (Turns the LED off)
TRISA.0 = 1 ' Set PORTA.0 To ADC input
Low PORTB.7 ' Set PORTB.7 to logic zero
'3-4) Take a measurement of the analogue voltage, this is a reference level
for later use.
Raw1 = ADIn 0 ' Read the ADC

'5) Wait while the photon current is integrated


' the longer the more sensitive but too long
' and effects other than photo current dominate.
DelayMS 8
'6) Measure the voltage again and subtract it from
' the reference value you took before the integration time.
' This value is your light reading
Raw2 = ADIn 0 ' Read the ADC

Low PORTA.0 'Turn LED on again


High PORTB.7 '
LightVal = Raw2 - Raw1 ' Take the the two values and substract the last re
ading
' from the reference reading.
' Voala! Here you have a variable to use.

HSerOut ["Data: ", Dec LightVal ,13] 'This serout was added for debugging...
Wend 'Make it all over again :)

'¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ HARDWARE ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤


'
' Wire up the LED so that the anode is connected to a digital pin
' and the cathode is connected to a an analogue input pin.
' Don t forget a current limiting resistor in each LED.
'
' So, that s simple enough.
'
' PORTB.7 ------>|-------|||||------- PORTA.0
' LED RESISTOR
'

Anda mungkin juga menyukai