질문게시판 > 시계, 스위치 제어

인기검색어 > 아두이노 센서 ATMEGA128

최신글 질문게시판 동영상강좌 가입하기

▼ BASIC4MCU 후원업체 신제품 정보 ▼

▲ BASIC4MCU 후원업체 신제품 정보 ▲

BASIC4MCU > 질문게시판

시계, 스위치 제어

페이지 정보

작성자 반헬쭉 작성일19-11-01 00:19 조회3,739회 댓글0건

본문

CLCD에 실제 시계처럼 디스플레이 되도록 구현해보려했는데요,

00시부터 11시 59분까지는 AM

12시부터는 PM

다만, 13~21시는 1~9시로 표시

22~24시는 10~12시로 표시

초는 :가 깜빡이는걸로 표현하였습니다.

 

근데 위의 시간을 구현해보니

11시 59분 59초에서 12시 00분 00초로 넘어가기 전에 AM이 먼저 PM으로 바뀌고 딜레이가 생긴 후에 12시 00분으로 바뀌더군요..

:가 깜빡이는 딜레이도 상당하고요. 1초로 계산한건데 ,

:가 띄어지는 시간보다 띄어지지않는시간이 훨씬 긴  것 같아요. 

 

또 스위치에서는,

4번 스위치는 low level을 이용해 길게누르면 시간이 멈추고  hr이나 min이 깜빡거리고, 짧게 누르면 재시작하고

나머지는 그냥 falling edge로

5번 스위치를 누르면 hr이나 min이 증가

6번 스위치를 누르면 hr이나 min이 감소

