Programming for CFAH2004A-YMC-JP

cwpoff

New member
I'm using a microprocessor to control a CFAH2004A-YMC-JP LCD and having trouble getting it to work at all. I've looked through the software links and all the fourms and can't find any sample code to work off of. Does anyone have any code I could look at to see what I'm doing wrong or simple flow chart to go off of. I'm having trouble with the flow chart in the datasheets, and the datasheet also doesn't say what order of commands to use to print out characters. Thanks!
Looking for additional LCD resources? Check out our LCD blog for the latest developments in LCD technology.
 

CF Tech

Administrator
What processor are you using?

Take a look at Init_LCD_8() in CFAH_WinTestDlg.cpp CFAH_WinTest:

Code:
void CCFAH_WinTestDlg::Init_LCD_8(ubyte enable)
  {
  //This sequence is from the initialization on page 45 of the HD44780U
  //data sheet.
  Sleep(40);
  Write_LCD_Control_8(0x38,enable);
  //Yes, the data sheet says write it twice.
  Sleep(5);
  Write_LCD_Control_8(0x38,enable);
  //Yes, the data sheet says write it three times.
  Sleep(125);
  Write_LCD_Control_8(0x38,enable);
  //Yes, the data sheet says write it four times, but at least this one counts.
  //038h is Function set:
  //8 bit,
  //2 line
  //F = (5x8)
  Sleep(1);
  Write_LCD_Control_8(0x38,enable);
  //"Display off"
  Sleep(2);
  Write_LCD_Control_8(0x08,enable);
  //"Display clear"
  Sleep(2);
  Write_LCD_Control_8(0x01,enable);
  //006h is Entry mode set, increment, no shift
  Sleep(2);
  Write_LCD_Control_8(0x06,enable);
  //Display on, cursor on, blinking
  Sleep(2);
  Write_LCD_Control_8(0x0F,enable);
  //Clear the display again. This seems to fix a power-up problem
  //where the display shows "{{{{{" in a couple of places.
  Sleep(2);
  Write_LCD_Control_8(0x01,enable);
  }
 

parks853

New member
I am assuming you want C code for the micro. I had a hard time doing that. I have a great program for my cfa display, but mine is for 4bit mode. I used the wintest as a reference. you will just want to use the "write_lcd_control_8". and you will have to write a function that looks just like the above one so you can write data to the Register like change the write_lcd_data and make it look like the one I mentioned before.

Code:
#include mega8.h
#include delay.h //there shoudl be the < and > here around delay.h, but it wont show up.

/***************************** port defintions *****************************
                               PORTD.7-4....... DB7-4
                               PORTB.1......... RS 
                               PORTB.2......... enable
***************************************************************************/ 
#define RS PORTB.1
#define enable PORTB.2

void toggle_enable(void)  //toggles the enable line
{
    delay_ms(1);
    enable=1;
    delay_ms(1);
    enable=0;
}

void write_lcd_control_8(unsigned char data)  //use this for 8bit instructions
{
    RS=0;  //RS=instructions
    PORTD=(data) ;
    toggle_enable(); 
} 

void write_lcd_control(unsigned char data)  //use this for 4bit instructions
{
    RS=0;     //RS=instructions 
    PORTD=(data);  //upper nibble
    toggle_enable();
    PORTD=((data*16));   //lower nibble
    toggle_enable();
}  

void write_lcd_data(unsigned char data)   //use this for 4bit instructions
{
    RS=1;     //RS=data 
    PORTD=(data);  //upper nibble
    toggle_enable();
    PORTD=((data*16));   //lower nibble
    toggle_enable();
}

void write_lcd_data_8(unsigned char data)  //use this for 8bit data
{
    RS=1;  //RS=data
    PORTD=(data) ;
    toggle_enable();
}  

void init_lcd(void)
{
    delay_ms(40);
    write_lcd_control_8(0x30);  //function set
    delay_ms(5);
    write_lcd_control_8(0x30);  //function set
    delay_us(125); 
    write_lcd_control_8(0x30);  //function set
    delay_ms(1);
    write_lcd_control_8(0x20);  //4 bit mode

    /***************** start of 4 bit ****************/
            
    write_lcd_control(0x28);  //function set (interface is 4bits, # of display lines and character font
    delay_us(40);  
    write_lcd_control(0x08);   //display off
    delay_us(40); 
    write_lcd_control(0x01);   //display clear
    delay_ms(2);
    write_lcd_control(0x06);   //Entry mode set
    delay_us(40); 
    write_lcd_control(0x0C);   //display on, cursor off, blinking off
    delay_us(40);
    write_lcd_control(0x01);   //diplay clear
    
    write_lcd_data(0x41);
     write_lcd_data('a');
}
that is all before main. Dont forget to call for init_lcd in main before while(1){} you will have to change the ports for your own needs, and I also tied the R/W pin ot ground since i dont plan on reading from the lcd. hope that helps. and you might want to write a function that will display text for ya. I put an example of just displaying a or A. but you can display a whole string using a pointer and waiting for the null character

**edit.. I changed some stuff around, and added the function for 8bit mode. hopefully this can be used for a reference file for people that wnat help with C code. just changed everything for 8bit mode to the functions that end iwith _8
 
Last edited:

cwpoff

New member
Clear Display

Thanks for the code examples on initializing the LCD. I have everything working great now, except for one thing. I can’t get the LCD to clear the entire screen without rerunning the initialization function. Shouldn’t I be able to clear the screen with just using the Clear Display Instruction Code?
 

CF Tech

Administrator
> . . . Shouldn’t I be able to clear the screen with
> just using the Clear Display Instruction Code? . . .

Ya. That should work:
Code:
write_lcd_control(0x01);
delay_ms(1);
Make sure you are writing to the control register and not the data register. Also, it is slow, (2mS, I think) so put a delay after it or wait for the busy flag to drop.
 

cwpoff

New member
CFAH2004A-YMC-JP Heat

Should the LCD get fairly warm to the touch when it's on for a while with the backlight on? Thanks.
 

CF Tech

Administrator
An array backlight at full brightness can be warm to the touch.

Nothing should be uncomfortably warm to touch.

Do you have a current-limiting resistor in series with the backlight? What current are you driving into the backlight?
 

cwpoff

New member
It starts out fine, but after about 15 minutes of being on, it does start to get a little warmer then I'm comfortable operating the LCD at. When I checked the current, it was at 380 mA which I see is 100 mA over the datasheet typical, however is well under the maxiumum. I'm using a 10k potentiometer with the backlight and just adjusted it util the light came on. What value of a resistor in series with the backlight do you suggest to use?
 

CF Tech

Administrator
Please take a look at this thread for information on calculating the backlight current limiting resistor.

The 10K pot is going to act as basically a "switch". . . you would need something like a 50 ohm or 100 ohm.
 
Top