User manual

68l Robotics experiment with PIC microcontroller
A6.1 Write the Listing A6-1. Compile and download the code to Robo-PICA.
A6.2 Plug the GP2D120 sensor to RA2 port pin of Robo-PICA. (ready from building activty 3).
A6.3 Run the program. Place some object at the front of GP2D120 module. Observe the
LCD operation.
A6.4 Adjust the distance of the object from GP2D120 sensor and observe the result.
From testing, you will found the GP2D120 can detect the object in range 4 to 30cm.
Activity 6
Testing GP2D120
int Adc;
char txt[6];
void Read_Adc()
{
ADCON0=0b11001001; // Select Analog2 RC_Mode and ADON
ADCON0.GO=1; // Start Convert
while(ADCON0.GO); // Wait Until Convert Complete
Adc=(ADRESH*4)+(ADRESL/64); // 10 bit Data ==> Adc
}
void main()
{
Delay_ms(1000);
Lcd_Init(&PORTD); // Initial LCD
Lcd_Cmd(LCD_CURSOR_OFF); // LCD Cursor OFF
Lcd_Out(1,1,"Raw Data= "); // Show Fisrt Line Text
while(1)
{
Read_Adc();
WordToStr(Adc,txt); // Convert To Show on LCD
Lcd_Out(1,10,txt);
if (Adc<90) // If Data < 90 It Out of Range
{
Lcd_Out(2,1,"Out of Range");
}
else
{
Adc = (2914/(Adc+5))-1; // Convert Data to Centimeter
WordToStr(Adc,txt); // Convert Data to String
Lcd_Out(2,1,"In CM= "); // Show on LCD
Lcd_Out(2,6,txt);
}
Delay_ms(1000);
}
}
Listing A6-1 : The GP2D120 demonstration program of Robo-
PICA