I2C Line Following Sensors Help
Navigates to RoboticsConnection.com Home RoboticsConnection.com HomePage
RoboticsConnection User Forum
Home       Members    Calendar    Who's On
Welcome Guest ( Login | Register )
        


12»»

I2C Line Following Sensors Help Expand / Collapse
Author
Message
Posted Tuesday, April 22, 2008 5:33 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: Thursday, April 24, 2008 5:05 PM
Posts: 8, Visits: 12
Hi all,
I currently got the sensors after a long wait but after a lot of reading in the data-sheets, I couldn't know how to use the sensors.
Can anyone help me in reading the output of the sensors.
I use a PIC18F452 microcontroller. I have to follow a white line on a black floor. There are some turns.
The main problem is how to read the byte.
Please give me some example program.
Regards
Post #393
Posted Tuesday, April 22, 2008 6:14 AM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Moderators
Last Login: Wednesday, November 05, 2008 10:13 AM
Posts: 38, Visits: 54
If you are using the CCS compiler for the 452 then you must set up your I2C like this

#use I2C(master, sda=PIN_c4, scl=PIN_c3,slow)

Then to read the sensor use

        i2c_start();   // start condition
        i2c_write(ADDRESS + 1);
        value = i2c_read(0);
        i2c_stop();

where address is the address of the line sensor you are using.

If you are using a different compiler or language then you will have to port this over to it.

This will return a byte with either 5 or 7 bits set according to teh sensor status. If you have an older line sensor, it will have 5 bits, if it is a newer sensor with the holes on teh left and right to mount extra sensors then it will return 7 bits.

Hope this helps.

Ringo

Ringo

Ringo Davis

Post #394
Posted Tuesday, April 22, 2008 6:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: Thursday, April 24, 2008 5:05 PM
Posts: 8, Visits: 12
We are using a c18 compiler to compile the C-code in microchip mplab ide
Post #395
Posted Tuesday, April 22, 2008 7:21 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: Thursday, April 24, 2008 5:05 PM
Posts: 8, Visits: 12
What is #use in C code?
Post #396
Posted Tuesday, April 22, 2008 7:57 AM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Moderators
Last Login: Wednesday, November 05, 2008 10:13 AM
Posts: 38, Visits: 54
#Use is part of the CCS compiler. It just tells the compiler how to set up the I2C port. I'm not familiar with the C18 compiler, but I believe microchip has forums and I know they have a help system where you can talk or email a tech questions.   I just googgled "c18 compiler i2c" and it came up with lots of hits. Look for examples of code that talk to other devices, then just modify that to use the commands I listed above. This sensor is really eay to use since it only returns 1 byte, so any example you find should be good nough for you to modify.

Ringo

Ringo Davis

Post #400
Posted Tuesday, April 22, 2008 8:28 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: Thursday, April 24, 2008 5:05 PM
Posts: 8, Visits: 12
Im trying something lik this but this is not working? Do we have to set the pins as out/input of RC3,4


#include

#include
#include

unsigned char value;
//void StartI2C(void);
//void StopI2C(void);
void OpenI2C(unsigned char sync_mode,
unsigned char slew);
unsigned char WriteI2C(
unsigned char data_out);
unsigned char ReadI2C (void);


void configure_LEDS(void);

void display_on_LEDS(unsigned char value);


void main() {

while (1) {
configure_LEDS();

OpenI2C(MASTER, SLEW_ON);
//SSPADD = 0x50;

StartI2C();
WriteI2C(0x50 +1);
value = ReadI2C();

display_on_LEDS(value);

//StopI2C();

}
}



void configure_LEDS(void) {
TRISD = 0x00;
}


void display_on_LEDS(unsigned char value) {
LATD = value;
}
Post #401
Posted Tuesday, April 22, 2008 8:46 AM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Moderators
Last Login: Wednesday, November 05, 2008 10:13 AM
Posts: 38, Visits: 54
Just looking around the net I found this thread

http://www.robot-electronics.co.uk/forums/viewtopic.php?p=300&sid=67e9c8c9479b41ccb19f0c42d1a57aa6

It mentions setting the I2C bus speed, which should be 100khz. How you set this depends on the freq of the crystal you are using with your pic.

See if this helps

Ringo

Ringo Davis

Post #402
Posted Tuesday, April 22, 2008 6:28 PM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Moderators
Last Login: Wednesday, November 05, 2008 10:13 AM
Posts: 38, Visits: 54
Oh yeah, do you have pullups on both I2C lines? Somehting around 1.8K?

Ringo Davis
Post #410