Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

I2C communication code for PIC16F877A in Mikro C

Status
Not open for further replies.

ammmmu

Newbie level 3
Joined
Nov 5, 2008
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,300
Dear friends,

I need to PIC 16F877A in I2C mikro C coding please help me my friends.....
 

coding for i2c

Here is code for I2C communication what i have!
Is for 8051 microcontrollers, but ca be easily adopt to any micro.

Is written in Keil C!

If you want in assembler you can find it on net!
 

i2c keil c

24XX512.h


#include "i2c.h" /* Need i2c bus */

#define EEPROMS_ID 0xA0 /* Microchip 24xx512 */

unsigned char EEPROM_get(unsigned int addr)
{
unsigned char dat;

I2C_start(); /* Start i2c bus */

I2C_write(EEPROMS_ID); /* Connect to EEPROM */
I2C_write(addr&0xF0); /* Request RAM address (Hight byte) */
I2C_write(addr&0x0F); /* Request RAM address (Low byte) */

I2C_start(); /* Start i2c bus */

I2C_write(EEPROMS_ID+1);/* Connect to EEPROM for Read */
dat = I2C_read(); /* Receive data */

I2C_noack();

I2C_stop(); /* Stop i2c bus */

return dat;
}

void EEPROM_set(unsigned int addr, unsigned char val)
{
I2C_start();

I2C_write(EEPROMS_ID); /* Connect to EEPROM */
I2C_write(addr&0xF0); /* Request RAM address (Hight byte) */
I2C_write(addr&0x0F); /* Request RAM address (Low byte) */

I2C_write(val); /* Write sec on RAM specified address */

I2C_stop(); /* Stop i2c bus */
}

Added after 46 seconds:

i2c.h

#define SDA P2_2 /* Set P2.7 = SDA */
#define SCL P2_3 /* Set P2.6 = SCL */
#define I2C_DELAY 0x0F /* For delay i2c bus */

void I2C_delay(void)
{
unsigned char i;

for(i=0; i<I2C_DELAY; i++);
}

void I2C_clock(void)
{
I2C_delay();

SCL = 1; /* Start clock */

I2C_delay();

SCL = 0; /* Clear SCL */
}

void I2C_start(void)
{
if(SCL)
SCL = 0; /* Clear SCL */

SDA = 1; /* Set SDA */
SCL = 1; /* Set SCL */

I2C_delay();

SDA = 0; /* Clear SDA */

I2C_delay();

SCL = 0; /* Clear SCL */
}

void I2C_stop(void)
{
if(SCL)
SCL = 0; /* Clear SCL */

SDA = 0; /* Clear SDA */
I2C_delay();

SCL = 1; /* Set SCL */

I2C_delay();

SDA = 1; /* Set SDA */
}

bit I2C_write(unsigned char dat)
{
bit data_bit;
unsigned char i;

for(i=0;i<8;i++) /* For loop 8 time(send data 1 byte) */
{
data_bit = dat & 0x80; /* Filter MSB bit keep to data_bit */
SDA = data_bit; /* Send data_bit to SDA */

I2C_clock(); /* Call for send data to i2c bus */

dat = dat<<1;
}

SDA = 1; /* Set SDA */

I2C_delay();

SCL = 1; /* Set SCL */

I2C_delay();

data_bit = SDA; /* Check acknowledge */
SCL = 0; /* Clear SCL */

I2C_delay();

return data_bit; /* If send_bit = 0 i2c is valid */
}

unsigned char I2C_read(void)
{
bit rd_bit;
unsigned char i, dat;

dat = 0x00;

for(i=0;i<8;i++) /* For loop read data 1 byte */
{
I2C_delay();

SCL = 1; /* Set SCL */

I2C_delay();

rd_bit = SDA; /* Keep for check acknowledge */
dat = dat<<1;
dat = dat | rd_bit; /* Keep bit data in dat */

SCL = 0; /* Clear SCL */
}

return dat;
}

void I2C_ack()
{
SDA = 0; /* Clear SDA */

I2C_delay();

I2C_clock(); /* Call for send data to i2c bus */

SDA = 1; /* Set SDA */
}

void I2C_noack()
{
SDA = 1; /* Set SDA */

I2C_delay();

I2C_clock(); /* Call for send data to i2c bus */

SCL = 1; /* Set SCL */
}

Added after 1 minutes:

main

#include <AT89X51.H>
#include <24xx512.h>

unsigned int del,i,x,temp;

char getCharacter (void)
{
char chr; // variable to hold the new character
while (RI != 1) {;}
chr = SBUF;
RI = 0;
return(chr);
}
void send (char a)
{
SBUF = a;
while (TI != 1);
TI=0;
}

void main (void){
char chr;
int i=0,j=0,count=0,inc=47;//inc=47
int index=0;
P1=0X00;
P2=0X00;

SCON = 0x50; // mode 1, 8-bit uart, enable receiver
TMOD = 0x20; // timer 1, mode 2, 8-bit reload
TH1 = 0XE6; // 1200
TL1 = 0XE6;
TR1 = 1;
TI = 0;
SBUF =0;

while(1){


your code

}//end of while

}//end of main
 

set byte in micro c

i am getting an error for both lib,
 

i2c code in c

which compiler you're going to use???
depending on that you have to verify if the base libraries are available...
 

i2c+sbuf

i m using microvission keil & proteous & this code is perfectly workin fine

Added after 1 minutes:

it only gives one warning msg bcaz of ack function bcaz m not using any ack during read & write in my code
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top