الخطوة التالية هى تنفيذ كود المصدر لبرنامج يستخدم دالة التحويل وعرض هذه القيمة , لهذا الغرض يمكن مراقبة وتحليل كود المصدر للبرنامج التالى :
كود:
//LCD Pin Definition
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;
//CD TRIS Definition
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;
void main( void )
{
//Variables Declaration .
unsigned int Radc, DisI;
float Dis;
char Text[16];
//Set the ADC module with pin AN3
//as positive reference voltage.
ADCON1 = 0b11000001;
//Initiation LCD
Lcd_Init();
//Clearing the cursor.
Lcd_Cmd(_LCD_CURSOR_OFF);
//Printing text.
Lcd_Out( 1, 1, "Distance:");
while(1) //Infinite Loop.
{
//Reading analog channel 0 .
Radc=ADC_Read(0);
//Implementation of the transfer function Equation
Dis = (14226.02784/Radc)-4.793814433;
//force the result to an integer value.
DisI=Dis;
//convert the integer value to string.
IntToStr( DisI, Text );
//Print the sensor reading.
Lcd_Out( 2, 1, Text );
//Delay 100m-seconds.
delay_ms(100);
}
}


