정보보안공부

마이크로프로세서_day12 본문

ATMega128

마이크로프로세서_day12

Steady_sp 2017. 5. 18. 21:34

==========================LCD==========================

***NAMSEOUL UNIVERSITY 문자 LCD에 출력하기

 

#define ALLCLR                  0x01
#define CURSOR_HOME       0x02
#define ENTMODE              0x06
#define FUNSET                 0x38
#define DISP_ON               0x0C
#define DISP_OFF              0x08
#define CURSOR_ON          0x0E
#define RIGHT 1  //LCD오른쪽으로 이동하는 헤더파일
#define LEFT 0   //LCD왼쪽으로 이동하는 헤더파일

 

void init_devices(void)
{    //초기화 함수
     MCUCR = 0x80;  
     EX_LED = 0x00;       //모든 LED off
     FND_S = 0xff;         //모든 FND off
     DOT_C = 0x00;       //DotMatrix off
     TCCR0 = 0x00;       //TCCR0 레지스터 초기화
}

 

void LCD_cmd_write(unsigned char cmd)
{    //명령어 써주는 함수
     LCD_CONTROL = 0x00;
     _delay_us(20);
     LCD_CONTROL = 0x04;
     LCD_DATA = cmd;
     _delay_us(20);
     LCD_CONTROL =0x00;
}

 

void LCD_data_write(unsigned char data)
{    //데이터 써주는 함수
     LCD_CONTROL = 0x00;
     _delay_us(2);
     LCD_CONTROL =0x05;
     _delay_us(20);
     LCD_DATA = data;
     _delay_us(20);
     LCD_CONTROL = 0x00;
}

 

void init_LCD(void)
{    //LCD초기화 함수
     LCD_cmd_write(FUNSET);     //function set
     _delay_ms(10);
     LCD_cmd_write(FUNSET);
     _delay_us(200);
     LCD_cmd_write(FUNSET);
     LCD_cmd_write(DISP_ON);    //display on
     LCD_cmd_write(ALLCLR);      //display clear
     LCD_cmd_write(ENTMODE);  //entry-mode set 
     _delay_ms(10);
}

 

void char_out(char c)
{    //LCD에 (char c)를 쓴다.
     LCD_data_write(c);     // R/W = 0, RS = 1
     _delay_ms(2);
}

 

void string_out(char *str)
{    //문자열을 문자로 쪼개는 함수
     int i = 0;
     while(str[i])    //str[]배열이 null값이되면 거짓으로 while문 탈출
     {
          char_out(str[i]);
          i++;
     }
}

 

void x_y_position(char x, char y)
{    //입력하려는 문자의 위치를 조절
     char position;
     position = y? x+0xc0 : x+0x80;  //조건연산자 : y가 참이면 왼쪽 거짓이면 오른쪽
     LCD_cmd_write(position);
     _delay_ms(5); 
}

 

void display_shift(char p)
{    //LCD를 오른쪽이나 왼쪽으로 이동시킨다.
     if(p==RIGHT)
      LCD_cmd_write(0x1C);
     else if(p==LEFT)
      LCD_cmd_write(0x18);
     _delay_ms(100);
}

 

int main(void)
{
     char * ptr1, *ptr2;
     ptr1 = "NAMSEOUL";
     ptr2 = "UNIVERSITY";
     init_devices();
     init_LCD();
     x_y_position(0,0);
     string_out(ptr1);
     x_y_position(1,1);
     string_out(ptr2);
     while(1)
     {
          display_shift(LEFT);     //LCD를 왼쪽으로 이동
          display_shift(RIGHT);   //LCD를 오른쪽으로 이동
          _delay_ms(20);
     }
}

 

=========================keyscan=========================
#include "myheader.h"

void init_devices(void)
{    //초기화함수
     MCUCR = 0x80;  
     EX_LED = 0x00;       //모든 LED off
     FND_S = 0xff;     //모든 FND off
     DOT_C = 0x00;   //DotMatrix off
     TCCR0 = 0x00;   //TCCR0 레지스터 초기화
}

unsigned char keyscan(void)
{     //Keyscan함수

unsigned char key, out, i;
out = 0x01;
DDRB = 0xff;
for(i=0;i<4;i++)
{
    PORTB = out;
    DDRB = 0x0f;
    _delay_ms(20);
    key = PINB & 0xf0;
    if(key != 0x00)

    {
        key |= out;
        break;
    }
    out <<= 1;

}
return key;

}

 

int main(void)
{
    unsigned char data;
    init_devices();
    while(1)
    {
        data = keyscan();
        EX_LED = data;
    }

}

 

