[당근보드] 4개의FND에 숫자 차례로 카운트하고 버튼누르면 잠시 멈추기

글 내용

이 소스를 당근보드에 적용하면 FND에 0000부터 9999까지 카운트되며,
SW1을 누르면 카운트가 잠시 멈췄다가 다시 시작하는 예제입니다. SW2를 누르면 숫자가 초기화 됩니다.

----------------------------------------------------------------------------------------
#include <avr/io.h>
#include <carroty/delay.h>
#include <carroty/fundamental.h>
#include <avr/interrupt.h>
#include <carroty/fnd.h>
#include <avr/signal.h>

unsigned char number_array[16]= { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, };
unsigned char fnd_sel_array[4]= { 0,0,0,0,};
int n=0, n10, n100, n1000;

void display_fnd(unsigned char number, unsigned char fnd_select) {
  PORTB= number;
  PORTC= fnd_select;
}

SIGNAL(SIG_INTERRUPT0) {
 for(int g=0;g<200;g++){
  display_fnd(number_array[n1000], fnd_sel_array[0]); delay_ms(3);
  display_fnd(FULL_OFF, fnd_sel_array[0]);    delay_us(10);
  display_fnd(number_array[n100], fnd_sel_array[1]);  delay_ms(3);
  display_fnd(FULL_OFF, fnd_sel_array[1]);    delay_us(10);
  display_fnd(number_array[n10], fnd_sel_array[2]); delay_ms(3);
  display_fnd(FULL_OFF, fnd_sel_array[2]);    delay_us(10);
  display_fnd(number_array[n], fnd_sel_array[3]);  delay_ms(3);
  display_fnd(FULL_OFF, fnd_sel_array[3]);    delay_us(10);
 }
}

int main(void) {
 DDRB= 0xFF;
 DDRC= 0xF0;
 DDRD= 0x00;
 
 MCUCR = 0x0A;
 GICR = 0xC0;
 sei();

 int count= 1;

 number_array[0]=  NUMBER_0;
 number_array[1]=  NUMBER_1;
 number_array[2]=  NUMBER_2;
 number_array[3]=  NUMBER_3;
 number_array[4]=  NUMBER_4;
 number_array[5]=  NUMBER_5;
 number_array[6]=  NUMBER_6;
 number_array[7]=  NUMBER_7;
 number_array[8]=  NUMBER_8;
 number_array[9]=  NUMBER_9;

 number_array[10]= NUMBER_A;
 number_array[11]= NUMBER_B;
 number_array[12]= NUMBER_C;
 number_array[13]= NUMBER_D;
 number_array[14]= NUMBER_E;
 number_array[15]= NUMBER_F;

 fnd_sel_array[0]= FND_SEL_1;
 fnd_sel_array[1]= FND_SEL_2;
 fnd_sel_array[2]= FND_SEL_3;
 fnd_sel_array[3]= FND_SEL_4;

  while(true) {
 
 count+=1;
 n1000=count/1000;
 n100=(count-(count/1000*1000))/100;
 n10=(count-(count/100*100))/10;
 n=count-(count/10*10);

 display_fnd(number_array[n1000], fnd_sel_array[0]); delay_ms(3);
 display_fnd(FULL_OFF, fnd_sel_array[0]);    delay_us(10);
 display_fnd(number_array[n100], fnd_sel_array[1]);  delay_ms(3);
 display_fnd(FULL_OFF, fnd_sel_array[1]);    delay_us(10);
 display_fnd(number_array[n10], fnd_sel_array[2]); delay_ms(3);
 display_fnd(FULL_OFF, fnd_sel_array[2]);    delay_us(10);
 display_fnd(number_array[n], fnd_sel_array[3]);  delay_ms(3);
 display_fnd(FULL_OFF, fnd_sel_array[3]);    delay_us(10);

 if(count==10000) count=0;
  }
  return(1);
}

------------------------------------------------------------------------------------------

작동 모습은 아래와 같습니다.