학습목표    1

학습주제    1

예습내용    1

실습방법    1

        1

중요함수    1

시계 만들기

학습목표

타이머의 동작을 알아보고 활용을 알아 본다.

학습주제

타이머를 0.3 간격으로 동작시켜 이를 텍스트로 출력시킨다.

주기적으로 동작해야 하는 곳에 타이머를 활용한다.

 

예습내용

다음 함수를 알아 본다.

void _ftime( struct _timeb *timeptr );

char *ctime( const time_t *timer );

실습방법

    

(1)     프로젝트 이름을 Clock으로 만든다. 아래 해당번호의 프로그램은 ClockDlg.cpp 빨간 글씨로 표시되어 있다.

(2)     메뉴의 View->ClassWizard에서 0.5초마다 동작하는 타이머를 설정한다.

WM_TIMER 마우스 왼쪽 버튼을 두번 크릭하면 OnTimer 함수가 만든다.

BOOL CClockDlg::OnInitDialog() SetTimer(1,500,NULL); 추가하여 타이머가 0.5초마다 동작하게 한다.

(3)     Edit Box 만든다.

(5)     Edit Box Variable 아래와 같이 추가한다.

(6)     #include <sys/timeb.h> 시간을 얻기위해 헤더파일을 추가한다.

(7)      OnTimer() 함수에 시간을 얻고 Edit Box 출력하기 위한 프로그램을 추가한다.

OnTimer() 함수의 timeline 에는 "Fri Nov 09 20:43:23 2001"다음 문자가 저장되며 이중 timeline 배열의 [11]~[18]까지이다. 이를 Tbuf 복사하여 Edit Box 출력한다.

처음

   

1.        다음은꿈의소리.wav” 사운드를 연주하는 함수이다. 다음을 이용하여 하루중 일정한 시간이 되면 음악을 연주하는 프로그램을 완성하시오. 원하는 음악을 연주하려면꿈의소리.wav” 대신 다른 음악파일을 넣으면 된다.

sndPlaySound("꿈의소리", SND_SYNC);

2.        아래 그림과 같이 에디트 창을 하나 만들어서 여기에는 ,, 중에 초만 표시 되도록 프로그램 한다.

   

  변수는 아래와 같이 정의 한다.

타이머 함수를 아래와 같이 수정한다.

 

void CClockDlg::OnTimer(UINT nIDEvent)

{

              // TODO: Add your message handler code here and/or call default

              struct _timeb timebuffer;                                                   // (7)

              char *timeline;

              char Tbuf[9];

 

              _ftime( &timebuffer );

              timeline = ctime( & ( timebuffer.time ) );              

              Tbuf[0]=timeline[11];

              Tbuf[1]=timeline[12];

              Tbuf[2]=timeline[13];

              Tbuf[3]=timeline[14];

              Tbuf[4]=timeline[15];

              Tbuf[5]=timeline[16];

              Tbuf[6]=timeline[17];

              Tbuf[7]=timeline[18];

              Tbuf[8]=0x00;

              m_clock.Format("%s",Tbuf);

 

              // 만을 표시하는

              Tbuf[0]=timeline[17];

              Tbuf[1]=timeline[18];

              Tbuf[2]=0x00;

              m_clock1.Format("%s",Tbuf);

 

              UpdateData(FALSE);

 

              CDialog::OnTimer(nIDEvent);

}

 

 

 

 

3.        그림과 같이 Led 추가하여 0초가 On 되도록 프로그램 하여라.

   

4.        아래는 Led 4 만들어 초침의 십단위 숫자에 따라 led 켜지도록 프로그램한것이다.

  프로그램으 Variable 아래와 같이 정의 한다.

  

 

AVR 통신 프로그램

예제4 AVR 보드와 연결하여 동작 시킨다.

C언어 첫페이지의 RS232 기본 프로그램을 내려받아 위의 Led 추가한다. [PC통신프로그램 내려받기]

 

  

 

   Variable 아래와 같이 정의 한다.

OnTimer 함수를 아래와 같이 정의 작성한다.

 

void CRs232Dlg::OnTimer(UINT nIDEvent)