7번 스위치를 누르면 깜박거림이나 증`감을 hr을 할것인지 min을 할것인지를 결정하는식으로

구현하려합니다.

 

시간을 증가하다보면 시간이 증가하다가 8 9 (20시 21시)에 이상한 문자가 뜨고,

분을 증가하면 59분에서 60분으로

즉, 01시 00분이 될 때

01시 00분이 바로 뜨는게 아니라 00시 60분이 뜨고 01시 00분이 뜨네요...

시계동작과 스위치를 따로했을때는 문제가 없었는데 두개를 합치니까 오류가 생기네요..

 

감소 스위치는 00분이나 00시에서 눌렀을때 59분이나 23시로 가게 하고 싶은데

조건을 어떻게 줘야할지 감이 안 잡힙니다..

 

 

 

#include <iom128v.h>
#include <avrdef.h>

void delay_ms(unsigned int m);
void clcd_init(void);
void write_instruction(char addr);
void write_data(char data);
void clock_operating(void);

char sec_1,sec_10,min_1,min_10,hr_1,hr_10;
char sec=57,min=59,hr=11;
unsigned int number;
char short_key=1, long_key=0,mode=0;

void main(void)
{
    DDRC = 0xff; // clcd data bus
    DDRG = 0x03;
    EICRB = 0xA8;
    EIMSK = 0xF0;

    SEI(); //re-enable interrupts
    clcd_init(); // clcd function reset

    while(1)
    {
        while(short_key)
        {
            clock_operating();
            if(hr==12)
            {
                write_instruction(0x88);
                write_data('P');
                write_data('M');
                write_data(' ');
                write_data(hr_10+0X30);
                write_data(hr_1+0X30);
                write_data(':');
                write_data(min_10+0X30);
                write_data(min_1+0X30);
                delay_ms(500);
                write_instruction(0x88);
                write_data('P');
                write_data('M');
                write_data(' ');
                write_data(hr_10+0X30);
                write_data(hr_1+0X30);
                write_data(' ');
                write_data(min_10+0X30);
                write_data(min_1+0X30);
                delay_ms(500);
            }
            else if((hr>=13)&&(hr<22))
            {
                write_instruction(0x88);
                write_data('P');
                write_data('M');
                write_data(' ');
                write_data(' ');
                write_data((hr_1-2)+0X30);
                write_data(':');
                write_data(min_10+0X30);
                write_data(min_1+0X30);
                delay_ms(500);
                write_instruction(0x88);
                write_data('P');
                write_data('M');
                write_data(' ');
                write_data(' ');
                write_data((hr_1-2)+0X30);
                write_data(' ');
                write_data(min_10+0X30);
                write_data(min_1+0X30);
                delay_ms(500);
            }
            else if(hr>=22)
            {
                write_instruction(0x88);
                write_data('P');
                write_data('M');
                write_data(' ');
                write_data(1+0X30);
                write_data((hr_1-2)+0X30);
                write_data(':');
                write_data(min_10+0X30);
                write_data(min_1+0X30);
                delay_ms(500);
                write_instruction(0x88);
                write_data('P');
                write_data('M');
                write_data(' ');
                write_data(1+0X30);
                write_data((hr_1-2)+0X30);
                write_data(' ');
                write_data(min_10+0X30);
                write_data(min_1+0X30);
                delay_ms(500);
            }
            else
            {
                write_instruction(0x88);
                write_data('A');
                write_data('M');
                write_data(' ');
                write_data(hr_10+0X30);
                write_data(hr_1+0X30);
                write_data(':');
                write_data(min_10+0X30);
                write_data(min_1+0X30);
                delay_ms(500);
                write_instruction(0x88);
                write_data('A');
                write_data('M');
                write_data(' ');
                write_data(hr_10+0X30);
                write_data(hr_1+0X30);
                write_data(' ');
                write_data(min_10+0X30);
                write_data(min_1+0X30);
                delay_ms(500);
            }
        }
        while(long_key)
        {
            clock_operating();
            if(mode==0)
            {
                if(hr==12)
                {
                    write_instruction(0x88);
                    write_data('P');
                    write_data('M');
                    write_data(' ');
                    write_data(hr_10+0X30);
                    write_data(hr_1+0X30);
                    write_data(':');
                    write_data(min_10+0X30);
                    write_data(min_1+0X30);
                    delay_ms(100);
                    write_instruction(0x88);
                    write_data('P');
                    write_data('M');
                    write_data(' ');
                    write_data(' ');
                    write_data(' ');
                    write_data(':');
                    write_data(min_10+0X30);
                    write_data(min_1+0X30);
                    delay_ms(100);
                }
                else if((hr>=13)&&(hr<22))
                {
                     write_instruction(0x88);
                    write_data('P');
                    write_data('M');
                    write_data(' ');
                    write_data(' ');
                    write_data((hr_1-2)+0X30);
                    write_data(':');
                    write_data(min_10+0X30);
                    write_data(min_1+0X30);
                    delay_ms(100);
                    write_instruction(0x88);
                    write_data('P');
                    write_data('M');
                    write_data(' ');
                    write_data(' ');
                    write_data(' ');
                    write_data(':');
                    write_data(min_10+0X30);
                    write_data(min_1+0X30);
                    delay_ms(100);
                }
                else if(hr>=22)
                {
                    write_instruction(0x88);
                    write_data('P');
                    write_data('M');
                    write_data(' ');
                    write_data(1+0X30);
                    write_data((hr_1-2)+0X30);
                    write_data(':');
                    write_data(min_10+0X30);
                    write_data(min_1+0X30);
                    delay_ms(100);
                    write_instruction(0x88);
                    write_data('P');
                    write_data('M');
                    write_data(' ');
                    write_data(' ');
                    write_data(' ');
                    write_data(':');
                    write_data(min_10+0X30);
                    write_data(min_1+0X30);
                    delay_ms(100);
                }
                else
                {
                    write_instruction(0x88);
                    write_data('A');
                    write_data('M');
                    write_data(' ');
                    write_data(hr_10+0X30);
                    write_data(hr_1+0X30);
                    write_data(':');
                    write_data(min_10+0X30);
                    write_data(min_1+0X30);
                    delay_ms(100);
                    write_instruction(0x88);
                    write_data('A');
                    write_data('M');
                    write_data(' ');
                    write_data(' ');
                    write_data(' ');
                    write_data(':');
                    write_data(min_10+0X30);
                    write_data(min_1+0X30);
                    delay_ms(100);
                }
            }
            if(mode==1)
            {
            if(hr==12)
            {
                write_instruction(0x88);
                write_data('P');
                write_data('M');
                write_data(' ');
                write_data(hr_10+0X30);
                write_data(hr_1+0X30);
                write_data(':');
                write_data(min_10+0X30);
                write_data(min_1+0X30);
                delay_ms(100);
                write_instruction(0x88);
                write_data('P');
                write_data('M');
                write_data(' ');
                write_data(hr_10+0X30);
                write_data(hr_1+0X30);
                write_data(':');
                write_data(' ');
                write_data(' ');
                delay_ms(100);
            }
            else if((hr>=13)&&(hr<22))
            {
                write_instruction(0x88);
                write_data('P');
                write_data('M');
                write_data(' ');
                write_data(' ');
                write_data((hr_1-2)+0X30);
                write_data(':');
                write_data(min_10+0X30);
                write_data(min_1+0X30);
                delay_ms(100);
                write_instruction(0x88);
                write_data('P');
                write_data('M');
                write_data(' ');
                write_data(' ');
                write_data((hr_1-2)+0X30);
                write_data(':');
                write_data(' ');
                write_data(' ');
                delay_ms(100);
            }
            else if(hr>=22)
            {
                write_instruction(0x88);
                write_data('P');
                write_data('M');
                write_data(' ');
                write_data(1+0X30);
                write_data((hr_1-2)+0X30);
                write_data(':');
                write_data(min_10+0X30);
                write_data(min_1+0X30);
                delay_ms(100);
                write_instruction(0x88);
                write_data('P');
                write_data('M');
                write_data(' ');
                write_data(1+0X30);
                write_data((hr_1-2)+0X30);
                write_data(':');
                write_data(' ');
                write_data(' ');
                delay_ms(100);
            }
            else
            {
                write_instruction(0x88);
                write_data('A');
                write_data('M');
                write_data(' ');
                write_data(hr_10+0X30);
                write_data(hr_1+0X30);
                write_data(':');
                write_data(min_10+0X30);
                write_data(min_1+0X30);
                delay_ms(100);
                write_instruction(0x88);
                write_data('A');
                write_data('M');
                write_data(' ');
                write_data(hr_10+0X30);
                write_data(hr_1+0X30);
                write_data(':');
                write_data(' ');
                write_data(' ');
                delay_ms(100);
            }
            }
        }
    }
}

#pragma interrupt_handler int4_isr:iv_INT4
void int4_isr(void)
{
    number++;
    delay_ms(1);
    if(number<100)
    {
        short_key = 1;
        long_key = 0;
    }
    else if ((number>=100)&&(number<250))
    {
        short_key = 0;
        long_key = 1;
    }
}

#pragma interrupt_handler int5_isr:iv_INT5
void int5_isr(void)
{
    if(long_key)
    {
        if(mode==0)
        {
            hr++;
        }
        else if(mode==1)
        {
            min++;
        }
    }
}

#pragma interrupt_handler int6_isr:iv_INT6
void int6_isr(void)
{
    if(long_key)
    {
        if(mode==0)
        {
            hr--;
        }
        else if(mode==1)
        {
            min--;
        }
    }
}

#pragma interrupt_handler int7_isr:iv_INT7
void int7_isr(void)
{
    mode^=1;
}

void delay_ms(unsigned int m)
{
    int i,j;
    for(i=0; i<m; i++)
    {
        for(j=0; j<2180; j++)
        {
            ;
        }
    }
}
void clcd_init(void)
{
    delay_ms(15);
    write_instruction(0x38);
    delay_ms(5);
    write_instruction(0x38);
    delay_ms(100);
    write_instruction(0x38);
    write_instruction(0x08);
    write_instruction(0x01);
    write_instruction(0x06);
    write_instruction(0x0C);
}
void write_instruction(char addr)
{
    PORTG = 0x02;
    PORTC = addr;
    PORTG = 0x00;
    delay_ms(1);
    PORTG = 0x02;
}
void write_data(char data)
{
    PORTG = 0x03;
    PORTC = data;
    PORTG = 0x01;
    delay_ms(1);
    PORTG = 0x03;
}

void clock_operating(void)
{
    hr_10 = hr / 10;
    hr_1 = hr - hr_10*10;
    min_10 = min / 10;
    min_1 = min - min_10*10;
    sec_10 = sec / 10;
    sec_1 = sec - sec_10*10;
    if(short_key==1)
    {
    sec++;
    delay_ms(1000);
    }
    if(sec==60)
    {
        sec=0;
        min++;
    }
    if(min==60)
    {
        min=0;
        hr++;
    }
    if(hr==24)
    {
        hr=0;
    }

 

  • BASIC4MCU 작성글 SNS에 공유하기
  • 페이스북으로 보내기
  • 트위터로 보내기
  • 구글플러스로 보내기
  • 카카오톡으로 보내기

댓글 : 0

조회수 : 3,739

등록된 댓글이 없습니다.

게시물 검색

BASIC4MCU > 질문게시판 목록

제목 날짜
공지 MCU, AVR, 아두이노 등 전자공학에 관련된 질문은… 스태프  19-01-15
공지 사이트 이용 안내 댓글[25] master  17-10-29
질문 라즈베리파이4 신형 선택문제 maximum 새글 22-06-26
질문 음전압 ADC 412904 새글 22-06-26
질문 아두이노 센서값 출력 질문입니다! 댓글[1] 아궁2  22-06-24
질문 ATmega128, ATmega28 RS232통신 첨부파일Moon4201  22-06-24
답변 답변 : ATmega128, ATmega28 RS232… 댓글[1] master 새글 22-06-25
질문 서보모터, 온습도센서 코드 질문 댓글[2] 첨부파일kshdlfaldfh  22-06-22
답변 답변 : 서보모터, 온습도센서 코드 질문 댓글[15] master  22-06-22
질문 steppermulti5 질문 응애  22-06-21
답변 답변 : steppermulti5 질문 댓글[1] master  22-06-22
질문 atmega128 avr usart 질문 snoflak  22-06-21
답변 답변 : atmega128 avr usart 질문 댓글[5] master  22-06-21
질문 사용자 정의 함수 변환 하는 방법 댓글[2] 첨부파일da0800  22-06-21
질문 atmega128 댓글[1] 양의노래  22-06-20
질문 아두이노 동시동작 재질문 댓글[9] 첨부파일죠르디  22-06-20
질문 초음파센서로 dc모터 제어하기 댓글[1] 첨부파일난쟁2  22-06-20
질문 아두이노 동시동작 댓글[7] 죠르디  22-06-19
질문 ATMEGA128 UART,FND 연동 질문 JMOD  22-06-19
답변 답변 : ATMEGA128 UART,FND 연동 질문 댓글[1] master  22-06-20
질문 atmeg128 질문이요 댓글[1] 첨부파일공대생er  22-06-18
질문 atmega128 초음파 센서로 신호등 시간초 늘리기 서동씨  22-06-18
답변 답변 : atmega128 초음파 센서로 신호등 시간초… 댓글[1] master  22-06-18
질문 atmega128 댓글[2] 망경이  22-06-18
질문 앱인벤터 이용해서 수동 자동 할라고하는데 ..잘안됩니다… 댓글[1] 따랑해영  22-06-18
답변 질문 : 앱인벤터 이용해서 수동 자동 할라고하는데 ..… 댓글[1] 첨부파일따랑해영  22-06-19
답변 질문 : 앱인벤터 이용해서 수동 자동 할라고하는데 .… 첨부파일따랑해영  22-06-19
질문 서보모터와 초음파센서 댓글[1] 브링미히어  22-06-17
질문 LCD 도와주세요 ㅜㅠㅠㅠ 댓글[2] 첨부파일dentist22  22-06-17
질문 안녕하세요 atmega128 질문드리고 싶습니다. (자… 댓글[3] dlcldl  22-06-17
게시물 검색


Privacy Policy
MCU BASIC ⓒ 2017
PC버전