본문 바로가기

AVR

4자리수 FND 초시계와 분시계 만들기

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