{

              // TODO: Add your message handler code here and/or call default

              struct _timeb timebuffer;                                                   // (7)

              char *timeline;

              char Tbuf[9];

              unsigned char buff[5]="";

 

              _ftime( &timebuffer );

              timeline = ctime( & ( timebuffer.time ) );              

              Tbuf[0]=timeline[11];

              Tbuf[1]=timeline[12];

              Tbuf[2]=timeline[13];

              Tbuf[3]=timeline[14];

              Tbuf[4]=timeline[15];

              Tbuf[5]=timeline[16];

              Tbuf[6]=timeline[17];

              Tbuf[7]=timeline[18];

              Tbuf[8]=0x00;

              m_clock.Format("%s",Tbuf);

 

              // 만을 표시하는

              Tbuf[0]=timeline[17];

              Tbuf[1]=timeline[18];

              Tbuf[2]=0x00;

              m_clock1.Format("%s",Tbuf);

 

              // 초침의 십단위 숫자에 따라 led 켜진다.

              if(Tbuf[0]==0x30) {

                             m_led0.Value=1;

                             m_led1.Value=0;

                             m_led2.Value=0;

                             m_led3.Value=0;

                             buff[0]=0x01;

                             m_ComuPort.WriteComm((unsigned char*)buff,1);

              }

              else if(Tbuf[0]==0x31) {

                             m_led0.Value=0;

                             m_led1.Value=1;

                             m_led2.Value=0;

                             m_led3.Value=0;

                             buff[0]=0x02;

                             m_ComuPort.WriteComm((unsigned char*)buff,1);

              }

              else if(Tbuf[0]==0x32) {

                             m_led0.Value=0;

                             m_led1.Value=0;

                             m_led2.Value=1;

                             m_led3.Value=0;

                             buff[0]=0x04;

                             m_ComuPort.WriteComm((unsigned char*)buff,1);

              }

              else if(Tbuf[0]==0x33) {

                             m_led0.Value=0;

                             m_led1.Value=0;

                             m_led2.Value=0;

                             m_led3.Value=1;

                             buff[0]=0x08;

                             m_ComuPort.WriteComm((unsigned char*)buff,1);

              }

              else {

                             m_led0.Value=0;

                             m_led1.Value=0;

                             m_led2.Value=0;

                             m_led3.Value=0;

                             buff[0]=0x00;

                             m_ComuPort.WriteComm((unsigned char*)buff,1);

              }

 

              UpdateData(FALSE);

              CDialog::OnTimer(nIDEvent);

}

 

 이번 과제 전체 소스 프로그램을 아래에서 내려 받아 실행하여 비교해 보시오.

[전체프로그램 내려받기]

중요함수

_ftime

Gets the current time.

void _ftime( struct _timeb *timeptr );

Function

Required Header

Compatibility

_ftime

<sys/types.h> and <sys/timeb.h>

Win 95, Win NT

 

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB

Single thread static library, retail version

LIBCMT.LIB

Multithread static library, retail version

MSVCRT.LIB

Import library for MSVCRT.DLL, retail version

 

Return Value

_ftime does not return a value, but fills in the fields of the structure pointed to by timeptr.

Parameter

timeptr

Pointer to _timeb structure

Remarks

The _ftime function gets the current local time and stores it in the structure pointed to by timeptr. The _timeb structure is defined in SYS\TIMEB.H. It contains four fields:

dstflag

Nonzero if daylight savings time is currently in effect for the local time zone. (See _tzset for an explanation of how daylight savings time is determined.)

millitm

Fraction of a second in milliseconds.

time

Time in seconds since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC).

timezone

Difference in minutes, moving westward, between UTC and local time. The value of timezone is set from the value of the global variable _timezone (see _tzset).

Example

/* FTIME.C: This program uses _ftime to obtain the current
 * time and then stores this time in timebuffer.
 */
 
#include <stdio.h>
#include <sys/timeb.h>
#include <time.h>
 
void main( void )
{
   struct _timeb timebuffer;
   char *timeline;
 
   _ftime( &timebuffer );
   timeline = ctime( & ( timebuffer.time ) );
 
   printf( "The time is %.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20] );
}
 

Output

The time is Tue Mar 21 15:26:41.341 1995
 

Time Management Routines

See Also   asctime, ctime, gmtime, localtime, time

 

 

ctime, _wctime

Convert a time value to a string and adjust for local time zone settings.

char *ctime( const time_t *timer );

wchar_t *_wctime( const time_t *timer );

Routine

Required Header

Compatibility

ctime

<time.h>

ANSI, Win 95, Win NT

_wctime

<time.h> or <wchar.h>

Win 95, Win NT

 

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB

Single thread static library, retail version

LIBCMT.LIB

Multithread static library, retail version

MSVCRT.LIB

Import library for MSVCRT.DLL, retail version

 

Return Value

Each of these functions returns a pointer to the character string result. If time represents a date before midnight, January 1, 1970, UTC, the function returns NULL.

Parameter

timer

Pointer to stored time

Remarks

The ctime function converts a time value stored as a time_t structure into a character string. The timer value is usually obtained from a call to time, which returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC). The string result produced by ctime contains exactly 26 characters and has the form:

Wed Jan 02 02:03:55 1980\n\0

A 24-hour clock is used. All fields have a constant width. The newline character ('\n') and the null character ('\0') occupy the last two positions of the string.

The converted character string is also adjusted according to the local time zone settings. See the time, _ftime, and localtime functions for information on configuring the local time and the _tzset function for details about defining the time zone environment and global variables.

A call to ctime modifies the single statically allocated buffer used by the gmtime and localtime functions. Each call to one of these routines destroys the result of the previous call. ctime shares a static buffer with the asctime function. Thus, a call to ctime destroys the results of any previous call to asctime, localtime, or gmtime.

_wctime is a wide-character version of ctime; _wctime returns a pointer to a wide-character string. _wctime and ctime behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine

_UNICODE & _MBCS Not Defined

_MBCS Defined

_UNICODE Defined

_tctime

ctime

ctime

_wctime

 

Example

/* CTIME.C: This program gets the current
 * time in time_t form, then uses ctime to
 * display the time in string form.
 */
 
#include <time.h>
#include <stdio.h>
 
void main( void )
{
   time_t ltime;
 
   time( &ltime );
   printf( "The time is %s\n", ctime( &ltime ) );
}
 

Output

The time is Fri Apr 29 12:25:12 1994
 

Time Management Routines

See Also   asctime, _ftime, gmtime, localtime, time