| Swordfish Tutorial - ADC (Output to LCD) |
|
|
| Saturday, 04 April 2009 08:24 | |
|
This Analogue to Digital (ADC) PIC micro example is not only great for learning how to use ADC with Swordfish, but also to learn how a Function works. The Function will perform the required task/commands and then return with a Word variable called Result. you don't need to declare the variable "Result", as its the sole purpose of using a Function.
In this case, the Function is called "ADInAsVolt". It gets the ADC value on channel 0, and converts it to volts, and stores it in the Functions Result register. Just because the Function is at the start of the program does not mean that it is executed at the start - the first line to be executed is "ADCON1 = $07", and the program follows on from there. The command ADVal = ADInAsVolt instructs the program to call the function, and store the result in ADVal. Handy!
Device = 18F452 Clock = 20 // some LCD options... #option LCD_DATA = PORTD.4 #option LCD_RS = PORTD.2 #option LCD_EN = PORTD.3 // uses LCD and AD libraries... Include "LCD.bas" Include "ADC.bas" Include "convert.bas" Dim ADVal As Word // Read the AD port and scale for 0.00 - 5.00 volts... Function ADInAsVolt() As Word result = (ADC.Read(0) + 1) * 500 / 1024 End Function // Start Of Program... ADCON1 = %10000000 ADCON0.7 = 1 ADCON0.6 = 1 DelayMS (150) LCD.Cls // main program loop... While true ADVal = ADInAsVolt LCD.MoveCursor (1,1) LCD.Write("DC Volts = ", DecToStr(ADVal / 100), ".", DecToStr(ADVal, 2), " ") DelayMS(250) Wend
Note the PIC's power supply/oscillator are not shown
18F452 ADC Example
|
|
| Last Updated ( Friday, 24 April 2009 10:01 ) |
Whos Online
- theoptimist
- Jon Chandler
Forum Activity
DROID! Just got it - hop Monday, 08 February 2010 14:07 [4 replies]
Math - mast Monday, 08 February 2010 03:21 [4 replies]
win 98 / swordfish - Graham Saturday, 06 February 2010 17:34 [1 reply]
iHID - rickeybell Friday, 05 February 2010 15:28 [4 replies]
Problem in ON INDEX GOTOL - Roshan Thursday, 04 February 2010 07:58 [3 replies]
not familar with uart or ? - Jon Chandler Thursday, 04 February 2010 04:25 [5 replies]
Will this fry my CPU? - Graham Thursday, 04 February 2010 03:40 [16 replies]





Thanks for going to the trouble of creating this tutorial...
Cheers Jon, sounds like a good rapid way to calibrate the...
Thanks Jon, should be working now - had some issues with ...
The video link appears to be broken by the way.
Nice work Graham! Love using a pot as the user interface...
The devices in the schematic above are actually user desi...
hello everyone,i am using a tlp 434 & rlp 434 rf module t...
I've been working on a better way to calibrate the servo ...