READING VALUES FROM SENSOR AND THAT WRITING TO EEPROM


READING VALUES FROM SENSOR AND THAT WRITING TO EEPROM


code for above tittle project

 #include <LPC21xx.H>
#include "lcdheader.h"
static int count_num=0;
void i2c_start(void)
{
  I2CONCLR=(1<<3);     //CLEARING SI FLAG
I2CONSET=(1<<5);     //Start communication
while(!(I2CONSET & (1<<3))){}   //wait for SI to go high
  I2CONCLR=(1<<5);
I2CONCLR=(1<<3); //CLEARING SI FLAG

}

void i2c_stop(void)
{
I2CONSET = (1<<4);//STOPPING DATA TRANSFER
}

char i2c_recv(void)
{
  int  r;
I2CONCLR=(1<<3);
while(!(I2CONSET & (1<<3)));
r = I2DAT;
//lcd_cmd(0xc0);
//lcd_data(r);
  return(r);
}

void i2c_data(unsigned char data)
{
  I2CONCLR=(1<<3);    //clearing SI Flag
I2DAT=data;         //Sending SLAVE ADDRESS + READ/WRITE
// lcd_data(data);
while(!(I2CONSET & (1<<3))); //wait for SI to go high
  I2CONCLR = (1<<3);   //clearing SI Flag
}

int i2c_num(unsigned long int num)
{
static unsigned int count=0;
if(num)
{
count++;
i2c_num(num/10);
i2c_data(num%10 + 0x30);
}
return count;
}

int adc_conv(void)
{
int y=0;
int ch=0;
int a = 1;
while(!(ADDR & a << 31));

ch = ADDR & (7 << 24);
ch = ch >> 24;

y = ADDR & (0x3ff << 6);
y = y >> 6;

y = y * 0.3;
delay(10000);
return (y);
}

int main()
{
int i,n=6;
int d;
int adc_val =0;
PINSEL1 |= (1<<24);
PINSEL1 &= ~(1<<25);
ADCR |= (1<<1)|(4<<8)|(1<<16)|(0<<17)|(1<<21);

PINSEL0|=(1<<4)|(1<<6);   //SCL0 P0.2 and SDA0 P0.3
PINSEL0&=~((1<<5)|(1<<7));
I2CONSET=(1<<6);   //enable i2c in control register
I2SCLL =75;         //for 100kbps
I2SCLH = 75;        //for 100kbps


i2c_start();

/*******writing device eeprom address*******/

i2c_data(0xA0);
i2c_data(0x50);
lcd_init();//
/*******writing data in to eeprom*******/

adc_val = adc_conv();
delay(1000);

count_num=i2c_num(adc_val);
//   lcd_cmd(0x38);
delay(1000);

i2c_stop();
delay(1000);
i2c_start();
i2c_data(0xA0);
i2c_data(0x50);
//count_num=i2c_num();


i2c_stop();       //stop the communication
delay(1000);


i2c_start();    //start conditon

/*******Device Address****/
i2c_data(0xA0);
i2c_data(0x51);

i2c_start();        //Repeat Start
i2c_data(0xA1);
I2CONSET =(1<<2);   //to generate ack on recipt


for(i=0; i<count_num ;i++)
    {  
       
d=i2c_recv();   //Reading Data
     lcd_data(d);
}


i2c_stop();
delay(1000);
}

Comments

Popular posts from this blog

project for checking status of lights very day 12 :11 am