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 콩알은