fnd를 사용하여 초와 분을 확인할 수 있는 시계를 만들어보겠습니다.
캐소드를 이용하여 제작하였습니다.
#if 1
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#define FND_DATA_DDR DDRC
#define FND_DATA_PORT PORTC
#define FND_DIGHT_DDR DDRB
#define FND_DIGHT_PORT PORTB
#define FND_DIGHT_D1 4
#define FND_DIGHT_D2 5
#define FND_DIGHT_D3 6
#define FND_DIGHT_D4 7
int ms_count=0; // ms를 재는 변수
int sec_count=0; // 초를 재는 변수
int digit_position=0; // 자리수 선택
int main(void)
{
init_fnd();
while (1)
{
display_fnd();
_delay_ms(1);
ms_count++;
if (ms_count >= 1000) // 1000ms : 1sec
{
ms_count=0;
sec_count++;
}
}
}
void display_fnd(void)
{
#if 0 // common 애노우드
unsigned char fnd_font[] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xd8,0x80,0x98};
#else // common 캐소우드
unsigned char fnd_font[] =
{~0xc0,~0xf9,~0xa4,~0xb0,~0x99,~0x92,~0x82,~0xd8,~0x80,~0x98};
#endif
switch(digit_position)
{
case 0: // 1단위
#if 0
FND_DIGHT_PORT = 0b10000000;
#else // 캐소우드
FND_DIGHT_PORT = ~0b10000000;
#endif
FND_DATA_PORT = fnd_font[sec_count%10]; // 0~9
break;
case 1: // 10
#if 0
FND_DIGHT_PORT = 0b01000000;
#else // 캐소우드
FND_DIGHT_PORT = ~0b01000000;
#endif
FND_DATA_PORT = fnd_font[sec_count/10%6]; // 0~59
break;
case 2: // 분 단위
#if 0
FND_DIGHT_PORT = 0b00100000;
#else // 캐소우드
FND_DIGHT_PORT = ~0b00100000;
#endif
FND_DATA_PORT = fnd_font[sec_count/60%10]; // 0~59
break;
case 3: // 분 10단위
#if 0
FND_DIGHT_PORT = 0b00010000;
#else // 캐소우드
FND_DIGHT_PORT = ~0b00010000;
#endif
FND_DATA_PORT = fnd_font[sec_count/600%6]; // 0~59
break;
}
digit_position++; // 다음 표시할 자릿수 선택
digit_position %=4; // digit_position = digit_position %4;
}
void init_fnd(void)
{
FND_DATA_DDR = 0xff; // 출력 으로 설정
FND_DIGHT_DDR |= 0xf0; // 4567만1로 설정 3210은 그대로 유지
#if 0
FND_DATA_PORT = ~0x00; // all off
#else // CL5642AH: common 캐소우드 방식
FND_DATA_PORT = 0x00; // all off
#endif
}
#endif
'AVR' 카테고리의 다른 글
[AVR] PWM LED dimming 제어 (0) | 2023.03.23 |
---|---|
인터럽트를 사용해서 PORTA에 LED 깜빡거리게 켜기 (0) | 2023.03.17 |
4자리 7세그먼트 표시장치 - 기본 동작 확인 (0) | 2023.03.16 |
토글스위치 3개를 이용해 순차적 점등 및 일괄점등 (0) | 2023.03.15 |
토글스위치3개를 이용해 LED ON / OFF 조절 (0) | 2023.03.15 |