티스토리 뷰

#include <io.h>

#include <mega128.h>

#include <delay.h>


unsigned char sw;

int count;

int h_time, m_time, s_time;

int h_save, m_save, s_save;

unsigned char status;

unsigned char led;

unsigned char two_line;



void lcd_func(void){    // 기능 설정

    PORTG = 0x01;       // 명령어 쓰는 신호

    PORTA = 0x38;       // 0b00111000    8비트, 2행, 5*7도

    delay_us(39);

    PORTG = 0x00;

}

void lcd_pic(void){     // 화면 설정

    PORTG = 0x01;      

    PORTA = 0x0e;       // 0b00001110   화면on, 커서 on, 블링크 off

    delay_us(39);

    PORTG = 0x00;

}

void lcd_mv(void){      // 커서위치

    PORTG = 0x01;

    PORTA = 0x06;       // 0b00000110   중가, 표시시프트 off

    delay_us(39);

    PORTG = 0x00;

}

void lcd_clear(void){

    PORTG = 0x01;

    PORTA = 0x01;       // clear

    delay_us(2);

    PORTG = 0x00;

}


void lcd_data(unsigned char a, unsigned char b){        // 데이터 넣기


    sw = 0x00;

    if(a == 1)sw = 0xc0;    // a가 1일 때, sw는 0b11000000; 0b00000000;

    

    PORTG = 0x01;           // 명령어 신호

    PORTA = (0x80|b|sw);    // 0b10000000|8bit|11000000 or 00000000 == 0b11010000|b 하위 7비트 사용해서 DDRAM 저장  

    delay_us(39);

    PORTG = 0x00;

}

void lcd_read(unsigned char a){  // 데이터 불러오기(화면 입장에서 read, 저장공간에서 읽어옴)

    PORTG = 0x05;           // 0b00000101, 쓰기신호, Enable

    PORTA = a;              // 

    delay_us(43);

    PORTG = 0x04;           // 100 쓰기, Disenable

}


void lcd_str(unsigned char *str){

    while(*str != 0){

        lcd_read(*str);

        str++;

    }

}


void init_timer(void){

    TCCR2 = 0x00;

    TCCR2 |= (1<<CS21);

    TCNT2 = 55;

    TIMSK = (1<<TOIE2);

}


void timer_stop(void){

    TIMSK = (0<<TOIE2);

}


void timer_start(void){

    TIMSK = (1<<TOIE2);

}


interrupt [TIM2_OVF] void overflow(void){

   TCNT2 = 55;

   count++;

   if(count > 100){

        if(s_time == 59){

            s_time = 0;

            m_time++;      

            

            if(led == 0b00000001)led = 0b10000000;

            else led = led >> 1;

        }            

        if(m_time == 59){

            m_time = 0;

            h_time++;


        }            

        if(h_time == 23){

            h_time++;

        }           

        

        count = 0;

        s_time++; 

    

   }

}


interrupt [2] void int0_func(){   // INT.0의 인터럽트 벡터는 2

        if(status == 0) {

                        status = 1;

        } else {

                       status = 0;

        }  

          lcd_clear();

             two_line = 1;  

        

}


interrupt [3] void int1_func(){   // INT.1의 인터럽트 벡터는 3


   status = 0;


       h_time = 0;

       m_time = 0; 

       s_time = 0;           

       

               lcd_clear();

       two_line = 1;

}

interrupt [4] void int2_func(){   // INT.2의 인터럽트 벡터는 4

    h_save = h_time;

    m_save = m_time;

    s_save = s_time;       

}

interrupt [5] void int3_func(){   // INT.3의 인터럽트 벡터는 5

        lcd_data(1,0);

        lcd_read((h_save/10) + '0');

        lcd_read((h_save%10) + '0');

        lcd_read(':');

        lcd_read((m_save/10) + '0');

        lcd_read((m_save%10) + '0');

        lcd_read(':');

        lcd_read((s_save/10) + '0');

        lcd_read((s_save%10) + '0');

        lcd_read(':');

        lcd_read((count/10) + '0');

        lcd_read((count%10) + '0');

        delay_ms(3000);        

        

                       lcd_clear();

       two_line = 1;

}



void main(){

    EIMSK = 0b00001111;

    EICRA = 0b00000000;

    SREG |= 0x80;

    DDRB = 0xff;

    DDRD = 0x00;

    led = 0b10000000;

    status = 0; 

    two_line = 0;


    init_timer();

    SREG |= 0x80; 

    DDRG = 0x07;

    DDRA = 0xff;

    

    lcd_clear();  

    delay_ms(10);


    lcd_func();

    lcd_pic();

    lcd_mv();  

    

    s_time = 0;

    m_time = 0;

    h_time = 0;

    count = 0;

    

    while(1){

        lcd_data(1,0);

        lcd_read((h_time/10) + '0');

        lcd_read((h_time%10) + '0');

        lcd_read(':');

        lcd_read((m_time/10) + '0');

        lcd_read((m_time%10) + '0');

        lcd_read(':');

        lcd_read((s_time/10) + '0');

        lcd_read((s_time%10) + '0');

        lcd_read(':');

        lcd_read((count/10) + '0');

        lcd_read((count%10) + '0');

        PORTB = ~led;

        if(status == 1){

            timer_stop();

        }else{

          timer_start();

        }

       

    }

}

'프로그래밍 > CodeVision' 카테고리의 다른 글

마이크로프로세서181009  (0) 2018.10.09
181002 마이크로프로세서  (0) 2018.10.02
190918 마이크로프로세서  (0) 2018.09.18
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함