AVR2012.02.11 14:32

#include <mega128.h>
#include 
<delay.h>
#include 
<stdio.h>
//
//[lcd1]       [AVR PortA]
//RS (pin4) -----  bit 0
//RD (pin 5) ----- bit 1
//EN (pin 6) ----- bit 2
//사용안함         bit 3
//DB4 (pin 11) --- bit 4
//DB5 (pin 12) --- bit 5
//DB6 (pin 13) --- bit 6
//DB7 (pin 14) --- bit 7
//
#define lcd1_RS  PORTA.0
#define lcd1_E   PORTA.2
#define lcd1_Out PORTA   
// PORTA.4~7 4bits
//
flash char str1[]= " cafe.naver.com ";
flash char str2[]= " circuitsmanual ";
//
void lcd1Data(char d){

    lcd1_RS=1;
    lcd1_Out=(d&0xF0)|1; lcd1_E=1; lcd1_E=0; delay_us(1);
    lcd1_Out=(d<<4)|1;   lcd1_E=1; lcd1_E=0; delay_us(100);
}
//
void lcd1Cmd(char c){
    lcd1_RS=0;
    lcd1_Out=c&0xF0; lcd1_E=1; lcd1_E=0; delay_us(1);
    lcd1_Out=c<<4;   lcd1_E=1; lcd1_E=0; delay_ms(5);
}
//
void lcd1_init(void){
    delay_ms(150); DDRA=0xFF
// lcd port output
    lcd1Cmd(0x28); lcd1Cmd(0x28); lcd1Cmd(0x28);
    lcd1Cmd(0x0C); lcd1Cmd(0x06); lcd1Cmd(0x01); delay_ms(30); 
}
//
void lcd1_gotoxy(char x, char y){
    if(!y)lcd1Cmd(0x80+x);
    else  lcd1Cmd(0xC0+x);
}
//
void lcd1_puts(char *str){ while(*str)lcd1Data(*str++); }
//
void lcd1_putsf(char flash *str){ while(*str)lcd1Data(*str++); }
//
void main(void){
    char buf[30], i;
    int k=1234;
    lcd1_init();
    while(1){
        lcd1_gotoxy(0,0); lcd1_putsf(str1);
        lcd1_gotoxy(0,1); lcd1_putsf(" circuitsmanual ");
        delay_ms(1000);
        for(i=0; i<16; i++)buf[i]=str2[i];
        lcd1_gotoxy(0,0); lcd1_puts(buf);
        sprintf(buf,"test %05d",k);
        lcd1_gotoxy(0,1); lcd1_puts(buf);
        delay_ms(1000);
    }
}

동작 시켜본 것은 아닙니다.

필요한 분은 테스트 해보세요

Posted by 콩알은
AVR2012.02.11 14:31
//LCD 선 연결
//[LCD]            [AVR Port]
//RS (pin4) -----  bit 0
//RD (pin 5) ----- bit 1
//EN (pin 6) ----- bit 2
//DB4 (pin 11) --- bit 4
//DB5 (pin 12) --- bit 5
//DB6 (pin 13) --- bit 6
//DB7 (pin 14) --- bit 7
//
//함수들
//unsigned char lcd_read_byte(unsigned char addr);
//     reads a byte from the LCD character generator or display RAM.
//     The high level LCD Functions are:
//
//unsigned char lcd_init(unsigned char lcd_columns)
//     initializes the LCD module, clears the display and sets the printing character position at row 0 and column 0. 
//     The numbers of columns of the LCD must be specified (e.g. 16). No cursor is displayed.
//     The function returns 1 if the LCD module is detected and 0 if it is not.
//     This is the first function that must be called before using the other high level LCD Functions.
//
//void lcd_clear(void)
//     clears the LCD and sets the printing character position at row 0 and column 0.
//
//void lcd_gotoxy(unsigned char x, unsigned char y)
//     sets the current display position at column x and row y. The row and column numbering starts from 0.
//
//void lcd_putchar(char c)
//    displays the character c at the current display position.
//
//void lcd_puts(char *str)
//     displays at the current display position the string str, located in RAM.
//
//void lcd_putsf(char flash *str)
//     displays at the current display position the string str, located in FLASH.
//
//
//==================================================================
// 코드비젼 컴파일러 사용
#pragma opt- 
//최적화금지옵션
//==================================================================
#include 
<mega128.h>
#include 
<stdio.h>
#include 
<delay.h>
//==================================================================
// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x1B ;PORTA
#endasm
#include 
<lcd.h>
//==================================================================
#define  SW1 PINB.0 
// 멈춤, 시작
#define  SW2 PINB.1 
// 리셋
//==================================================================

