Hey there,
Im using a KL25Z and having tons of trouble for the past 2 weeks getting a LCD screen to initialize using I2C. Ive asked about it on reddit and Ive just been told the reason people use arduino is because its open source and these things are so **bleep** finicky. I guess i was hoping the community here can help.
I have this screen
https://www.amazon.ca/gp/product/B019K5X53O/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1
Default write address is 0x4E
This is board manual
https://spivey.oriel.ox.ac.uk/dswiki/images-digisys/5/56/KL25-refman.pdf
Im trying to use port E pins 1 and 0.
Ive tried using external 3.8k and 1k pull up resistors on sda and scl.
Any insight is of help, this is making me just want to use arduinos aha but i must prevail and learn.
Screen just stays light with top row as blocks.
My code as its called is as follows
int main (void) {
i2c_init(); // enable I2C
Delay(10);
init_I2C_LCD(); // enable screen
// Print_LCD("Hello, World!"); // print, commented out since doesn't seem to communicate properly.
}
where
void i2c_init(void)
{
//clock i2c peripheral and port E
SIM->SCGC4 |= SIM_SCGC4_I2C1_MASK;
SIM->SCGC5 |= (SIM_SCGC5_PORTE_MASK);
//set pins to I2C function
PORTE->PCR[0] |= PORT_PCR_MUX(6);
PORTE->PCR[1] |= PORT_PCR_MUX(6);
//set to 100k baud
//baud = bus freq/(scl_div*mul)
//100k = 24M/(1*240); icr=0x1F sets scl_div to 240
I2C1->F = (I2C_F_ICR(0x1F) | I2C_F_MULT(0));
//enable i2c and interrupts
I2C1->C1 = 0x00; // preemptive clearing for trouble shooting, didnt fix anything
I2C1->C1 |= (I2C_C1_IICEN_MASK | I2C_C1_IICIE_MASK); // enable
I2C1->S |= I2C_S_IICIF_MASK;
}
void init_I2C_LCD(void)
{
// Delay(100);
LCD_Write_4bit_CMD(0x3); // set 8 bit
Delay(100);
LCD_Write_4bit_CMD(0x3); // set 8 bit
Delay(10);
LCD_Write_4bit_CMD(0x3); // set 8 bit
LCD_Write_4bit_CMD(0x2); // set to 4 bit
LCD_Write_8bit_CMD(0x28); // set 2 lines
LCD_Write_8bit_CMD(0x0C); //
LCD_Write_8bit_CMD(0x06);
LCD_Write_8bit_CMD(0x80);
}
void LCD_Write_4bit_CMD(uint8_t c)
{
// input must be in form of 0x0c or 0xc
// For instructions RS & RW =0
uint8_t EHi = 0x04;
uint8_t ELo = 0x00;
I2C_TRAN;
I2C_M_START;
I2C1->S |= I2C_S_IICIF_MASK;
I2C1->D = LCD_ADDR_W;
I2C_WAIT;
I2C1->D = ((c<<4) | ELo);
I2C_WAIT;
I2C1->D = ((c<<4) | EHi);
I2C_WAIT;
I2C_M_STOP;
}
#define I2C_TRAN I2C1->C1 |= I2C_C1_TX_MASK
#define I2C_M_START I2C1->C1 |= I2C_C1_MST_MASK
#define I2C_M_STOP I2C1->C1 &= ~I2C_C1_MST_MASK
#define I2C_WAIT while((I2C1->S & I2C_S_IICIF_MASK)==0) {} \
I2C1->S |= I2C_S_IICIF_MASK;
#define LCD_ADDR_W 0x4E
im using the KL26Z and ive tried for a few weeks now to get this to work with the example sdk. did you end up getting the LCD to work?
Its likely that the LCD has a 5V power supply. They alot of the time on teh i2c breakouts it seems, have internal pull up resistors on the SDA and SCL pins that pull it up to the power supply of the LCD which is 5V. If youre not careful and plug that into your kl25Z which is 3.3v tolerant on the pins, then you will burn out your pins. I reccomend
1.) powering the LCD with 5V seperated from your microcontroller
2.) measuring the voltages on your SCD and SCL lines
3.) If ~5V on SCL and SDA lines, get a soldering iron, use a continuity device on a multi meter to track down which are the pull down resistors and de solder them from the I2C break out. Also worth using your old I2C pins on your microcontroller as a GPIO and see if they can toggle high and low, if they cant then the 5V previously probably burnt out your pins. RIP.
4.) Use a new breakout connection on board that supports I2C. Use external pull up resistors from 3.3V to your SDA and SCL lines. try 3.6 or 4k. ohm.
See if that fixes your inital problem.
Hi, thank you for your interest in NXP products
In this case I would like to recommend you import an example of I2C from our SDK in MCUXpresso, this will show you how to set up your device then you will be able to modify the code in base your project needs.
The steps to import a new project:
Also, you can consult this page for more information about i2c: MCUXpresso SDK API Reference Manual: I2C Slave Driver (nxp.com)
I hope this information help you, if you have any other question, please let me know.