Here is a few code snippets. This was done a long time ago and I am better at it now so no critics please... The code works and it works well with the hardware, see attached schematic. This was all done in smt and uses very little pcb space. Today of course you just buy a chip that does it all but thats now and this was then!
Note that I didn't have an rtos when this was written so it was done the hard way. The el bl isr runs the buzzer too, and some other things.
(The lines of dots are the code I left out)
Note also, the code controls the bl frequency which sets the brightness of the bl. The voltage is set in hardware by a pot and influences the life expectancy of the bl. The el bl is just a light emitting capacitor. Stuff it with ac and it glows - simple.
Hope this is of some use.
// ......................
// LCD backlight defines
#define LCDBLPORT PORTC // port for backlight control
#define LCDBLDIR TRISC // direction reg for backlight
#define LCDBLPIN 1 // port pin for backlight control
//#define LCDBLPLOG // backlight logic (positive logic)
#define DEFBLFREQ 40 // default backlight frequency (Hz/10)
#define MINBLFREQ 10 // minimum backlight frequency
#define MAXBLFREQ 100 // maximum backlight frequency
// ......................
// LCD Backlight macros
#define enable_lcd_bl(state) flg_lcd_bl_en=!!state
#define lcd_bl_ison() flg_lcd_bl_hi
#define lcd_bl_isenabled() flg_lcd_bl_en
#define init_lcd_bl(freq) lcd_bl_dir=B_OUT;\
setup_timer_2(T2_DIV_BY_16,5);\
set_bl_freq(freq);\
enable_interrupts(INT_TIMER2);\
enable_lcd_bl(true)
#ifdef LCDBLPLOG
#define set_lcd_bl(state) lcd_bl_drive=flg_lcd_bl_en&state;flg_lcd_bl_hi=!!state
#else
#define set_lcd_bl(state) lcd_bl_drive=!(flg_lcd_bl_en&state);flg_lcd_bl_hi=!!state
#endif // LCDBLPLOG
#ifdef _FAST_CLOCK
#define set_bl_freq(freq) bl_freq=freq;\
PR2=2500L/bl_freq-1
#else
#define set_bl_freq(freq) bl_freq=freq;\
PR2=1250L/bl_freq-1
#endif // _FAST_CLOCK
// ......................
void init_proc() {
// ......................
// Initialize peripheral hardware.
init_buzzer(); // enable and turn off buzzer
lcd_init(); // initialize the lcd
init_lcd_bl(DEFBLFREQ); // initialize the lcd backlight to default
enable_global_interrupts(); // set it all in motion !!
init_switches(); // get initial keyboard switch states
setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_4); // setup spi - must be before init_eeprom
// ......................
main() {
// ......................
while(true) {
// ......................
if(read_switches()) { // read and process keypad switches
switch (swt) {
case 2 : lcd_brighten_bl(TRUE); break; // for testing
case 10 : lcd_brighten_bl(FALSE); break;// for testing
}
}
enable_lcd_bl(sys_flags.sys.awake); // set the backlight
// ......................
}
// Backlight interrupt handler - default 400 Hz for electro luminescent display.
// ......................
else if(TMR2IF) { // toggle the backlight drive
if(lcd_bl_ison()){set_lcd_bl(false);} // is backlight on, yes, turn it off
else {set_lcd_bl(true);} // no, turn it on
if(!--tmr_10ms) { // decrement the 10mS timer
tmr_10ms = bl_freq/4; // if zero, set timer to 10ms
if(!!buz_elaps) --buz_elaps; // decrement the buzzer timer to zero
if(!!dbc_elaps) --dbc_elaps; // decrement the debounce timer to zero
if(!!auto_rep) --auto_rep; // decrement autorepeat timer to zero
}
clrwdt(); // good place to tie up the dog
TMR2 = 0;
TMR2IF = false; // clear the interrupt flag
}
// ......................
// Alter the brightness level of lcd backlight.
void lcd_brighten_bl(unsigned char brighten) {
if(brighten) {
if(bl_freq < MAXBLFREQ) {
set_bl_freq(++bl_freq);
}
} else if(bl_freq > MINBLFREQ) {
set_bl_freq(--bl_freq);
}
}
[/font]