di3558l1.txt ;*********************************************************************************** ; ; LISTING 1 ; ; "Rainbow LED indicates voltage with color," EDN, December 7, 2004, pg 106 ; ;*********************************************************************************** ' PicBasic Pro program to read A/D on 12F675 ADC ' and display voltage A/D output using multi-color LED ' using a cold-to-hot color pallette. Center of range ' is dark. ' ' (c) 2003 by David Prutchi ' i VAR BYTE ' Define loop variable blue VAR BYTE ' Define blue pulse-width variable green VAR BYTE ' Define green pulse-width variable red VAR BYTE ' Define red pulse-width variable ' GPIO port 0 to GREEN LED ' GPIO port 1 TO RED LED ' GPIO port 2 to BLUE LED color VAR BYTE ' Define LED color variable x VAR BYTE ' Allocate A/D variable ' Set A/D Parameters DEFINE ADC_BITS 8 ' Use 10-bit A/D as 8-bit A/D ANSEL.3=1 ' Set ANS3 as analog input pin ANSEL.4 = 0 ' Set A/D clock ANSEL.5 = 1 ANSEL.6 = 0 ADCON0.0 = 1 ' Turn On A/D ADCON0.2 = 1 ' A/D channel 3 ADCON0.3 = 1 ADCON0.6 = 0 ' VDD is voltage reference ADCON0.7 = 0 ' Left Justify result ' Set GPIO port pins 0, 1 and 2 as outputs TRISIO.0=0 TRISIO.1=0 TRISIO.2=0 GoTo mainloop ' Skip subroutines ' SUBROUTINES ' ----------- ' Subroutine to read a/d converter getad: ADCON0.1 = 1 ' Start conversion PauseUs 50 ' Wait for conversion x = ADRESH Return ' MAIN ' ---- mainloop: GoSub getad ' Get x value by performing A/D conversion ' RGB ENCODING FUNCTION ' Convert A/D reading into color table ' Each color has 14 possible intensity levels IF x<=42 Then 'aqua red=x/3 blue=14 green=(42-x)/3 EndIF IF x>42 AND x<=84 Then 'shades of violet red=(84-x)/3 green=0 blue=14 EndIF IF x>84 AND x<=126 Then 'shades of green red=0 green=0 blue=(126-x)/3 EndIF IF x>126 AND x<=130 Then 'dark red=0 green=0 blue =0 EndIF IF x>130 AND x<=172 Then 'shades of red red=(x-130)/3 green=0 blue=0 EndIF IF x>172 AND x<=214 Then 'red / orange / yellow red=14 green=(x-172)/3 blue=0 EndIF IF x>214 Then 'yellow to white red=14 green=14 blue=(x-214)/3 EndIF ' PULSE WIDTH MODULATOR ' Each PWM frame has 14 steps. For i = 1 TO 14 ' Cycle through 14 steps of frame color=0 IF red>0 Then color=color+2 red=red-1 EndIF IF green>0 Then color=color+1 green=green-1 EndIF IF blue>0 Then color=color+4 blue=blue-1 EndIF GPIO=color PauseUs 100 ' Allow LEDs to shine for a few microseconds Next i GPIO=0 GoTo mainloop ' Do it forever End