arduinoUNOで周波数カウンタを作てみた。(約10khzぐらいまで)(秋月シリアル7セグ)


目的
基板作成が落ち着いたから

秋月シリアル7セグに出力する。
レジスター直読み込みで高速化した。
「Arduino Frequency Counter Library」を使用

赤色７セグメントＬＥＤシリアルドライバキット（ＤＩＰ化キット）
[AE-7SEG-BOARD-KIT-RED]
通販コード　K-10360


約4186 C8ぐらい


![h_con24.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/e40f0bc7-9717-c585-a1fd-31305c4f4666.jpeg)




参考


http://arms22.blog91.fc2.com/blog-entry-244.html



```c++

#include <SPI.h>
#include <FreqCounter.h>

char seg[32] = {
   0x00 , //0 @ -> ' '
   0x77 , //1 A  o
   0x7c , //2 B     　combined use "6"
   0x39 , //3 C
   0x5e , //4 D
   0x79 , //5 E  o
   0x71 , //6 F
   0x3d , //7 G

   0x76 , //8 H  o
   0x06 , //9 I    　combined use "1"
   0x1e , //10 J
   0x75 , //11 K 
   0x38 , //12 L  o
   0x15 , //13 M
   0x37 , //14 N  o
   0x3f , //15 O  o　combined use "0"

   0x73  , //16 P
   0x67  , //17 Q　combined use "9"
   0x50  , //18 R
   0x6d  , //19 S　combined use "5"
   0x78  , //20 T
   0x3e  , //21 U
   0x1c  , //22 V
   0x2a  , //23 W  o
   0x64  , //24 X

   0x6e  , //25 Y
   0x5b  , //26 Z　combined use "2"
   0x4f  , //27 [  --> "3"
   0x66  , //28 \  --> "4"
   0x27  , //29 ]  --> "7"
   0x7f  , //26 ^  --> "8"
   0x08    //31 _
};

char nn[]={'O','I','Z','[','\\','S','B', ']','^','Q'};

#define DW    digitalWrite

int comma1=0; // コンマ(小数点)の有効フラグ　1で有効　0無効

//7segの一文字出力 nana_seg
int ns_putc(char ch) {

    if(ch == ' ') {
        ch = '@'-64;
    } else if(ch == '.') {
        return(0);
    } else if (ch >= '0' && ch <= '9' ) {
        ch = nn[ch-'0']-64;
    } else {
        ch = ch-64;
    }

    if( comma1 != 0 ) { comma1 = (0x80); }
    SPI.transfer(seg[ch] | comma1  );DW(SS,HIGH);DW(SS,LOW);
    comma1 = 0;

    //戻り値
    return(0);
}//ns_putc

//文字列の表示 nana_seg
int ns_printf(char *str1) {

    //文字の中身がゼロか
    while(*str1){

        //コンマの処理
        comma1=0;
        if( (*(str1+1) ) == '.' ) { comma1=1; }

        //一文字出力
        ns_putc(*str1 ++);

    } //while

    //戻り値
    return(0);
}//ns_printf

void setup() {

  // SPIの初期化
  SPI.begin(); // 11 12 13
  DW(SS, LOW); // 10
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV8);  // 2Mhz

} //setup

int n0; //温度　小数点以上
char data_read[8]; //バッファー
long int frq;

void loop() {

  FreqCounter::f_comp = 8;             // Set compensation to 12
  FreqCounter::start(100);            // Start counting with gatetime of 100ms
  while (FreqCounter::f_ready == 0); // wait until counter ready
  frq = FreqCounter::f_freq;            // read result
  frq = frq * 10;

  //表示用の変数の初期化
  int s = frq;
  int n0,n10,n100,n1000;
  
  n10   = s / 10;            // 1234 -> 123
  n0    = s - (n10 *10);     // 1234 -  1230 -> 4
  n100  = n10 / 10;          // 123  -> 12
  n10   = n10 - (n100 *10);  // 123  -  120 -> 3
  n1000 = n100 / 10;         // 12   -> 1
  n100  = n100 - (n1000*10); // 12   -  10  -> 2
  
  //画面に表示
  data_read[0] = '0' + n1000;
  data_read[1] = '0' + n100;
  data_read[2] = '0' + n10;
  data_read[3] = '0' + n0;
  data_read[4] = 0;
  ns_printf(data_read);
  delay(1000); //1秒待つ

} //loop



```

![h_con28.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/fb0652fb-66d0-43a6-77ef-bd42c698a90f.jpeg)