반응형

- 시뮬레이터 -> 프로테우스 8.1 VSM

- 컴파일러 -> 코드비전 2.05



 


"Download" Proteus File - [Proteus] C_LCD_4Bit.zip

"Download" Source Code - [Codevision] C_LCD_4Bit.zip


 


<Proteus Circuit>






<Source Code>


#include <mega128.h>
#include <delay.h>

#define LCD_PORT_DIR    DDRB
#define LCD_PORT        PORTB

#define ENABLE  PORTB |= 0x04     
#define DISABLE PORTB &= ~0x04    

void ToggleEpinOfLCD(void)
{
    ENABLE;              
    delay_us(100);      
    DISABLE;            
    delay_us(100);     
}

//명령어 쓰기 함수
void instruction_out(unsigned char b) 
{
    // 상위 4BIT 출력
    LCD_PORT = b & 0xF0;   
    
    ENABLE;           //E = 1
    DISABLE;          //E = 0
                           
    //하위 4BIT 출력
    LCD_PORT = (b << 4) & 0xF0;  
                           
    ENABLE;           //E = 1
    DISABLE;          //E = 0
    
    delay_ms(30);
}
 
//LCD에 한문자 출력 함수
void char_out(unsigned char b) 
{
    LCD_PORT = (b & 0xF0) | 0x01;
    
    ENABLE;           //E = 1
    DISABLE;          //E = 0
    
    LCD_PORT = ((b << 4) & 0xF0) | 0x01;    
    
    ENABLE;           //E = 1
    DISABLE;          //E = 0
    
    delay_ms(30);
}
 
//문자열 출력 함수
void string_out(unsigned char b, char *str) 
{
    unsigned int i = 0;
  
    //LCD 위치 지정           
    instruction_out(b); 
    
    //NULL 문자를 만날 때 까지 캐릭터 출력     
    do{
        char_out(str[i]);
    } while(str[++i]!='\0'); 
}

// PORT 초기화 
void Port_Init(void)
{
    //PORTB 출력
    LCD_PORT_DIR = 0xFF;
                    
    //초기 값
    LCD_PORT = 0x00;
}

// LCD초기화
void init_LCD(void)  
{  
    delay_ms(40);
    
    LCD_PORT &= 0x0F;              
    LCD_PORT |= 0x30;              
    ToggleEpinOfLCD();          // 펄스

    delay_ms(6);
   
    LCD_PORT &= 0x0F;              
    LCD_PORT |= 0x30;              
    ToggleEpinOfLCD();          // 펄스

    delay_us(300);

   
    LCD_PORT &= 0x0F;              
    LCD_PORT |= 0x30;              
    ToggleEpinOfLCD();          // 펄스

    delay_ms(2);

   
    LCD_PORT &= 0x0F;              
    LCD_PORT |= 0x20;              
    ToggleEpinOfLCD();          // 펄스
    
    delay_ms(2);
                
    instruction_out(0x28);
    instruction_out(0x0C);
    instruction_out(0x01);
    instruction_out(0x06);
}

void main(void)
{
    delay_ms(100);

    // 포트 초기화
    Port_Init();
                  
    delay_ms(500);
    
    // LCD 초기화
    init_LCD();                 
        
    // 출력문자열
    string_out(0x80, "Hello World!"); 
    string_out(0xC0, "--Lcd Display!--");

    while(1)
    {
        ;
    }
}


반응형
Posted by 메가아재
,