hemnath
Advanced Member level 3
I'm trying to read ADC MCP3421. But in display it shows different values.
Where i'm doing wrong? Please help.
In display it shows "255". I know, wrong value is displaying in the LCD. Please help
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