AVR study

[AVR] 간단한 LCD 구동 코드 예제

techbro 2013. 1. 29. 18:38
반응형

#include<avr/io.h>
#include<util/delay.h>

#define RS 0
#define RW 1
#define E 2
#define DATA PORTA
#define LCD_CLEAR 0x1f
#define LCD_RTH 0x02
#define LCD_RSH 0x07
#define LCD_LSH 0x06
#define LCD_NOMAL 0x04
#define LCD_on 0x0c
#define LCD_off 0x08
#define LCD_8bit 0x30
#define LCD_ALLon 0x0e
#define LCD_5X8 0x38
//#define LCD_
unsigned char a[]="IHATEAVR";
unsigned char c[]="hellomysemina";

void LCD_CR(unsigned char com)
{
    PORTC&=~(1<<RS);
    PORTC&=~(1<<RW);
    PORTC|=(1<<E);
    _delay_ms(50);
    DATA=com;
    _delay_ms(50);
    PORTC&=~(1<<E);

}

void LCD_DB(unsigned char com)
{
    PORTC|=(1<<RS);
    PORTC&=~(1<<RW);
    PORTC|=(1<<E);
    _delay_ms(10);
    DATA=com;
    //delay_ms();
    PORTC&=~(1<<E);
}  
void LCD_PST(unsigned char col, unsigned char row)
{
    LCD_CR(0x80|(row+col*0x40));
}
void str(unsigned char* com)
{
while(*com!=0)
{
 LCD_DB(*com);
 com++;

}

}
void init_buffer(void)
{

PORTC|=(1<<RS);
PORTC|=(1<<RW);
PORTC|=(1<<E);
_delay_ms(10);
while(DATA!=0)
{
LCD_DB(0);
_delay_ms(10);
}
}
int main()
{
    int b;
    DDRA=0xff;
    DDRC=0xff;
    PORTA=0x00;
    PORTB=0x00;
    LCD_CR(0x30);  
    _delay_ms(2);
    LCD_CR(0x38);
       
    _delay_ms(2);
    LCD_CR(0x0c);
    //delay_ms(2);
    LCD_CR(0x14);
   _delay_ms(2);
    LCD_CR(0x06);        
    _delay_ms(2);
    LCD_CR(0x01);
            
       
       
   // while(1)
    //{
   
   // for(b=0;b<16;b++)
    //{
   // while(1)
     //       {
        LCD_PST(0,0);
        str(a);     
         
   // for(b=0;b<16;b++)
    //{ 
        LCD_PST(1,0);
        str(c);
      //  LCD_DB(0X23);
     // init_buffer();     
          //}     
      
                   
         
       // delay_ms(1000);
        //str(a);
        //LCD_PST(1,1);
        //delay_ms(1000);
        //LCD_DB(0x2b);
       
        //delay_ms(1000);
        //LCD_CR(LCD_CLEAR);
        //delay_ms(1000);
   //}

반응형