Monday, September 29, 2008

Code for a simple line follower using PIC18F4550

Open the Demo project in your MPLAB program. Type in the following code into the code window overwriting what is written before. Click build all (or press Ctrl + F-10).




/** I N C L U D E S **********************************************************/
#include
#include

/** V A R I A B L E S ********************************************************/
#pragma udata

/** P R I V A T E P R O T O T Y P E S ***************************************/

/** V E C T O R R E M A P P I N G *******************************************/

extern void _startup (void); // See c018i.c in your C18 compiler dir
#pragma code _RESET_INTERRUPT_VECTOR = 0x000800
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code

#pragma code _HIGH_INTERRUPT_VECTOR = 0x000808
void _high_ISR (void)
{
;
}

#pragma code _LOW_INTERRUPT_VECTOR = 0x000818
void _low_ISR (void)
{
;
}
#pragma code

/** D E C L A R A T I O N S **************************************************/
#pragma code
/******************************************************************************
* Function: void main(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Main program entry point.
*
* Note: None
*****************************************************************************/
/** L E D ***********************************************************/


// Sensor Inputs
#define mInitAllSensors() TRISAbits.TRISA1=1;TRISAbits.TRISA2=1;
#define mInitAllLEDs() TRISBbits.TRISB0=0;TRISBbits.TRISB1=0;


#define SensorLeft PORTAbits.RA2
#define SensorRight PORTAbits.RA1


#define LeftLED PORTBbits.RB0
#define RightLED PORTBbits.RB1

int i=0;


void Delay_ms( long int x)
{
for(i=0;i
}
void Delay_s( long int x)
{
for(i=0;i}
void Delay_us(long int x)
{
for(i=0;i}

int sensor =0;



int SensorR()
{SetChanADC( ADC_CH1 );
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
sensor = ReadADC(); // Read result
if (sensor<350) return 1;
if(sensor>350) return 0;
}

int SensorL()
{SetChanADC( ADC_CH2 );
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
sensor = ReadADC(); // Read result
if (sensor<350) return 1;
if(sensor>350) return 0;
}






void code()
{

if(SensorL()==1 && SensorR()==0) {RightLED=1;LeftLED=0;}
else if (SensorR()==1 && SensorL()==0){LeftLED=1;RightLED=0;}
else {LeftLED=0;RightLED=0;}

}

void main(void)
{
ADCON1 |= 0x0F; // Default all pins to digital
mInitAllSensors();
mInitAllLEDs();




//ADC begins

OpenADC( ADC_FOSC_2 & ADC_RIGHT_JUST & ADC_1ANA , ADC_CH1 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS , 0b1010 );
OpenADC( ADC_FOSC_2 & ADC_RIGHT_JUST & ADC_1ANA , ADC_CH2 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS , 0b1010 );
Delay_ms( 50 ); // Delay for 50
//ADC ends


code();




}

//end main

/** EOF Demo02.c *************************************************************/

Sunday, December 16, 2007

The Brain

Well, the first thing that comes to your mind as soon as you think about a new robot is how are you gonna control the whole thing. I have used Microchip's PIC18F4550 controller for the purpose. The best part of this controller is its inbuilt support for USB interface.
Here are various steps for implementing this microcontroller....

Step 1: Build an USB devlopment board based on a PIC18F4550

Minimum requirement arround the PIC:
- an 20MHz crysal oscillator & the two 15 or 22pF associated capacitors.
- an USB-B connector powering the PIC (Vss & Vdd) and linked to D- & D+ pins.
- a 'decoupling' capacitor between Vss & Vdd (100nF & eventualy an other between 1 and 10 uF)
- a 'decoupling' capacitor between Vss & Vusb (220nF min, 470nF is also good)
- a button/switch S1 for hard reset on MCLR'
- a button/switch S2 for entering in Boot mode on RB4.



Step 2: Install needed softwares, get Boot Firmware Source Code & PDFSUSB utility
Microchip MPLAB 7.00+ available from http://www.microchip.com/ide/
Microchip C18 2.40+ Student Ed from http://www.microchip.com/c18/
Microchip USB Bootloader files

Step 3: Connect USB & 1st bootload detection
Connect the USB cable. It powers the PIC (not visible at this time).
Enter Boot Mode by holding down S2 (enter boot) and then pressing S1 (reset).
Normally, Windows will start detecting a new USB unknown device.

Step 4: Manually specify Microchip "PIC18F4550 Family Device" INF driver to MSWindows
At this point, you must have a window asking a driver for the new USB device.
The procedure depends on your Operating System version.

For Win XP, refuse Windows Update search, then choose "install from specific location", and "include the following location in the search": C:\MCHPFSUSB\Pc\MCHPUSB Driver\Release\
It should be recognised as "PIC18F4550 Family Device".

Ok, all is configured.

Note: I am assuming that you have already boot loaded your PIC.

Step 5: Testing a code on the PIC

Let us run the Microchip Demo02 project, a test program that can be USB-bootloaded.


1)Open the project "C:\MCHPFSUSB\fw\Demo02\Demo02.mcp" in MPLAB. Click 'Build all' or press Ctrl+F10.
2) On the BOARD enter Boot Mode by pressing [S1 Reset] while maintaining [S2 enter Boot].
3) Lauch Microchip tool : "C:\MCHPFSUSB\Pc\Pdfsusb\PDFSUSB.exe" (I call it "Microchip USB-Bootload utility").
4) In the drop-down list, select your PIC, actualy named "PICDEM FS USB 0 (Boot)".
5) Click "Load HEX File" : "C:\MCHPFSUSB\fw\Demo02\Demo02.hex" .
6) Click "Program Device".
7) Click "Execute" to start the PIC in user mode (ignore the error).
8) On the BOARD with this Demo02 program, when pressing S2, the state of pin RD1 must change (0V to 5V...).


Step 6: So, finally we can now program this PIC with our own programs written in language C. For details like the pin diagram of the PIC read its datasheet. You can get it from www.create.ucsb.edu/~dano/CUI/PIC18F4550datasheet.pdf

My first micromouse-- THE !

Hi everyone,
This part of my blog is dedicated to the first micromouse named ! (pronounced as the opening sound of a champagne :-), I am trying to develop. This is being developed keeping in view The IITB techfest to be held from 25-28 Jan 2008. Right now I am done with majority part of my Drive system and working on the IR sensors. I have used L297-L298 drive system to drive my bipolar stepper motors. For the processing part I have used Microchip's 18F4550 microcontroller. I have partitioned this blog into various parts which tells about each important component of the micromouse.