ex8_1.h

 

#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
/* LCD 활성화 Enable(E) */
#define ENABLE (PORTA |= 0x04)
#define DISABLE (PORTA &= 0xFB)
/* LCD Instruction */
#define FUNCSET 0x28
#define ENTRYSET 0x06
#define LCDCLEAR 0x01
#define RETURNHOME 0x02
#define DISPLAYON 0x0C
#define CURSORON 0x0A
#define BLINKON 0x09
/* Timer */
#define CPU_CLOCK 16000000
#define TICKS_PER_SEC 1000

----------------------------------------

ex8_1.c

 

#include "ex8_1.h"
/* Timer 루틴 */
volatile unsigned int tic_time;
SIGNAL(SIG_OVERFLOW0)
{
tic_time++;
TCNT0 = 256 - (CPU_CLOCK / TICKS_PER_SEC / 64);
}
void delay_ms(unsigned int msec)
{
tic_time = 0;
while(msec > tic_time);
}
void init_timer(void)
{
TCCR0 = (1 << CS02) | (0 << CS01) | (0 << CS00);
TCNT0 = 256 - (CPU_CLOCK / TICKS_PER_SEC / 64);
TIMSK = (0 << OCIE0) | (1 << TOIE0);
sei();
}
/* 사용할 포트 초기화 */
void init_porta(void)
{
DDRA = 0xFF; // PORTA 출력
PORTA = 0xFF; // 초기 값
}

/* instruction function */
void instruction(unsigned char b) // 명령어 쓰기 함수
{
PORTA = b&0xF0; // 상위 4비트 출력
ENABLE;
DISABLE;
PORTA = (b<<4)&0xF0; // 하위 4비트 출력
ENABLE;
DISABLE;
delay_ms(2);
}
/* LCD에 한 문자 출력 */
void char_out(unsigned char b) // LCD에 한 문자 출력 함수
{
PORTA = (b&0xF0)|0x01; // 상위 4비트 출력
ENABLE;
DISABLE;
PORTA = ((b<<4)&0xF0)|0x01; // 하위 4비트 출력
ENABLE;
DISABLE;
delay_ms(2);
}
/* LCD에 문자열 출력 */
void string_out(unsigned char b, unsigned char *str)
{
unsigned int i = 0;
instruction(b); // LCD 위치 지정
do{
char_out(str[i]);
}while(str[++i]!='\0');
}
/* lcd 초기화 함수 */
void init_lcd(void)
{
init_porta(); // LCD를 연결한 포트 초기화
delay_ms(50);
instruction(FUNCSET); // LCD FUNCTION SET(16x2 LINE, 4 BIT, 5x8 DOT)
delay_ms(2);
instruction(FUNCSET); // LCD FUNCTION SET(16x2 LINE, 4 BIT, 5x8 DOT)
delay_ms(2);
instruction(DISPLAYON); // LCD DISPLAY ON, CURSOR OFF, BLINK OFF
instruction(LCDCLEAR); // LCD CLEAR
delay_ms(2);
instruction(ENTRYSET); // LCD ENTRY MODE SET
instruction(RETURNHOME); // RETURN HOME
instruction(LCDCLEAR); // LCD CLEAR
}
/* 메인 함수 */
int main(void)
{
unsigned char shift=0;
init_timer();
init_lcd();
instruction(LCDCLEAR); // LCD를 지운다
string_out(0x80, "LOTTO"); // LCD 첫번째 라인
string_out(0xC0, "ATmega128!"); // LCD 두번째 라인
delay_ms(2);
return 0; //
} //

/*while(1)
{
for(shift = 0; shift < 8; shift++) {
// S/C = 1, R/L = 1, 화면 오른쪽으로 시프트
instruction(0x1C);
delay_ms(500);
}
for(shift = 0; shift < 16; shift++) {
// S/C = 1, R/L = 0, 화면 왼쪽으로 시프트
instruction(0x18);
delay_ms(500);
}
for(shift = 0; shift < 8; shift++) {
// S/C = 1, R/L = 1, 화면 오른쪽으로 시프트
instruction(0x1C);
delay_ms(500);
}
}
return 0;
}
*/

 

by 리베리온 2013. 12. 29. 23:44