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.

Help with I2C!!! I'm stucked.

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 trying to read ADC MCP3421. But in display it shows different values.
Where i'm doing wrong? 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_START();
void I2C_RESTART();
void I2C_STOP();
void I2C_ACK();
void I2C_NAK();
void I2C_WAIT();

void I2C_WRITE(unsigned char data);
unsigned char I2C_READ(void);

int16 READ_ADC();
unsigned int16 value, high_byte, low_byte, final_byte;

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

while(1)
{
value	=	READ_ADC();
lcd_cmd(0x80);
printf(lcd_data,"%5lu",value);
}
}

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

SSPSTAT	=	0b10000000;		//	Slew rate disabled
SSPCON1	=	0b00101000;		//	SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1))
SSPCON2	=	0b00000000;

SSPADD	=	0x28;			//	100 Khz @ 4 Mhz oscillator
}

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

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

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

void I2C_ACK()
{
ACKDT	=	0;		//	Acknowledge data bit, 0 = ACK 
ACKEN	=	1;		//	ACk data enabled
while(ACKEN);		//	wait for ACK data to send on bus
}

void I2C_NAK()
{
ACKDT	=	1;		//	Acknowledge data bit, 1 = NAK
ACKEN	=	1;		//	ACK data enabled
while(ACKEN);		//	wait for ACK data to send on bus
}

void I2C_WAIT()
{
while((SSPCON2 & 0x1F) || (SSPSTAT & 0x04));		// wait for any pending transfer
}

void I2C_WRITE(unsigned char data)
{
SSPBUF	=	data;		//	Move data to SSPBUF
while(BF);				//	wait till complete data is sent from buffer
I2C_WAIT();				//	wait for any pending transfer
}


unsigned char I2C_READ(void)
{
unsigned char temp;
RCEN	=	1;			//	Enable data reception
while(!BF);				//	wait for buffer full
temp	=	SSPBUF;		//	Read serial buffer and store in temp register
I2C_WAIT();				//	wait to check any pending transfer
return temp;			//	Return the read data from bus
}


int16 READ_ADC()
{
I2C_START();
I2C_WRITE(0xD0);
I2C_ACK();
I2C_WRITE(0x88);
I2C_ACK();
I2C_RESTART();

I2C_WRITE(0xD1);
I2C_ACK();
low_byte	=	I2C_READ();
high_byte	=	I2C_READ();

final_byte	=	((high_byte << 8) | low_byte);
I2C_STOP();

return final_byte;
}

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);
}

In display it shows "255". I know, wrong value is displaying in the LCD. Please help
 

TRIS_C3 = 1; // configures SCL and SDA pins as inputs
TRIS_C4 = 1;

I would think these should be outputs. SDA would change to an input when detecting ACK and reading.
 
Have you been able to determine what part of the code might be causing the error; the reading or the displaying? In main(), rather then calling READ_ADC(), set value to a known number to see if it is properly displayed. If it does, then we can look at the ADC read. If it doesn't, then there is a problem with the display routines.
 

I'm Using MCP3421 . Device address is 1101 0000. I have pull up's between MCP3421 and uC. I'm clear about the hardware side. Please help me with the code what i'm doing wrong. It would be helpful for me. Thanks in advance :)

- - - Updated - - -

@spudboy488 : Displaying part of the program is good. I think the problem in reading the i2c value.
 

In READ_ADC() change:

low_byte = I2C_READ();
high_byte = I2C_READ();

to

low_byte = 0xaa;
high_byte = 0x55;

which should display 21930 if working correctly.

If that works, restore the original code in READ_ADC(). In I2C_Read() change:

temp = SSPBUF;

to

temp = 0x55;

which should display 21845 if working correctly.

If that works, restore the original code in I2C_Read().

Double check the MSSP configuration. Use a scope to verify bus activity. You may want to try Firmware Controlled Master mode to see if it makes a difference.
 

low_byte = 0xaa;
high_byte = 0x55;
When i changed as you mentioned above, nothing displayed on the lcd. So i have commented all I2C_ACK() in READ_ADC() function. Then it displays 21930 in the display.
Then, I restored the program and changed
temp = 0x55;
as you said, there was nothing displaying on the lcd. What could be the program.
 

@spudboy : I commented the line
HTML:
while(!BF);
in I2C_READ() function. Now it displays 21845 in the LCD.
@jayanth : yes may be the problem with ACK function. But i don't know how to solve this.
 

I also caught this error, where pro to help me
Untitled.png
In this topic I can post the file proteus + program + hex file
https://www.edaboard.com/threads/283540/#post1212516
 
Last edited:

Are you debugging on real hardware or in Proteus? If in the simulator, does it handle I2C communication fully?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top