bit  CntFlg=0, KeyFlg=0;
char Min=0, Sec=0, mSec=0;
//==================================================================
//     Interrupt Service Routinue
//==================================================================
interrupt [TIM1_COMPA] void timer1_compa_isr(void){ 
//100msec 주기
//Key check
    if((SW1==0)&&(KeyFlg==0)){
        KeyFlg=1;       
//한번의 키 입력 연속으로 인식 막기위한 플래그
        CntFlg=~CntFlg; 
//SW1눌리면 플래그 반전
    }
    else KeyFlg=0;

    if(SW2==0){    
//SW2눌리면 카운터 초기
        CntFlg=0;
        Min=Sec=mSec=0;
    }
    
//time count-up
    if(CntFlg){
        mSec++;
        if(mSec>9){ mSec=0;  Sec++; }
        if(Sec>59){ Sec=0;   Min++; }
        if(Min>59)Min=0;
    }
}

//==================================================================
//     Main Routinue
//==================================================================

void main(void){
  char buf[20];
  
  PORTB=0xff
//스위치 입력 풀업저항 사용
  DDRA=0xff;

  TCCR1B=0x0C; OCR1A=6249; TIMSK=0x10
//16000000/256/(1+6249)=10Hz=0.1sec

  lcd_init(16);
  lcd_clear();
  lcd_gotoxy(00); lcd_putsf("circuitsmanual");

  #asm("sei")

  while(1){
      lcd_gotoxy(01);
      sprintf(buf,"%2d:%2d.%d",Min,Sec,mSec);
      lcd_puts(buf); 
      delay_ms(100);
  };
}
Posted by 콩알은
AVR2012.02.11 14:29

 

#include <lcd.h> 헤더 안에 있는 함수입니다.

 

void _lcd_ready(void);

void _lcd_write_data(unsigned char data);

 

// write a byte to the LCD character generator or display RAM
void lcd_write_byte(unsigned char addr, unsigned char data);

 

// read a byte from the LCD character generator or display RAM
unsigned char lcd_read_byte(unsigned char addr);

 

// set the LCD display position  x=0..39 y=0..3
void lcd_gotoxy(unsigned char x, unsigned char y);

 

// clear the LCD
void lcd_clear(void);

void lcd_putchar(char c);

 

// write the string str located in SRAM to the LCD
void lcd_puts(char *str);

 

// write the string str located in FLASH to the LCD
void lcd_putsf(char flash *str);

 

// initialize the LCD controller
unsigned char lcd_init(unsigned char lcd_columns);

 

//--------------------------------------------------------------

#include <mega128.h>
#asm
   .equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>

 

 

void main(void){
  lcd_init(16);

  while(1){
      lcd_gotoxy(0,0);
      lcd_putsf("HELLO");
      
      lcd_gotoxy(0,1);
      lcd_puts("Lcd Test Program");
  };
}

 

 

 

 

4x24 CLCD경우에는

위처럼 선을 연결 하고서

 

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

#asm
   .equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd4x40.h>

 

void main(void){
  lcd_init();

  while(1){

      lcd_clear();
      lcd_gotoxy(0,0);
      lcd_puts("Lcd Test 1-Line");
      lcd_gotoxy(0,1);
      lcd_puts("Lcd Test 2-Line");
      lcd_gotoxy(0,2);
      lcd_puts("Lcd Test 3-Line");
      lcd_gotoxy(0,3);
      lcd_puts("Lcd Test 4-Line");

      delay_ms(1000);
  };
}

 

위처럼 하면 될 것 같습니다.

잘 안되면 lcd4x40.h 내용을 읽어보세요

Posted by 콩알은