/** * Backlight is open drain on pin D2, controlled by OC4, using Timer 3 * Contrast uses normal CMOS with OC3, also driven by Timer 3 */ void lcd_init(void) { //Set backlight to open drain mPORTDOpenDrainOpen( BIT_2 ); //Start the timer for contrast/brightness PWM OpenTimer3(T3_ON, LCD_PWMPERIOD); //Enable contrast control OpenOC3( OC_ON | OC_TIMER_MODE16 | OC_TIMER3_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH , LCD_PWMPERIOD, 0x500 ); //Enable brightness control OpenOC4( OC_ON | OC_TIMER_MODE16 | OC_TIMER3_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH , LCD_PWMPERIOD, 0x500 ); }
static void lcd_setup(void) { //mPORTDSetPinsDigitalOut(BIT_3); //mPORTDClearBits(BIT_3); mPORTDSetPinsDigitalOut( BIT_2 ); //Set backlight to open drain mPORTDOpenDrainOpen( BIT_2 ); //Enable contrast control OpenTimer2(T2_ON, LCD_PWMPERIOD); OpenOC4( OC_ON | OC_TIMER_MODE16 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH , LCD_PWMPERIOD, 0x0458 ); //Start the timer for contrast/brightness PWM //OpenTimer3(T3_ON | T3_PS_1_1 | T3_SOURCE_INT, LCD_PWMPERIOD); //PR3 = LCD_PWMPERIOD - 1; //OC3R = 0; //mT3SetIntPriority(4); //mT3ClearIntFlag(); //mT3IntEnable(1); //SetDCOC3PWM(LCD_PWMPERIOD >> 2); //SetDCOC3PWM(1); //Enable brightness control OpenOC3( OC_ON | OC_TIMER_MODE16 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH , LCD_PWMPERIOD, 0x500 ); // Open ENABLE line as output LCD_EN_CLR(); PORT_DIR_OUT(LCD_EN_P, LCD_EN); LCD_EN_CLR(); // Open RW and RS lines as output LCD_RS_CLR(); PORT_CLR(LCD_RW_P, LCD_RW); PORT_DIR_OUT(LCD_RS_P, LCD_RS); PORT_DIR_OUT(LCD_RW_P, LCD_RW); LCD_RS_CLR(); PORT_CLR(LCD_RW_P, LCD_RW); // Set data lines as output PORT_CLR(LCD_DATA_P, LCD_DATA_MASK); PORT_DIR_OUT(LCD_DATA_P, LCD_DATA_MASK); PORT_CLR(LCD_DATA_P, LCD_DATA_MASK); // Wait for proper power up task_delay(LCD_LONG_DELAY); task_delay(LCD_LONG_DELAY); task_delay(LCD_LONG_DELAY); task_delay(LCD_LONG_DELAY); // Wait for the LCD to power up correctly lcd_command(LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS); task_delay(LCD_SHORT_DELAY); lcd_command(LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS); task_delay(LCD_VERY_SHORT_DELAY); lcd_command(LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS); task_delay(LCD_VERY_SHORT_DELAY); // Set up the LCD function lcd_command(LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS | LCD_FUNCTION_SET_2_LINES); task_delay(LCD_VERY_SHORT_DELAY); // Turn the display on lcd_command(LCD_DISPLAY_CTRL_CMD | LCD_DISPLAY_CTRL_CURSOR_ON | LCD_DISPLAY_CTRL_BLINK_ON); task_delay(LCD_VERY_SHORT_DELAY); // Clear the display lcd_command(LCD_CLEAR_DISPLAY_CMD); task_delay(LCD_SHORT_DELAY); // Increase the cursor lcd_command(LCD_ENTRY_MODE_CMD | LCD_ENTRY_MODE_INCREASE); task_delay(LCD_SHORT_DELAY); lcd_command(LCD_DISPLAY_CTRL_CMD | LCD_DISPLAY_CTRL_DISPLAY_ON); task_delay(LCD_SHORT_DELAY); }