LCD code for SX (lcd.c)

SX sample code that works with LCD displays based on the Hitachi 44780 series of controllers.




#include "lcd.h"

#include "ports.h"





void lcd_cmd(char cmd)	//issue a command without munging state of other bits on port

{

	char temp;

	asm clrb _LCD_CONTROL_PORT.2	;//first setup control lines

	asm setb _LCD_CONTROL_PORT.1

	delay_us(1);	

	asm mov W,param00_lcd_cmd				;//now move command into W

	asm	and W,#US $F0			;//get left bits

	asm	mov _LCD_DATA_PORT,W				;//output correct bits to data port

	delay_us(1);							//now strobe E to enter command part one

	asm clrb _LCD_CONTROL_PORT.1

	delay_us(1);

	asm setb _LCD_CONTROL_PORT.1



	//now we need to do the same thing with the nibble swapped version of cmd



	asm clrb _LCD_CONTROL_PORT.2	;//first setup control lines

	asm setb _LCD_CONTROL_PORT.1

	delay_us(1);	

	asm mov W,<>param00_lcd_cmd				;//now move command into W

	asm	and W,#US $F0			;//get left bits

	asm	mov _LCD_DATA_PORT,W				;//output correct bits to data port

	delay_us(1);							//now strobe E to enter command part one

	asm clrb _LCD_CONTROL_PORT.1

	delay_us(1);

	asm setb _LCD_CONTROL_PORT.1



	delay_us(50);

	return;									//and I'm spent!

}



void lcd_print_char(char cmd)	//issue a command without munging state of other bits on port

{

	char temp;

	asm setb _LCD_CONTROL_PORT.2	;//first setup control lines

	asm setb _LCD_CONTROL_PORT.1

	delay_us(50);	

	asm mov W,param00_lcd_print_char				;//now move command into W

	asm	and W,#US $F0			;//get left bits

	asm	mov _LCD_DATA_PORT,W				;//output correct bits to data port

	delay_us(50);							//now strobe E to enter command part one

	asm clrb _LCD_CONTROL_PORT.1

	delay_us(50);

	asm setb _LCD_CONTROL_PORT.1



	//now we need to do the same thing with the nibble swapped version of cmd



	asm setb _LCD_CONTROL_PORT.2	;//first setup control lines

	asm setb _LCD_CONTROL_PORT.1

	asm mov W,<>param00_lcd_print_char				;//now move command into W

	asm	and W,#US $F0			;//get left bits

	asm	mov _LCD_DATA_PORT,W				;//output correct bits to data port

	delay_us(50);							//now strobe E to enter command part one

	asm clrb _LCD_CONTROL_PORT.1

	delay_us(50);

	asm setb _LCD_CONTROL_PORT.1



	return;									//and I'm spent!

}





void lcd_init(void)

{

	DDRE(0x0F);

	DDRD(0xF9);

	//okay...now we're ready to initialize the LCD to nibble mode

	asm clrb _LCD_CONTROL_PORT.2

	asm setb _LCD_CONTROL_PORT.1

	asm mov W,#US $20

	asm mov	_LCD_DATA_PORT,W

	delay_us(1);

	asm setb _LCD_CONTROL_PORT.1

	delay_us(1);

	asm clrb _LCD_CONTROL_PORT.1

	delay_us(50);

	//okay...now we're in the ever-coveted nibble mode-> we can use lcd_cmd()



	lcd_cmd(0x28);				//buy our way into two line nibble mode

	lcd_cmd(0x01);				//clear the display

	delay_ms(2);				//clearing takes some time

	lcd_cmd(0x02);				//set cursor and display to home

	delay_ms(2);				//damn slow ass LCD controller

	lcd_cmd(0x0D);				//display_on, cursor on, cursor blink

	delay_ms(2);



	return;						//we're done

}



void lcd_clear(void)

{

	lcd_cmd(0x01);

	delay_ms(2);

	lcd_cmd(0x02);

	delay_ms(2);

	lcd_cmd(0x0D);



	return;

}


Copyright© 2003 Pavel Baranov.