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.

Need Help with I2C!!!

Status
Not open for further replies.

hemnath

Advanced Member level 3
Advanced Member level 3
Joined
Jun 24, 2012
Messages
702
Helped
61
Reputation
120
Reaction score
57
Trophy points
1,308
Location
Chennai
Visit site
Activity points
6,589
I'm reading the 16 bit ADC value and displaying in LCD using MCP3421. When i use inbuilt I2C functions in CCS, everything works good. But I'm trying to use the registers and displaying the value. Below program shows how i configured. But in the display, it shows different value what i expect. Please help
HTML:
#include "18F2520.h"
#include "f2520_regs.h"
#fuses INTRC
#use delay(clock=4000000)

#define RS PIN_A2
#define EN PIN_A1

void lcd_init();
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);

/********** I2C functions**********/
void I2C_INIT();
void I2C_IDLE();
void I2C_START();
void I2C_RESTART();
void I2C_STOP();

unsigned char READ_ADC();
unsigned int16 high_data, low_data, final_data, value,byte2; 

/*****************************************************************
						Main Function
*****************************************************************/
void main()
{
lcd_init();
I2C_INIT();

printf(lcd_data,"switzer");
lcd_cmd(0x01);
delay_ms(1000);
while(1)
{
value = READ_ADC();
lcd_cmd(0x80);
printf(lcd_data,"%lu",value);
delay_ms(200);
}
}


/**********************************************************************
						I2C Initialization
**********************************************************************/
void I2C_INIT()
{
TRIS_C3 = 1;		//	configures SCL and SDA pins as inputs
TRIS_C4 = 1;

SSPCON1	=	0x38;	//	Set I2C master mode
SSPCON2	=	0x00;	

SSPADD	=	10;		//	100Khz at 4Mhz clock

CKE	=	0;			//	Disable SMBus specific inputs
SMP	=	1;			//	Slew rate control disabled 
}

void I2C_IDLE()
{
while((SSPCON2 & 0x1F) | R_W);	//	wait for idle and not writing
}

void I2C_START()
{
I2C_IDLE();
SEN	=	1;
}

void I2C_RESTART()
{
I2C_IDLE();
RSEN	=	1;
}

void I2C_STOP()
{
I2C_IDLE();
PEN	=	1;
}

unsigned char I2C_WRITE (unsigned char I2C_WRITEDATA)
{
I2C_IDLE();
SSPBUF	=	I2C_WRITEDATA;
return (!ACKSTAT);	//	returns 1 if transmission is acknowledged
}

int I2C_READ (unsigned char ACK)
{
unsigned char I2C_READDATA;
RCEN	=	1;
I2C_IDLE();
I2C_READDATA	=	SSPBUF;
I2C_IDLE();

if(ACK)
{
ACKDT	=	0;
}
else 
{
ACKDT	=	1;
}
ACKEN	=	1;
return (I2C_READDATA);
}

unsigned char READ_ADC()
{
I2C_START();
I2C_WRITE(0xD0);
I2C_WRITE(0x88);
I2C_RESTART();

I2C_WRITE(0xD1);
high_data = I2C_READ(1);
low_data = I2C_READ(1);
byte2 = I2C_READ(0);

I2C_STOP();

final_data = (high_data * 256) + low_data;

return final_data;
}

void lcd_init()
{
lcd_cmd(0x30);      // Configure the LCD in 8-bit mode, 1 line and 5x7 font
lcd_cmd(0x0c);      // display on and cursor off
lcd_cmd(0x01);      // clear display screen
lcd_cmd(0x06);      // increment cursor
lcd_cmd(0x80);      // set cursor to 1st line
}

void lcd_cmd(unsigned char c)
{
output_b(c);
output_low(RS);
output_high(EN);
delay_ms(15);
output_low(EN);
}

void lcd_data(unsigned char z)
{
output_b(z);
output_high(RS);
output_high(EN);
delay_ms(15);
output_low(EN);
}
What I'm doing wrong?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top