embeded/AVR (ATmega,ATtiny)2013. 12. 25. 00:41


사용부품
[링크 : http://www.us-technology.co.kr/product/product_main.asp?mode=101&smode=9] v3.0 보드
[링크 : http://devicemart.co.kr/goods/view.php?seq=1075057 ] LC1628 (동일 부품 아닐수도 있음)

참고소스
[링크 : http://www.avrprojects.net/attachments/lcdinterface.c]

참고 데이터시트
[링크 : http://www.ganasys.co.kr/kor/support_board2/pds_file/LC1628-BMDWH6Rev1.0.pdf]



lc1628.h
/*******************************************************
	LC1628 definition
*******************************************************/

/***************** position *****************/
#define	CLCD_RS	0x01
#define	CLCD_RW	0x02
#define	CLCD_E	0x04
#define	CLCD_DA	0xF0

/***************** command *****************/
/* RS R/W = 0x00 */
#define CMD_CLS 0x01	// Clear Display
#define CMD_RTH 0x02	// Return HOME
#define CMD_MOD 0x04	// Entry Mode Set
	#define MOD_INC 0x02
	#define MOD_DEC 0x00
	#define MOD_SHL 0x01
	#define MOD_SHR 0x00
#define CMD_DIS 0x08	// Display on/off
	#define DIS_ON  0x04
	#define DIS_CUR 0x02
	#define DIS_BLK	0x01
#define CMD_CUR 0x10	// Cursor or Display Shift
	#define CUR_CUR 0x08
	#define CUR_ALL	0x00
	#define CUR_LEF 0x04
	#define CUR_RIG 0x00
#define CMD_FNC 0x20	// Function Set
	#define FNC_DL8 0x10
	#define FNC_DL4 0x00
	#define FNC_DN2 0x08
	#define FNC_DN1 0x00
	#define FNC_H10	0x04
	#define FNC_H07	0x00
#define CMD_CGA 0x40	// Set CGRAM Address
	#define CRA_ADR 0x3F
#define CMD_DDA 0x80	// Set DDRAM Address

lc1628.c
#include < stdio.h >
#include < avr/io.h >
#include < util/delay.h >
#include "lc1628.h"

#define DEFAULT_DLY	20

static int uart_putchar(char c, FILE *stream)
{
	if (c == '\n') uart_putchar('\r', stream);
	loop_until_bit_is_set(UCSR0A, UDRE);
	UDR0 = c;

  return 0;
}

static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);


/*************************************************/
void ls1628_write_cmd(char cmd)
{
	/**************************************/
	// set RS & R/W
	PORTC = 0x00;

	// set E clock to high
	_delay_us(DEFAULT_DLY); // 40ns delay
	PORTC |= CLCD_E;

	// data set - high nibble
	PORTC |= (cmd & 0xF0);
	_delay_us(DEFAULT_DLY); // 80ns delay

	// set E clock to low
	PORTC &= (~CLCD_E);
	_delay_us(DEFAULT_DLY); // 10ns delay

	// release RW
	PORTC &= (~CLCD_RW);


	/**************************************/
	// set RS & R/W
	PORTC = 0x00;

	// set E clock to high
	_delay_us(DEFAULT_DLY); // 40ns delay
	PORTC |= CLCD_E;

	// data set - low nibble
	PORTC |= ((cmd & 0x0F) << 4);
	_delay_us(DEFAULT_DLY); // 80ns delay

	// set E clock to low
	PORTC &= (~CLCD_E);
	_delay_us(DEFAULT_DLY); // 10ns delay

	// release RW
	PORTC &= (~CLCD_RW);
}

void ls1628_write_char(char data)
{
	/**************************************/
	// set RS & R/W
	PORTC = CLCD_RS;

	// set E clock to high
	_delay_us(DEFAULT_DLY); // 40ns delay
	PORTC |= CLCD_E;

	// data set - high nibble
	PORTC |= (data & 0xF0);
	_delay_us(DEFAULT_DLY); // 80ns delay

	// set E clock to low
	PORTC &= (~CLCD_E);
	_delay_us(DEFAULT_DLY); // 10ns delay

	// release RW
	PORTC &= (~CLCD_RW);


	/**************************************/
	// set RS & R/W
	PORTC = CLCD_RS;

	// set E clock to high
	_delay_us(DEFAULT_DLY); // 40ns delay
	PORTC |= CLCD_E;

	// data set - low nibble
	PORTC |= ((data & 0x0F) << 4);
	_delay_us(DEFAULT_DLY); // 80ns delay

	// set E clock to low
	PORTC &= (~CLCD_E);
	_delay_us(DEFAULT_DLY); // 10ns delay

	// release RW
	PORTC &= (~CLCD_RW);
}

/*************************************************/
void ls1628_cmd_clear()
{
	ls1628_write_cmd(CMD_CLS);
}

void li1628_cmd_setpos(char y, char x)
{
	// y must 0(1st line) or 1(2nd line)
	ls1628_write_cmd(CMD_DDA | (y * 0x40 + x));
}

void ls1628_write_string(char *data)
{
	int idx = 0;

	while(data[idx] != 0x00)
		ls1628_write_char(data[idx++]);
}

/*************************************************/
void init_clcd(void)
{
	_delay_ms(50);
	ls1628_write_cmd(CMD_FNC | FNC_DL4 | FNC_DN2 | FNC_H07);	// fuction set
	PORTC = CLCD_E; // unknown

	_delay_us(80);	 											// 39 us wait
	ls1628_write_cmd(CMD_DIS | DIS_ON | DIS_CUR | DIS_BLK);		// disp on/off control
	_delay_us(80);	 											// 39 us wait
	ls1628_write_cmd(CMD_CLS);									// disp clear
	_delay_ms(2);												// 1.53ms wait
	ls1628_write_cmd(CMD_MOD | MOD_INC | MOD_SHR);				// entry mode set CMD_MOD
	_delay_us(80);
	ls1628_write_cmd(CMD_DDA);
	_delay_us(80);
}

void init_uart0(void)
{
	/* UART0 115200-N-8-1 */
	UBRR0H = 0;
	UBRR0L = 8; // 115k with U2X = 0
	UCSR0A = 0x00; // U2X = 0;
	UCSR0B = 0xD8;
	UCSR0C = 0x06; //Asyncronous - no parity - 1bits(stop) - 8bits(data)

    stdout = &mystdout;

	printf("\n\n\n\n\n");
}

void init_atmega(void)
{
	DDRC = 0xFF;
	PORTC = 0xFF;

	SFIOR = SFIOR | 0x04;

	init_uart0();
}

int main(void)
{
	init_atmega();
	init_clcd();

	li1628_cmd_setpos(0,3);
	ls1628_write_string("Hello World");
	li1628_cmd_setpos(1,0);
	ls1628_write_string("ATmega128 CLCD");

	return 0;
}

'embeded > AVR (ATmega,ATtiny)' 카테고리의 다른 글

ATmega8 로 ES-311(HS-311) 서보 제어하기  (2) 2013.12.29
ATtiny2313 / ATmega8 인공호흡!  (0) 2013.12.27
ATmega128 + LC1628 제어하기 2  (0) 2013.12.24
ATmega128 + LC1628 제어하기 1  (0) 2013.12.24
lc1628 예제 소스  (0) 2013.12.20
Posted by 구차니