==================keyscan으로 LCD에 표시====================

 

unsigned char keyscan(void)

{

unsigned char key, out, i;

out = 0x01;

DDRB = 0xff;

for(i=0;i<4;i++)

{

PORTB = out;

DDRB = 0x0f;

_delay_ms(20);

key = PINB & 0xf0;

if(key != 0x00)

{

key |= out;

break;

}

out <<= 1;

}

return key;

}

 

unsigned char key_convert(unsigned char key)

{

unsigned data = 0;

switch(key)

{

case 0x11 :

return '1';

break;

case 0x21 :

return '2';

break;

case 0x41 :

return '3';

break;

case 0x81 :

return 'A';

break;

case 0x12 :

return '4';

break;

case 0x22 :

return '5';

break;

case 0x42 :

return '6';

break;

case 0x82 :

return 'B';

break;

case 0x14 :

return '7';

break;

case 0x24 :

return '8';

break;

case 0x44 :

return '9';

break;

case 0x84 :

return 'C';

break;

case 0x18 :

return '0';

break;

case 0x28 :

return 'F';

break;

case 0x48 :

return 'E';

break;

case 0x88 :

return 'D';

break;

default :

return 0x20; //공백문자

}

return data;

}

 

 

 

 

=================================김치=================================

#include "myheader.h"

 

void init_devices(void)

void init_LCD(void)

void LCD_cmd_write(unsigned char cmd)

void LCD_data_write(unsigned char data)

void x_y_position(char x, char y)

void char_out(char c)

 

void CG_RAM(void)

{

unsigned char str1[]={0x1d,0x05,0x19,0x00,0x1f,0x11,0x1f,0x00}; //

unsigned char str2[]={0x09,0x1d,0x09,0x09,0x15,0x15,0x15,0x00}; //

int i;

LCD_cmd_write(0x40); //명령어쓰기 0x40~0x47부터8개가 한문자

_delay_ms(200);

for(i=0;i<8;i++)

{

LCD_data_write(str1[i]); //문자()

_delay_ms(10);

}

 

LCD_cmd_write(0x48); //명령어쓰기 0x48~0x4f

_delay_ms(200);

for(i=0;i<8;i++)

{

LCD_data_write(str2[i]); //문자()

_delay_ms(10);

}

 

}

 

int main(void)

{

MCUCR=0x80;

init_LCD();

CG_RAM();

x_y_position(0,0);

LCD_data_write(0x00);//0x08

_delay_ms(10);

x_y_position(2,0);

LCD_data_write(0x01);//0x09

_delay_ms(10);

while(1);

}

 

 

 

 

==============================내이름쓰기==============================

void CG_RAM(void)

{

unsigned char str1[]={0x00,0x1d,0x0b,0x15,0x00,0x04,0x0a,0x04}; //

unsigned char str2[]={0x00,0x09,0x0b,0x15,0x00,0x04,0x0a,0x04}; //

unsigned char str3[]={0x15,0x1d,0x15,0x1d,0x01,0x04,0x0a,0x04}; //

      int i;

 

LCD_cmd_write(0x40); //명령어쓰기 0x40~0x47부터 8개가 한문자

_delay_ms(200);

for(i=0;i<8;i++)

{

LCD_data_write(str1[i]); //문자()

_delay_ms(10);

}

 

LCD_cmd_write(0x48); //명령어쓰기 0x48~0x4f

_delay_ms(200);

for(i=0;i<8;i++)

{

LCD_data_write(str2[i]); //문자()

_delay_ms(10);

}

 

LCD_cmd_write(0x50); //명령어쓰기 0x50~0x58

_delay_ms(200);

for(i=0;i<8;i++)

{

LCD_data_write(str3[i]); //문자()

_delay_ms(10);

}

}

 

int main(void)

{

MCUCR=0x80;

init_LCD();

CG_RAM();

x_y_position(0,0);

LCD_data_write(0x00); //

_delay_ms(10);

x_y_position(2,0);

LCD_data_write(0x01); //

_delay_ms(10);

x_y_position(4,0);

LCD_data_write(0x02); //

_delay_ms(10);

while(1);

}

 

'ATMega128' 카테고리의 다른 글

마이크로프로세서_day13  (0) 2017.06.02
마이크로프로세서_day11  (0) 2017.05.12
마이크로프로세서_day10  (0) 2017.05.06
마이크로프로세서_day09  (0) 2017.04.28
마이크로프로세서_day08  (0) 2017.04.26
Comments