User manual

Robotics experiment with PIC microcontroller l 69
A7.1 Write the Listing A7-1. Compile and download the code to Robo-PICA.
A7.2 Turn-off power and unplug the download cable from Robo-PICA.
Activity 7
Contactless object detection robot
/**********************************************************/
/***** Robot with Object Detector *************************/
/**********************************************************/
#include <motor.h>
int Adc; // Save analog Data
char Txt[6]; // Save String
void Read_Adc()
{
ADCON0=0b11011101; // 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); // Start up Delay
ANSELH.F4=0; // RBO ==> Digital IO
ANSEL=0xFF;
TRISA=0xFF;
Lcd_Init(&PORTD); // Initial LCD
Lcd_Cmd(LCD_CURSOR_OFF); // LCD Cursor OFF
while(PORTB.F0); // Wait Key Press
while(1)
{
Read_Adc(); // Read Analog 2
WordToStr(Adc,Txt); // Convert Data to string
Lcd_Out(1,1,Txt); // Show on LCD
if (Adc>300) // if Detect object in range
{
Backward(255);Delay_ms(500); // Backword and turn left
S_left(255);Delay_ms(400);
}
else
{
Forward(255); // Object out of range FORWARD
}
}
}
/**********************************************************/
Listing A7-1 : The C program of the contactless object detection robot